Skip to content

fix: false positives for ts in svelte/no-unused-svelte-ignore #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>