Skip to content

Commit

Permalink
fix: false positives for ts in svelte/no-unused-svelte-ignore (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Feb 6, 2023
1 parent 26870cf commit a561f99
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-garlics-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

fix: false positives for ts in `svelte/no-unused-svelte-ignore`
16 changes: 10 additions & 6 deletions src/shared/svelte-compile-warns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion src/shared/svelte-compile-warns/transform/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type BabelCore = typeof babelCore
*/
export function transform(
node: AST.SvelteScriptElement,
text: string,
context: RuleContext,
): TransformResult | null {
const babel = loadBabel(context)
Expand All @@ -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, {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/svelte-compile-warns/transform/less.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Less = typeof less
*/
export function transform(
node: AST.SvelteStyleElement,
text: string,
context: RuleContext,
): TransformResult | null {
const less = loadLess(context)
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/svelte-compile-warns/transform/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/svelte-compile-warns/transform/sass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Sass = typeof sass
*/
export function transform(
node: AST.SvelteStyleElement,
text: string,
context: RuleContext,
type: "scss" | "sass",
): TransformResult | null {
Expand All @@ -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, {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/svelte-compile-warns/transform/stylus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Stylus = typeof stylus
*/
export function transform(
node: AST.SvelteStyleElement,
text: string,
context: RuleContext,
): TransformResult | null {
const stylus = loadStylus(context)
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/svelte-compile-warns/transform/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type TS = typeof typescript
*/
export function transform(
node: AST.SvelteScriptElement,
text: string,
context: RuleContext,
): TransformResult | null {
const ts = loadTs(context)
Expand All @@ -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, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script lang="ts">
// svelte-ignore unused-export-let
export let something
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script lang="ts">
// svelte-ignore unused-export-let
export let something
</script>

0 comments on commit a561f99

Please sign in to comment.