From 6d5611a2f1f501cc9fae56b034a78f2112194342 Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Fri, 3 Feb 2023 23:10:30 +0900 Subject: [PATCH 1/2] fix: false positives for ts in `svelte/no-unused-svelte-ignore` --- src/shared/svelte-compile-warns/index.ts | 16 ++++++++++------ .../svelte-compile-warns/transform/babel.ts | 3 ++- .../svelte-compile-warns/transform/less.ts | 3 ++- .../svelte-compile-warns/transform/postcss.ts | 3 ++- .../svelte-compile-warns/transform/sass.ts | 3 ++- .../svelte-compile-warns/transform/stylus.ts | 3 ++- .../svelte-compile-warns/transform/typescript.ts | 3 ++- .../valid/ts-lang01-input.svelte | 4 ++++ .../valid/ts/ts-lang01-input.svelte | 4 ++++ 9 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-input.svelte create mode 100644 tests/fixtures/rules/valid-compile/valid/ts/ts-lang01-input.svelte diff --git a/src/shared/svelte-compile-warns/index.ts b/src/shared/svelte-compile-warns/index.ts index 74d4b389a..9c8e053a5 100644 --- a/src/shared/svelte-compile-warns/index.ts +++ b/src/shared/svelte-compile-warns/index.ts @@ -27,8 +27,8 @@ const STYLE_TRANSFORMS: Record< > = { postcss: transformWithPostCSS, pcss: transformWithPostCSS, - scss: (node, context) => transformWithSass(node, context, "scss"), - sass: (node, context) => transformWithSass(node, context, "sass"), + scss: (node, text, context) => transformWithSass(node, text, context, "scss"), + sass: (node, text, context) => transformWithSass(node, text, context, "sass"), less: transformWithLess, stylus: transformWithStylus, styl: transformWithStylus, @@ -94,7 +94,11 @@ function getSvelteCompileWarningsWithoutCache( for (const style of styleElementsWithNotCSS) { const transform = STYLE_TRANSFORMS[style.lang] if (transform) { - const result = transform(style.node, context) + const result = transform( + style.node, + context.getSourceCode().text, + context, + ) if (result) { transformResults.push(result) continue @@ -110,7 +114,7 @@ function getSvelteCompileWarningsWithoutCache( const text = buildStrippedText(context, ignoreComments, stripStyleTokens) - transformResults.push(...transformScripts(context)) + transformResults.push(...transformScripts(context, text)) if (!transformResults.length) { const warnings = getWarningsFromCode(text) @@ -396,7 +400,7 @@ function buildStrippedText( } /** Returns the result of transforming the required script for the transform. */ -function* transformScripts(context: RuleContext) { +function* transformScripts(context: RuleContext, text: string) { const transform = isUseTypeScript(context) ? hasTypeScript(context) ? transformWithTypescript @@ -410,7 +414,7 @@ function* transformScripts(context: RuleContext) { const root = sourceCode.ast for (const node of root.body) { if (node.type === "SvelteScriptElement") { - const result = transform(node, context) + const result = transform(node, text, context) if (result) { yield result } diff --git a/src/shared/svelte-compile-warns/transform/babel.ts b/src/shared/svelte-compile-warns/transform/babel.ts index 626e965ba..ea6d717bb 100644 --- a/src/shared/svelte-compile-warns/transform/babel.ts +++ b/src/shared/svelte-compile-warns/transform/babel.ts @@ -10,6 +10,7 @@ type BabelCore = typeof babelCore */ export function transform( node: AST.SvelteScriptElement, + text: string, context: RuleContext, ): TransformResult | null { const babel = loadBabel(context) @@ -22,7 +23,7 @@ export function transform( } else { inputRange = [node.startTag.range[1], node.range[1]] } - const code = context.getSourceCode().text.slice(...inputRange) + const code = text.slice(...inputRange) try { const output = babel.transformSync(code, { diff --git a/src/shared/svelte-compile-warns/transform/less.ts b/src/shared/svelte-compile-warns/transform/less.ts index 5f1e458ec..244dcd812 100644 --- a/src/shared/svelte-compile-warns/transform/less.ts +++ b/src/shared/svelte-compile-warns/transform/less.ts @@ -10,6 +10,7 @@ type Less = typeof less */ export function transform( node: AST.SvelteStyleElement, + text: string, context: RuleContext, ): TransformResult | null { const less = loadLess(context) @@ -22,7 +23,7 @@ export function transform( } else { inputRange = [node.startTag.range[1], node.range[1]] } - const code = context.getSourceCode().text.slice(...inputRange) + const code = text.slice(...inputRange) const filename = `${context.getFilename()}.less` try { diff --git a/src/shared/svelte-compile-warns/transform/postcss.ts b/src/shared/svelte-compile-warns/transform/postcss.ts index 5e0cc8564..50e8755b6 100644 --- a/src/shared/svelte-compile-warns/transform/postcss.ts +++ b/src/shared/svelte-compile-warns/transform/postcss.ts @@ -9,6 +9,7 @@ import type { TransformResult } from "./types" */ export function transform( node: AST.SvelteStyleElement, + text: string, context: RuleContext, ): TransformResult | null { const postcssConfig = context.settings?.svelte?.compileOptions?.postcss @@ -21,7 +22,7 @@ export function transform( } else { inputRange = [node.startTag.range[1], node.range[1]] } - const code = context.getSourceCode().text.slice(...inputRange) + const code = text.slice(...inputRange) const filename = `${context.getFilename()}.css` try { diff --git a/src/shared/svelte-compile-warns/transform/sass.ts b/src/shared/svelte-compile-warns/transform/sass.ts index 927cfa924..b987e4a74 100644 --- a/src/shared/svelte-compile-warns/transform/sass.ts +++ b/src/shared/svelte-compile-warns/transform/sass.ts @@ -10,6 +10,7 @@ type Sass = typeof sass */ export function transform( node: AST.SvelteStyleElement, + text: string, context: RuleContext, type: "scss" | "sass", ): TransformResult | null { @@ -23,7 +24,7 @@ export function transform( } else { inputRange = [node.startTag.range[1], node.range[1]] } - const code = context.getSourceCode().text.slice(...inputRange) + const code = text.slice(...inputRange) try { const output = sass.compileString(code, { diff --git a/src/shared/svelte-compile-warns/transform/stylus.ts b/src/shared/svelte-compile-warns/transform/stylus.ts index 4783d745b..400411205 100644 --- a/src/shared/svelte-compile-warns/transform/stylus.ts +++ b/src/shared/svelte-compile-warns/transform/stylus.ts @@ -11,6 +11,7 @@ type Stylus = typeof stylus */ export function transform( node: AST.SvelteStyleElement, + text: string, context: RuleContext, ): TransformResult | null { const stylus = loadStylus(context) @@ -23,7 +24,7 @@ export function transform( } else { inputRange = [node.startTag.range[1], node.range[1]] } - const code = context.getSourceCode().text.slice(...inputRange) + const code = text.slice(...inputRange) const filename = `${context.getFilename()}.stylus` try { diff --git a/src/shared/svelte-compile-warns/transform/typescript.ts b/src/shared/svelte-compile-warns/transform/typescript.ts index 6f3618961..56c2c1f52 100644 --- a/src/shared/svelte-compile-warns/transform/typescript.ts +++ b/src/shared/svelte-compile-warns/transform/typescript.ts @@ -10,6 +10,7 @@ type TS = typeof typescript */ export function transform( node: AST.SvelteScriptElement, + text: string, context: RuleContext, ): TransformResult | null { const ts = loadTs(context) @@ -22,7 +23,7 @@ export function transform( } else { inputRange = [node.startTag.range[1], node.range[1]] } - const code = context.getSourceCode().text.slice(...inputRange) + const code = text.slice(...inputRange) try { const output = ts.transpileModule(code, { diff --git a/tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-input.svelte b/tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-input.svelte new file mode 100644 index 000000000..34f0da9af --- /dev/null +++ b/tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-input.svelte @@ -0,0 +1,4 @@ + diff --git a/tests/fixtures/rules/valid-compile/valid/ts/ts-lang01-input.svelte b/tests/fixtures/rules/valid-compile/valid/ts/ts-lang01-input.svelte new file mode 100644 index 000000000..34f0da9af --- /dev/null +++ b/tests/fixtures/rules/valid-compile/valid/ts/ts-lang01-input.svelte @@ -0,0 +1,4 @@ + From 04284f1f88271cff786714809cab5ccb5a509a61 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Fri, 3 Feb 2023 23:12:15 +0900 Subject: [PATCH 2/2] Create honest-garlics-march.md --- .changeset/honest-garlics-march.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/honest-garlics-march.md diff --git a/.changeset/honest-garlics-march.md b/.changeset/honest-garlics-march.md new file mode 100644 index 000000000..117f0de09 --- /dev/null +++ b/.changeset/honest-garlics-march.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-svelte": patch +--- + +fix: false positives for ts in `svelte/no-unused-svelte-ignore`