intial
This commit is contained in:
5
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/extract-leading-comments.d.ts
generated
vendored
Normal file
5
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/extract-leading-comments.d.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../types.js';
|
||||
import type { ASTNodeWithParent } from '../../types-for-node.js';
|
||||
/** Extract comments */
|
||||
export declare function extractLeadingComments(context: RuleContext, node: ASTNodeWithParent): (AST.Token | AST.Comment)[];
|
||||
28
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/extract-leading-comments.js
generated
vendored
Normal file
28
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/extract-leading-comments.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
import { isOpeningParenToken } from '@eslint-community/eslint-utils';
|
||||
/** Extract comments */
|
||||
export function extractLeadingComments(context, node) {
|
||||
const sourceCode = context.sourceCode;
|
||||
const beforeToken = sourceCode.getTokenBefore(node, {
|
||||
includeComments: false,
|
||||
filter(token) {
|
||||
if (isOpeningParenToken(token)) {
|
||||
return false;
|
||||
}
|
||||
const astToken = token;
|
||||
if (astToken.type === 'HTMLText') {
|
||||
return false;
|
||||
}
|
||||
return astToken.type !== 'HTMLComment';
|
||||
}
|
||||
});
|
||||
if (beforeToken) {
|
||||
return sourceCode
|
||||
.getTokensBetween(beforeToken, node, { includeComments: true })
|
||||
.filter(isComment);
|
||||
}
|
||||
return sourceCode.getTokensBefore(node, { includeComments: true }).filter(isComment);
|
||||
}
|
||||
/** Checks whether given token is comment token */
|
||||
function isComment(token) {
|
||||
return token.type === 'HTMLComment' || token.type === 'Block' || token.type === 'Line';
|
||||
}
|
||||
15
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/ignore-comment.d.ts
generated
vendored
Normal file
15
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/ignore-comment.d.ts
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../types.js';
|
||||
export type IgnoreItemWithoutCode = {
|
||||
range: [number, number];
|
||||
code: null;
|
||||
token: AST.Token | AST.Comment;
|
||||
};
|
||||
export type IgnoreItem = {
|
||||
range: [number, number];
|
||||
code: string;
|
||||
codeForV5: string;
|
||||
token: AST.Token | AST.Comment;
|
||||
};
|
||||
/** Extract all svelte-ignore comment items */
|
||||
export declare function getSvelteIgnoreItems(context: RuleContext): (IgnoreItem | IgnoreItemWithoutCode)[];
|
||||
107
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/ignore-comment.js
generated
vendored
Normal file
107
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/ignore-comment.js
generated
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
const SVELTE_IGNORE_PATTERN = /^\s*svelte-ignore\s+/;
|
||||
/** Matches parenthetical notes in svelte-ignore comments, e.g. "(because of reasons)" */
|
||||
const PARENTHETICAL_NOTE_PATTERN = /\([^)]*\)/gu;
|
||||
/**
|
||||
* Map of legacy code -> new code
|
||||
* See https://github.com/sveltejs/svelte/blob/c9202a889612df3c2fcb369096a5573668be99d6/packages/svelte/src/compiler/utils/extract_svelte_ignore.js#L6
|
||||
*/
|
||||
const V5_REPLACEMENTS = {
|
||||
'non-top-level-reactive-declaration': 'reactive_declaration_invalid_placement',
|
||||
'module-script-reactive-declaration': 'reactive_declaration_module_script',
|
||||
'empty-block': 'block_empty',
|
||||
'avoid-is': 'attribute_avoid_is',
|
||||
'invalid-html-attribute': 'attribute_invalid_property_name',
|
||||
'a11y-structure': 'a11y_figcaption_parent',
|
||||
'illegal-attribute-character': 'attribute_illegal_colon',
|
||||
'invalid-rest-eachblock-binding': 'bind_invalid_each_rest',
|
||||
'unused-export-let': 'export_let_unused'
|
||||
};
|
||||
/** Extract all svelte-ignore comment items */
|
||||
export function getSvelteIgnoreItems(context) {
|
||||
const sourceCode = context.sourceCode;
|
||||
const ignoreComments = [];
|
||||
for (const comment of sourceCode.getAllComments()) {
|
||||
const match = SVELTE_IGNORE_PATTERN.exec(comment.value);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const codeListStart = match.index + match[0].length;
|
||||
const codeList = comment.value.slice(codeListStart);
|
||||
if (hasMissingCodeIgnore(codeList)) {
|
||||
ignoreComments.push({
|
||||
range: comment.range,
|
||||
code: null,
|
||||
token: comment
|
||||
});
|
||||
}
|
||||
else {
|
||||
const ignores = extractSvelteIgnore(comment.range[0] + 2, comment, codeList, codeListStart);
|
||||
if (ignores) {
|
||||
ignoreComments.push(...ignores);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const token of sourceCode.ast.tokens) {
|
||||
if (token.type === 'HTMLComment') {
|
||||
const text = token.value.slice(4, -3);
|
||||
const match = SVELTE_IGNORE_PATTERN.exec(text);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const codeListStart = match.index + match[0].length;
|
||||
const codeList = text.slice(codeListStart);
|
||||
if (hasMissingCodeIgnore(codeList)) {
|
||||
ignoreComments.push({
|
||||
range: token.range,
|
||||
code: null,
|
||||
token
|
||||
});
|
||||
}
|
||||
else {
|
||||
const ignores = extractSvelteIgnore(token.range[0] + 4, token, codeList, codeListStart);
|
||||
if (ignores) {
|
||||
ignoreComments.push(...ignores);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ignoreComments.sort((a, b) => b.range[0] - a.range[0]);
|
||||
return ignoreComments;
|
||||
}
|
||||
/** Extract svelte-ignore rule names */
|
||||
function extractSvelteIgnore(startIndex, token, codeList, ignoreStart) {
|
||||
const start = startIndex + ignoreStart;
|
||||
const results = [];
|
||||
// Replace parenthetical notes (e.g., "(because of reasons)") with spaces of the same length
|
||||
// to preserve character positions while preventing note words from being treated as rule names.
|
||||
const processedCodeList = codeList.replace(PARENTHETICAL_NOTE_PATTERN, (match) => ' '.repeat(match.length));
|
||||
const separatorPattern = /\s*[\s,]\s*/g;
|
||||
const separators = processedCodeList.matchAll(separatorPattern);
|
||||
let lastSeparatorEnd = 0;
|
||||
for (const separator of separators) {
|
||||
const code = processedCodeList.slice(lastSeparatorEnd, separator.index);
|
||||
if (code) {
|
||||
results.push({
|
||||
code,
|
||||
codeForV5: V5_REPLACEMENTS[code] || code.replace(/-/gu, '_'),
|
||||
range: [start + lastSeparatorEnd, start + separator.index],
|
||||
token
|
||||
});
|
||||
}
|
||||
lastSeparatorEnd = separator.index + separator[0].length;
|
||||
}
|
||||
if (results.length === 0) {
|
||||
const code = processedCodeList;
|
||||
results.push({
|
||||
code,
|
||||
codeForV5: V5_REPLACEMENTS[code] || code.replace(/-/gu, '_'),
|
||||
range: [start, start + code.length],
|
||||
token
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
/** Checks whether given comment has missing code svelte-ignore */
|
||||
function hasMissingCodeIgnore(codeList) {
|
||||
return !codeList.replace(PARENTHETICAL_NOTE_PATTERN, '').trim();
|
||||
}
|
||||
32
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/index.d.ts
generated
vendored
Normal file
32
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../types.js';
|
||||
import type { IgnoreItem } from './ignore-comment.js';
|
||||
export type SvelteCompileWarnings = {
|
||||
warnings: Warning[];
|
||||
unusedIgnores: IgnoreItem[];
|
||||
kind: 'warn' | 'error';
|
||||
stripStyleElements: AST.SvelteStyleElement[];
|
||||
};
|
||||
export type Loc = {
|
||||
start?: {
|
||||
line: number;
|
||||
column: number;
|
||||
character: number;
|
||||
};
|
||||
end?: {
|
||||
line: number;
|
||||
column: number;
|
||||
character: number;
|
||||
};
|
||||
};
|
||||
export type Warning = ({
|
||||
code: string;
|
||||
message: string;
|
||||
} | {
|
||||
code?: undefined;
|
||||
message: string;
|
||||
}) & Loc;
|
||||
/**
|
||||
* Get svelte compile warnings
|
||||
*/
|
||||
export declare function getSvelteCompileWarnings(context: RuleContext): SvelteCompileWarnings;
|
||||
524
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/index.js
generated
vendored
Normal file
524
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/index.js
generated
vendored
Normal file
@@ -0,0 +1,524 @@
|
||||
import * as compiler from 'svelte/compiler';
|
||||
import { decode } from '@jridgewell/sourcemap-codec';
|
||||
import { LinesAndColumns } from '../../utils/lines-and-columns.js';
|
||||
import { hasTypeScript, transform as transformWithTypescript } from './transform/typescript.js';
|
||||
import { transform as transformWithBabel } from './transform/babel.js';
|
||||
import { transform as transformWithPostCSS } from './transform/postcss.js';
|
||||
import { transform as transformWithSass } from './transform/sass.js';
|
||||
import { transform as transformWithLess } from './transform/less.js';
|
||||
import { transform as transformWithStylus } from './transform/stylus.js';
|
||||
import { getSvelteIgnoreItems } from './ignore-comment.js';
|
||||
import { extractLeadingComments } from './extract-leading-comments.js';
|
||||
import { findAttribute, getLangValue } from '../../utils/ast-utils.js';
|
||||
import { getSvelteVersion } from '../../utils/svelte-context.js';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
const STYLE_TRANSFORMS = {
|
||||
postcss: transformWithPostCSS,
|
||||
pcss: transformWithPostCSS,
|
||||
scss: (node, text, context) => transformWithSass(node, text, context, 'scss'),
|
||||
sass: (node, text, context) => transformWithSass(node, text, context, 'sass'),
|
||||
less: transformWithLess,
|
||||
stylus: transformWithStylus,
|
||||
styl: transformWithStylus
|
||||
};
|
||||
const CSS_WARN_CODES = new Set([
|
||||
'css-unused-selector',
|
||||
'css_unused_selector',
|
||||
'css-invalid-global',
|
||||
'css-invalid-global-selector'
|
||||
]);
|
||||
const cacheAll = new WeakMap();
|
||||
/**
|
||||
* Get svelte compile warnings
|
||||
*/
|
||||
export function getSvelteCompileWarnings(context) {
|
||||
const sourceCode = context.sourceCode;
|
||||
const cache = cacheAll.get(sourceCode.ast);
|
||||
if (cache) {
|
||||
return cache;
|
||||
}
|
||||
const result = getSvelteCompileWarningsWithoutCache(context);
|
||||
cacheAll.set(sourceCode.ast, result);
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Get svelte compile warnings
|
||||
*/
|
||||
function getSvelteCompileWarningsWithoutCache(context) {
|
||||
const sourceCode = context.sourceCode;
|
||||
// Process for styles
|
||||
const styleElementsWithNotCSS = [...extractStyleElementsWithLangOtherThanCSS(context)];
|
||||
const stripStyleElements = [];
|
||||
const transformResults = [];
|
||||
for (const style of styleElementsWithNotCSS) {
|
||||
const transform = STYLE_TRANSFORMS[style.lang];
|
||||
if (transform) {
|
||||
const result = transform(style.node, context.sourceCode.text, context);
|
||||
if (result) {
|
||||
transformResults.push(result);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
stripStyleElements.push(style.node);
|
||||
}
|
||||
const stripStyleTokens = stripStyleElements.flatMap((e) => e.children);
|
||||
const ignoreComments = getSvelteIgnoreItems(context).filter((item) => item.code != null);
|
||||
const text = buildStrippedText(context, ignoreComments, stripStyleTokens);
|
||||
transformResults.push(...transformScripts(context, text));
|
||||
if (!transformResults.length) {
|
||||
const warnings = getWarningsFromCode(text, context);
|
||||
return {
|
||||
...processIgnore(warnings.warnings, warnings.kind, stripStyleElements, ignoreComments, context),
|
||||
kind: warnings.kind,
|
||||
stripStyleElements
|
||||
};
|
||||
}
|
||||
class RemapContext {
|
||||
constructor() {
|
||||
this.originalStart = 0;
|
||||
this.code = '';
|
||||
this.locs = null;
|
||||
this.mapIndexes = [];
|
||||
}
|
||||
appendOriginal(endIndex) {
|
||||
const codeStart = this.code.length;
|
||||
const start = this.originalStart;
|
||||
this.code += text.slice(start, endIndex);
|
||||
this.originalStart = endIndex;
|
||||
const offset = start - codeStart;
|
||||
this.mapIndexes.push({
|
||||
range: [codeStart, this.code.length],
|
||||
remap(index) {
|
||||
return index + offset;
|
||||
}
|
||||
});
|
||||
}
|
||||
postprocess() {
|
||||
this.appendOriginal(text.length);
|
||||
return this.code;
|
||||
}
|
||||
appendTranspile(output) {
|
||||
const endIndex = output.inputRange[1];
|
||||
const codeStart = this.code.length;
|
||||
const start = this.originalStart;
|
||||
const inputText = text.slice(start, endIndex);
|
||||
const outputText = `${output.output}\n`;
|
||||
this.code += outputText;
|
||||
this.originalStart = endIndex;
|
||||
let outputLocs = null;
|
||||
let inputLocs = null;
|
||||
let decoded = null;
|
||||
this.mapIndexes.push({
|
||||
range: [codeStart, this.code.length],
|
||||
remap: (index) => {
|
||||
outputLocs = outputLocs ?? new LinesAndColumns(outputText);
|
||||
inputLocs = inputLocs ?? new LinesAndColumns(inputText);
|
||||
const outputCodePos = outputLocs.getLocFromIndex(index - codeStart);
|
||||
const inputCodePos = remapPosition(outputCodePos);
|
||||
return inputLocs.getIndexFromLoc(inputCodePos) + start;
|
||||
}
|
||||
});
|
||||
/** Remapping source position */
|
||||
function remapPosition(pos) {
|
||||
decoded = decoded ?? decode(output.mappings);
|
||||
const lineMaps = decoded[pos.line - 1];
|
||||
if (!lineMaps?.length) {
|
||||
for (let line = pos.line - 1; line >= 0; line--) {
|
||||
const prevLineMaps = decoded[line];
|
||||
if (prevLineMaps?.length) {
|
||||
const [, , sourceCodeLine, sourceCodeColumn] = prevLineMaps[prevLineMaps.length - 1];
|
||||
return {
|
||||
line: sourceCodeLine + 1,
|
||||
column: sourceCodeColumn
|
||||
};
|
||||
}
|
||||
}
|
||||
return { line: -1, column: -1 };
|
||||
}
|
||||
for (let index = 0; index < lineMaps.length - 1; index++) {
|
||||
const [generateCodeColumn, , sourceCodeLine, sourceCodeColumn] = lineMaps[index];
|
||||
if (generateCodeColumn <= pos.column && pos.column < lineMaps[index + 1][0]) {
|
||||
return {
|
||||
line: sourceCodeLine + 1,
|
||||
column: sourceCodeColumn + (pos.column - generateCodeColumn)
|
||||
};
|
||||
}
|
||||
}
|
||||
const [generateCodeColumn, , sourceCodeLine, sourceCodeColumn] = lineMaps[lineMaps.length - 1];
|
||||
return {
|
||||
line: sourceCodeLine + 1,
|
||||
column: sourceCodeColumn + (pos.column - generateCodeColumn)
|
||||
};
|
||||
}
|
||||
}
|
||||
remapLocs(points) {
|
||||
const mapIndexes = this.mapIndexes;
|
||||
const locs = (this.locs = this.locs ?? new LinesAndColumns(this.code));
|
||||
let start = undefined;
|
||||
let end = undefined;
|
||||
if (points.start) {
|
||||
const index = locs.getIndexFromLoc(points.start);
|
||||
const remapped = remapIndex(index);
|
||||
if (remapped) {
|
||||
start = { ...sourceCode.getLocFromIndex(remapped), character: remapped };
|
||||
}
|
||||
}
|
||||
if (points.end) {
|
||||
const index = locs.getIndexFromLoc(points.end);
|
||||
const remapped = remapIndex(index - 1 /* include index */);
|
||||
if (remapped) {
|
||||
const character = remapped + 1; /* restore */
|
||||
end = { ...sourceCode.getLocFromIndex(character), character };
|
||||
}
|
||||
}
|
||||
return { start, end };
|
||||
/** remap index */
|
||||
function remapIndex(index) {
|
||||
for (const mapIndex of mapIndexes) {
|
||||
if (mapIndex.range[0] <= index && index < mapIndex.range[1]) {
|
||||
return mapIndex.remap(index);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
const remapContext = new RemapContext();
|
||||
for (const result of transformResults.sort((a, b) => a.inputRange[0] - b.inputRange[0])) {
|
||||
remapContext.appendOriginal(result.inputRange[0]);
|
||||
remapContext.appendTranspile(result);
|
||||
}
|
||||
const code = remapContext.postprocess();
|
||||
const baseWarnings = getWarningsFromCode(code, context);
|
||||
const warnings = [];
|
||||
for (const warn of baseWarnings.warnings) {
|
||||
let loc = null;
|
||||
/** Get re-mapped location */
|
||||
// eslint-disable-next-line func-style -- ignore
|
||||
const getLoc = function getLoc() {
|
||||
if (loc) {
|
||||
return loc;
|
||||
}
|
||||
return (loc = remapContext.remapLocs(warn));
|
||||
};
|
||||
warnings.push({
|
||||
code: warn.code,
|
||||
message: warn.message,
|
||||
get start() {
|
||||
return getLoc().start;
|
||||
},
|
||||
get end() {
|
||||
return getLoc().end;
|
||||
}
|
||||
});
|
||||
}
|
||||
return {
|
||||
...processIgnore(warnings, baseWarnings.kind, stripStyleElements, ignoreComments, context),
|
||||
kind: baseWarnings.kind,
|
||||
stripStyleElements
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Extracts the style with the lang attribute other than CSS.
|
||||
*/
|
||||
function* extractStyleElementsWithLangOtherThanCSS(context) {
|
||||
const sourceCode = context.sourceCode;
|
||||
const root = sourceCode.ast;
|
||||
for (const node of root.body) {
|
||||
if (node.type === 'SvelteStyleElement') {
|
||||
const lang = getLangValue(node);
|
||||
if (lang != null && lang.toLowerCase() !== 'css') {
|
||||
yield { node, lang: lang.toLowerCase() };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Build the text stripped of tokens that are not needed for compilation.
|
||||
*/
|
||||
function buildStrippedText(context, ignoreComments, stripStyleTokens) {
|
||||
const sourceCode = context.sourceCode;
|
||||
const baseText = sourceCode.text;
|
||||
const stripTokens = new Set([...ignoreComments.map((item) => item.token), ...stripStyleTokens]);
|
||||
if (!stripTokens.size) {
|
||||
return baseText;
|
||||
}
|
||||
let code = '';
|
||||
let start = 0;
|
||||
for (const token of [...stripTokens].sort((a, b) => a.range[0] - b.range[0])) {
|
||||
code +=
|
||||
baseText.slice(start, token.range[0]) +
|
||||
baseText.slice(...token.range).replace(/[^\t\n\r ]/g, ' ');
|
||||
start = token.range[1];
|
||||
}
|
||||
code += baseText.slice(start);
|
||||
return code;
|
||||
}
|
||||
/** Returns the result of transforming the required script for the transform. */
|
||||
function* transformScripts(context, text) {
|
||||
const transform = isUseTypeScript(context)
|
||||
? hasTypeScript(context)
|
||||
? transformWithTypescript
|
||||
: transformWithBabel
|
||||
: isUseBabel(context)
|
||||
? transformWithBabel
|
||||
: null;
|
||||
const sourceCode = context.sourceCode;
|
||||
if (transform) {
|
||||
const root = sourceCode.ast;
|
||||
for (const node of root.body) {
|
||||
if (node.type === 'SvelteScriptElement') {
|
||||
const result = transform(node, text, context);
|
||||
if (result) {
|
||||
yield result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function isCustomElement(program) {
|
||||
return program.body.some((body) => {
|
||||
if (body.type !== 'SvelteElement' || body.kind !== 'special') {
|
||||
return false;
|
||||
}
|
||||
if (body.name.name !== 'svelte:options') {
|
||||
return false;
|
||||
}
|
||||
return Boolean(findAttribute(body, 'tag')) || Boolean(findAttribute(body, 'customElement'));
|
||||
});
|
||||
}
|
||||
const svelteVersion = getSvelteVersion();
|
||||
/**
|
||||
* Get compile warnings
|
||||
*/
|
||||
function getWarningsFromCode(code, context) {
|
||||
try {
|
||||
const svelteConfig = context.sourceCode.parserServices.svelteParseContext?.svelteConfig;
|
||||
const compilerOptions = svelteConfig?.compilerOptions ?? {};
|
||||
const result = compiler.compile(code, {
|
||||
...(svelteVersion === '5'
|
||||
? {
|
||||
experimental: {
|
||||
async: compilerOptions.experimental?.async
|
||||
}
|
||||
}
|
||||
: {}),
|
||||
generate: false,
|
||||
...(isCustomElement(context.sourceCode.ast) ? { customElement: true } : {})
|
||||
});
|
||||
return { warnings: result.warnings, kind: 'warn' };
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
|
||||
}
|
||||
catch (e) {
|
||||
return {
|
||||
warnings: [
|
||||
{
|
||||
code: e.code,
|
||||
message: e.message,
|
||||
start: e.start,
|
||||
end: e.end
|
||||
}
|
||||
],
|
||||
kind: 'error'
|
||||
};
|
||||
}
|
||||
}
|
||||
/** Ignore process */
|
||||
function processIgnore(warnings, kind, stripStyleElements, ignoreComments, context) {
|
||||
if (kind === 'error') {
|
||||
return {
|
||||
warnings,
|
||||
unusedIgnores: ignoreComments
|
||||
};
|
||||
}
|
||||
const sourceCode = context.sourceCode;
|
||||
const unusedIgnores = new Set(ignoreComments);
|
||||
const remainingWarning = new Set(warnings);
|
||||
for (const warning of warnings) {
|
||||
if (!warning.code) {
|
||||
continue;
|
||||
}
|
||||
let node = getWarningNode(warning);
|
||||
while (node) {
|
||||
for (const comment of extractLeadingComments(context, node).reverse()) {
|
||||
const ignoreItem = ignoreComments.find((item) => item.token === comment &&
|
||||
(item.code === warning.code || item.codeForV5 === warning.code));
|
||||
if (ignoreItem) {
|
||||
unusedIgnores.delete(ignoreItem);
|
||||
remainingWarning.delete(warning);
|
||||
break;
|
||||
}
|
||||
}
|
||||
node = getIgnoreParent(node);
|
||||
}
|
||||
}
|
||||
// Stripped styles are ignored from compilation and cannot determine css errors.
|
||||
for (const node of stripStyleElements) {
|
||||
for (const comment of extractLeadingComments(context, node).reverse()) {
|
||||
const ignoreItem = ignoreComments.find((item) => item.token === comment &&
|
||||
(CSS_WARN_CODES.has(item.code) || CSS_WARN_CODES.has(item.codeForV5)));
|
||||
if (ignoreItem) {
|
||||
unusedIgnores.delete(ignoreItem);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
warnings: [...remainingWarning],
|
||||
unusedIgnores: [...unusedIgnores]
|
||||
};
|
||||
/** Get ignore target parent node */
|
||||
function getIgnoreParent(node) {
|
||||
if (node.type !== 'SvelteElement' &&
|
||||
node.type !== 'SvelteIfBlock' &&
|
||||
node.type !== 'SvelteKeyBlock' &&
|
||||
node.type !== 'SvelteEachBlock' &&
|
||||
node.type !== 'SvelteAwaitBlock') {
|
||||
return null;
|
||||
}
|
||||
const parent = node.parent;
|
||||
if (parent.type === 'SvelteElseBlock') {
|
||||
return parent.parent; // SvelteIfBlock or SvelteEachBlock
|
||||
}
|
||||
if (parent.type === 'SvelteAwaitPendingBlock' ||
|
||||
parent.type === 'SvelteAwaitThenBlock' ||
|
||||
parent.type === 'SvelteAwaitCatchBlock') {
|
||||
return parent.parent; // SvelteAwaitBlock
|
||||
}
|
||||
if (parent.type !== 'SvelteElement' &&
|
||||
parent.type !== 'SvelteIfBlock' &&
|
||||
parent.type !== 'SvelteKeyBlock' &&
|
||||
parent.type !== 'SvelteEachBlock'
|
||||
// && parent.type !== "SvelteAwaitBlock"
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
/** Get warning node */
|
||||
function getWarningNode(warning) {
|
||||
const indexes = getWarningIndexes(warning);
|
||||
if (indexes.start != null) {
|
||||
const node = getWarningTargetNodeFromIndex(indexes.start);
|
||||
if (node) {
|
||||
return node;
|
||||
}
|
||||
if (indexes.end != null) {
|
||||
const center = Math.floor(indexes.start + (indexes.end - indexes.start) / 2);
|
||||
return getWarningTargetNodeFromIndex(center);
|
||||
}
|
||||
}
|
||||
if (indexes.end != null) {
|
||||
return getWarningTargetNodeFromIndex(indexes.end);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Get warning target node from the given index
|
||||
*/
|
||||
function getWarningTargetNodeFromIndex(index) {
|
||||
let targetNode = sourceCode.getNodeByRangeIndex(index);
|
||||
while (targetNode) {
|
||||
if (targetNode.type === 'SvelteElement' || targetNode.type === 'SvelteStyleElement') {
|
||||
return targetNode;
|
||||
}
|
||||
if (targetNode.parent) {
|
||||
if (targetNode.parent.type === 'Program' ||
|
||||
targetNode.parent.type === 'SvelteScriptElement') {
|
||||
return targetNode;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
targetNode = targetNode.parent || null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/** Get warning index */
|
||||
function getWarningIndexes(warning) {
|
||||
const start = warning.start && sourceCode.getIndexFromLoc(warning.start);
|
||||
const end = warning.end && sourceCode.getIndexFromLoc(warning.end);
|
||||
return { start, end };
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Check if using TypeScript.
|
||||
*/
|
||||
function isUseTypeScript(context) {
|
||||
const sourceCode = context.sourceCode;
|
||||
if (sourceCode.parserServices.esTreeNodeToTSNodeMap)
|
||||
return true;
|
||||
const root = sourceCode.ast;
|
||||
for (const node of root.body) {
|
||||
if (node.type === 'SvelteScriptElement') {
|
||||
const lang = getLangValue(node)?.toLowerCase();
|
||||
if (lang === 'ts' || lang === 'typescript') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Check if using Babel.
|
||||
*/
|
||||
function isUseBabel(context) {
|
||||
const parser = context.parserOptions?.parser;
|
||||
if (!parser) {
|
||||
return false;
|
||||
}
|
||||
const sourceCode = context.sourceCode;
|
||||
const root = sourceCode.ast;
|
||||
let scriptLang = 'js';
|
||||
for (const node of root.body) {
|
||||
if (node.type === 'SvelteScriptElement') {
|
||||
const lang = getLangValue(node)?.toLowerCase();
|
||||
if (lang === 'ts' || lang === 'typescript') {
|
||||
scriptLang = lang;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
const parserName = getParserName(scriptLang, parser);
|
||||
if (!parserName) {
|
||||
return false;
|
||||
}
|
||||
if (parserName === '@babel/eslint-parser') {
|
||||
return true;
|
||||
}
|
||||
if (parserName.includes('@babel/eslint-parser')) {
|
||||
let targetPath = parserName;
|
||||
while (targetPath) {
|
||||
const pkgPath = path.join(targetPath, 'package.json');
|
||||
if (fs.existsSync(pkgPath)) {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))?.name === '@babel/eslint-parser';
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const parent = path.dirname(targetPath);
|
||||
if (targetPath === parent) {
|
||||
break;
|
||||
}
|
||||
targetPath = parent;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
/** Get script parser name */
|
||||
function getParserName(lang, parser) {
|
||||
if (typeof parser === 'string') {
|
||||
return parser;
|
||||
}
|
||||
else if (typeof parser === 'object') {
|
||||
const name = parser[lang];
|
||||
if (typeof name === 'string') {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
9
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/babel.d.ts
generated
vendored
Normal file
9
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/babel.d.ts
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../../types.js';
|
||||
import type { TransformResult } from './types.js';
|
||||
/**
|
||||
* Transpile with babel
|
||||
*/
|
||||
export declare function transform(node: AST.SvelteScriptElement, text: string, context: RuleContext): TransformResult | null;
|
||||
/** Check if project has Babel. */
|
||||
export declare function hasBabel(context: RuleContext): boolean;
|
||||
49
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/babel.js
generated
vendored
Normal file
49
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/babel.js
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
import { loadModule } from '../../../utils/load-module.js';
|
||||
/**
|
||||
* Transpile with babel
|
||||
*/
|
||||
export function transform(node, text, context) {
|
||||
const babel = loadBabel(context);
|
||||
if (!babel) {
|
||||
return null;
|
||||
}
|
||||
let inputRange;
|
||||
if (node.endTag) {
|
||||
inputRange = [node.startTag.range[1], node.endTag.range[0]];
|
||||
}
|
||||
else {
|
||||
inputRange = [node.startTag.range[1], node.range[1]];
|
||||
}
|
||||
const code = text.slice(...inputRange);
|
||||
try {
|
||||
const output = babel.transformSync(code, {
|
||||
sourceType: 'module',
|
||||
sourceMaps: true,
|
||||
minified: false,
|
||||
ast: false,
|
||||
code: true,
|
||||
cwd: context.cwd ?? process.cwd()
|
||||
});
|
||||
if (!output) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
inputRange,
|
||||
output: output.code,
|
||||
mappings: output.map.mappings
|
||||
};
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/** Check if project has Babel. */
|
||||
export function hasBabel(context) {
|
||||
return Boolean(loadBabel(context));
|
||||
}
|
||||
/**
|
||||
* Load babel
|
||||
*/
|
||||
function loadBabel(context) {
|
||||
return loadModule(context, '@babel/core');
|
||||
}
|
||||
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/less.d.ts
generated
vendored
Normal file
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/less.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../../types.js';
|
||||
import type { TransformResult } from './types.js';
|
||||
/**
|
||||
* Transpile with less
|
||||
*/
|
||||
export declare function transform(node: AST.SvelteStyleElement, text: string, context: RuleContext): TransformResult | null;
|
||||
47
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/less.js
generated
vendored
Normal file
47
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/less.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
import { loadModule } from '../../../utils/load-module.js';
|
||||
/**
|
||||
* Transpile with less
|
||||
*/
|
||||
export function transform(node, text, context) {
|
||||
const less = loadLess(context);
|
||||
if (!less) {
|
||||
return null;
|
||||
}
|
||||
let inputRange;
|
||||
if (node.endTag) {
|
||||
inputRange = [node.startTag.range[1], node.endTag.range[0]];
|
||||
}
|
||||
else {
|
||||
inputRange = [node.startTag.range[1], node.range[1]];
|
||||
}
|
||||
const code = text.slice(...inputRange);
|
||||
const filename = `${context.filename}.less`;
|
||||
try {
|
||||
let output;
|
||||
less.render(code, {
|
||||
sourceMap: {},
|
||||
syncImport: true,
|
||||
filename,
|
||||
lint: false
|
||||
}, (_error, result) => {
|
||||
output = result;
|
||||
});
|
||||
if (!output) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
inputRange,
|
||||
output: output.css,
|
||||
mappings: JSON.parse(output.map).mappings
|
||||
};
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Load less
|
||||
*/
|
||||
function loadLess(context) {
|
||||
return loadModule(context, 'less');
|
||||
}
|
||||
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/postcss.d.ts
generated
vendored
Normal file
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/postcss.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../../types.js';
|
||||
import type { TransformResult } from './types.js';
|
||||
/**
|
||||
* Transform with postcss
|
||||
*/
|
||||
export declare function transform(node: AST.SvelteStyleElement, text: string, context: RuleContext): TransformResult | null;
|
||||
42
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/postcss.js
generated
vendored
Normal file
42
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/postcss.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
import postcss from 'postcss';
|
||||
import postcssLoadConfig from 'postcss-load-config';
|
||||
/**
|
||||
* Transform with postcss
|
||||
*/
|
||||
export function transform(node, text, context) {
|
||||
const postcssConfig = context.settings?.svelte?.compileOptions?.postcss;
|
||||
if (postcssConfig === false) {
|
||||
return null;
|
||||
}
|
||||
let inputRange;
|
||||
if (node.endTag) {
|
||||
inputRange = [node.startTag.range[1], node.endTag.range[0]];
|
||||
}
|
||||
else {
|
||||
inputRange = [node.startTag.range[1], node.range[1]];
|
||||
}
|
||||
const code = text.slice(...inputRange);
|
||||
const filename = `${context.filename}.css`;
|
||||
try {
|
||||
const configFilePath = postcssConfig?.configFilePath;
|
||||
const config = postcssLoadConfig.sync({
|
||||
cwd: context.cwd ?? process.cwd(),
|
||||
from: filename
|
||||
}, typeof configFilePath === 'string' ? configFilePath : undefined);
|
||||
const result = postcss(config.plugins).process(code, {
|
||||
...config.options,
|
||||
map: {
|
||||
inline: false
|
||||
}
|
||||
});
|
||||
return {
|
||||
inputRange,
|
||||
output: result.content,
|
||||
mappings: result.map.toJSON().mappings
|
||||
};
|
||||
}
|
||||
catch {
|
||||
// console.log(e)
|
||||
return null;
|
||||
}
|
||||
}
|
||||
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/sass.d.ts
generated
vendored
Normal file
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/sass.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../../types.js';
|
||||
import type { TransformResult } from './types.js';
|
||||
/**
|
||||
* Transpile with sass
|
||||
*/
|
||||
export declare function transform(node: AST.SvelteStyleElement, text: string, context: RuleContext, type: 'scss' | 'sass'): TransformResult | null;
|
||||
41
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/sass.js
generated
vendored
Normal file
41
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/sass.js
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
import { loadModule } from '../../../utils/load-module.js';
|
||||
/**
|
||||
* Transpile with sass
|
||||
*/
|
||||
export function transform(node, text, context, type) {
|
||||
const sass = loadSass(context);
|
||||
if (!sass) {
|
||||
return null;
|
||||
}
|
||||
let inputRange;
|
||||
if (node.endTag) {
|
||||
inputRange = [node.startTag.range[1], node.endTag.range[0]];
|
||||
}
|
||||
else {
|
||||
inputRange = [node.startTag.range[1], node.range[1]];
|
||||
}
|
||||
const code = text.slice(...inputRange);
|
||||
try {
|
||||
const output = sass.compileString(code, {
|
||||
sourceMap: true,
|
||||
syntax: type === 'sass' ? 'indented' : undefined
|
||||
});
|
||||
if (!output) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
inputRange,
|
||||
output: output.css,
|
||||
mappings: output.sourceMap.mappings
|
||||
};
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Load sass
|
||||
*/
|
||||
function loadSass(context) {
|
||||
return loadModule(context, 'sass');
|
||||
}
|
||||
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/stylus.d.ts
generated
vendored
Normal file
7
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/stylus.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../../types.js';
|
||||
import type { TransformResult } from './types.js';
|
||||
/**
|
||||
* Transpile with stylus
|
||||
*/
|
||||
export declare function transform(node: AST.SvelteStyleElement, text: string, context: RuleContext): TransformResult | null;
|
||||
45
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/stylus.js
generated
vendored
Normal file
45
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/stylus.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
import { loadModule } from '../../../utils/load-module.js';
|
||||
/**
|
||||
* Transpile with stylus
|
||||
*/
|
||||
export function transform(node, text, context) {
|
||||
const stylus = loadStylus(context);
|
||||
if (!stylus) {
|
||||
return null;
|
||||
}
|
||||
let inputRange;
|
||||
if (node.endTag) {
|
||||
inputRange = [node.startTag.range[1], node.endTag.range[0]];
|
||||
}
|
||||
else {
|
||||
inputRange = [node.startTag.range[1], node.range[1]];
|
||||
}
|
||||
const code = text.slice(...inputRange);
|
||||
const filename = `${context.filename}.stylus`;
|
||||
try {
|
||||
let output;
|
||||
const style = stylus(code, {
|
||||
filename
|
||||
}).set('sourcemap', {});
|
||||
style.render((_error, code) => {
|
||||
output = code;
|
||||
});
|
||||
if (output == null) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
inputRange,
|
||||
output,
|
||||
mappings: style.sourcemap.mappings
|
||||
};
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Load stylus
|
||||
*/
|
||||
function loadStylus(context) {
|
||||
return loadModule(context, 'stylus');
|
||||
}
|
||||
6
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/types.d.ts
generated
vendored
Normal file
6
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/types.d.ts
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
export type TransformResult = {
|
||||
inputRange: AST.Range;
|
||||
output: string;
|
||||
mappings: string;
|
||||
};
|
||||
1
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/types.js
generated
vendored
Normal file
1
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/types.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
9
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/typescript.d.ts
generated
vendored
Normal file
9
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/typescript.d.ts
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { AST } from 'svelte-eslint-parser';
|
||||
import type { RuleContext } from '../../../types.js';
|
||||
import type { TransformResult } from './types.js';
|
||||
/**
|
||||
* Transpile with typescript
|
||||
*/
|
||||
export declare function transform(node: AST.SvelteScriptElement, text: string, context: RuleContext): TransformResult | null;
|
||||
/** Check if project has TypeScript. */
|
||||
export declare function hasTypeScript(context: RuleContext): boolean;
|
||||
50
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/typescript.js
generated
vendored
Normal file
50
node_modules/eslint-plugin-svelte/lib/shared/svelte-compile-warns/transform/typescript.js
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
import { loadModule } from '../../../utils/load-module.js';
|
||||
/**
|
||||
* Transpile with typescript
|
||||
*/
|
||||
export function transform(node, text, context) {
|
||||
const ts = loadTs(context);
|
||||
if (!ts) {
|
||||
return null;
|
||||
}
|
||||
let inputRange;
|
||||
if (node.endTag) {
|
||||
inputRange = [node.startTag.range[1], node.endTag.range[0]];
|
||||
}
|
||||
else {
|
||||
inputRange = [node.startTag.range[1], node.range[1]];
|
||||
}
|
||||
const code = text.slice(...inputRange);
|
||||
try {
|
||||
const output = ts.transpileModule(code, {
|
||||
reportDiagnostics: false,
|
||||
compilerOptions: {
|
||||
target: context.sourceCode.parserServices.program?.getCompilerOptions()?.target ||
|
||||
ts.ScriptTarget.ESNext,
|
||||
module: ts.ModuleKind.ESNext,
|
||||
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Preserve,
|
||||
preserveValueImports: true,
|
||||
verbatimModuleSyntax: true,
|
||||
sourceMap: true
|
||||
}
|
||||
});
|
||||
return {
|
||||
inputRange,
|
||||
output: output.outputText,
|
||||
mappings: JSON.parse(output.sourceMapText).mappings
|
||||
};
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/** Check if project has TypeScript. */
|
||||
export function hasTypeScript(context) {
|
||||
return Boolean(loadTs(context));
|
||||
}
|
||||
/**
|
||||
* Load typescript
|
||||
*/
|
||||
function loadTs(context) {
|
||||
return loadModule(context, 'typescript');
|
||||
}
|
||||
Reference in New Issue
Block a user