Skip to content

Commit a561f99

Browse files
authored
fix: false positives for ts in svelte/no-unused-svelte-ignore (#357)
1 parent 26870cf commit a561f99

File tree

10 files changed

+35
-12
lines changed

10 files changed

+35
-12
lines changed

.changeset/honest-garlics-march.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": patch
3+
---
4+
5+
fix: false positives for ts in `svelte/no-unused-svelte-ignore`

src/shared/svelte-compile-warns/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const STYLE_TRANSFORMS: Record<
2727
> = {
2828
postcss: transformWithPostCSS,
2929
pcss: transformWithPostCSS,
30-
scss: (node, context) => transformWithSass(node, context, "scss"),
31-
sass: (node, context) => transformWithSass(node, context, "sass"),
30+
scss: (node, text, context) => transformWithSass(node, text, context, "scss"),
31+
sass: (node, text, context) => transformWithSass(node, text, context, "sass"),
3232
less: transformWithLess,
3333
stylus: transformWithStylus,
3434
styl: transformWithStylus,
@@ -94,7 +94,11 @@ function getSvelteCompileWarningsWithoutCache(
9494
for (const style of styleElementsWithNotCSS) {
9595
const transform = STYLE_TRANSFORMS[style.lang]
9696
if (transform) {
97-
const result = transform(style.node, context)
97+
const result = transform(
98+
style.node,
99+
context.getSourceCode().text,
100+
context,
101+
)
98102
if (result) {
99103
transformResults.push(result)
100104
continue
@@ -110,7 +114,7 @@ function getSvelteCompileWarningsWithoutCache(
110114

111115
const text = buildStrippedText(context, ignoreComments, stripStyleTokens)
112116

113-
transformResults.push(...transformScripts(context))
117+
transformResults.push(...transformScripts(context, text))
114118

115119
if (!transformResults.length) {
116120
const warnings = getWarningsFromCode(text)
@@ -396,7 +400,7 @@ function buildStrippedText(
396400
}
397401

398402
/** Returns the result of transforming the required script for the transform. */
399-
function* transformScripts(context: RuleContext) {
403+
function* transformScripts(context: RuleContext, text: string) {
400404
const transform = isUseTypeScript(context)
401405
? hasTypeScript(context)
402406
? transformWithTypescript
@@ -410,7 +414,7 @@ function* transformScripts(context: RuleContext) {
410414
const root = sourceCode.ast
411415
for (const node of root.body) {
412416
if (node.type === "SvelteScriptElement") {
413-
const result = transform(node, context)
417+
const result = transform(node, text, context)
414418
if (result) {
415419
yield result
416420
}

src/shared/svelte-compile-warns/transform/babel.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type BabelCore = typeof babelCore
1010
*/
1111
export function transform(
1212
node: AST.SvelteScriptElement,
13+
text: string,
1314
context: RuleContext,
1415
): TransformResult | null {
1516
const babel = loadBabel(context)
@@ -22,7 +23,7 @@ export function transform(
2223
} else {
2324
inputRange = [node.startTag.range[1], node.range[1]]
2425
}
25-
const code = context.getSourceCode().text.slice(...inputRange)
26+
const code = text.slice(...inputRange)
2627

2728
try {
2829
const output = babel.transformSync(code, {

src/shared/svelte-compile-warns/transform/less.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Less = typeof less
1010
*/
1111
export function transform(
1212
node: AST.SvelteStyleElement,
13+
text: string,
1314
context: RuleContext,
1415
): TransformResult | null {
1516
const less = loadLess(context)
@@ -22,7 +23,7 @@ export function transform(
2223
} else {
2324
inputRange = [node.startTag.range[1], node.range[1]]
2425
}
25-
const code = context.getSourceCode().text.slice(...inputRange)
26+
const code = text.slice(...inputRange)
2627

2728
const filename = `${context.getFilename()}.less`
2829
try {

src/shared/svelte-compile-warns/transform/postcss.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { TransformResult } from "./types"
99
*/
1010
export function transform(
1111
node: AST.SvelteStyleElement,
12+
text: string,
1213
context: RuleContext,
1314
): TransformResult | null {
1415
const postcssConfig = context.settings?.svelte?.compileOptions?.postcss
@@ -21,7 +22,7 @@ export function transform(
2122
} else {
2223
inputRange = [node.startTag.range[1], node.range[1]]
2324
}
24-
const code = context.getSourceCode().text.slice(...inputRange)
25+
const code = text.slice(...inputRange)
2526

2627
const filename = `${context.getFilename()}.css`
2728
try {

src/shared/svelte-compile-warns/transform/sass.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Sass = typeof sass
1010
*/
1111
export function transform(
1212
node: AST.SvelteStyleElement,
13+
text: string,
1314
context: RuleContext,
1415
type: "scss" | "sass",
1516
): TransformResult | null {
@@ -23,7 +24,7 @@ export function transform(
2324
} else {
2425
inputRange = [node.startTag.range[1], node.range[1]]
2526
}
26-
const code = context.getSourceCode().text.slice(...inputRange)
27+
const code = text.slice(...inputRange)
2728

2829
try {
2930
const output = sass.compileString(code, {

src/shared/svelte-compile-warns/transform/stylus.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Stylus = typeof stylus
1111
*/
1212
export function transform(
1313
node: AST.SvelteStyleElement,
14+
text: string,
1415
context: RuleContext,
1516
): TransformResult | null {
1617
const stylus = loadStylus(context)
@@ -23,7 +24,7 @@ export function transform(
2324
} else {
2425
inputRange = [node.startTag.range[1], node.range[1]]
2526
}
26-
const code = context.getSourceCode().text.slice(...inputRange)
27+
const code = text.slice(...inputRange)
2728

2829
const filename = `${context.getFilename()}.stylus`
2930
try {

src/shared/svelte-compile-warns/transform/typescript.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type TS = typeof typescript
1010
*/
1111
export function transform(
1212
node: AST.SvelteScriptElement,
13+
text: string,
1314
context: RuleContext,
1415
): TransformResult | null {
1516
const ts = loadTs(context)
@@ -22,7 +23,7 @@ export function transform(
2223
} else {
2324
inputRange = [node.startTag.range[1], node.range[1]]
2425
}
25-
const code = context.getSourceCode().text.slice(...inputRange)
26+
const code = text.slice(...inputRange)
2627

2728
try {
2829
const output = ts.transpileModule(code, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script lang="ts">
2+
// svelte-ignore unused-export-let
3+
export let something
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script lang="ts">
2+
// svelte-ignore unused-export-let
3+
export let something
4+
</script>

0 commit comments

Comments
 (0)