Skip to content

Commit a192f35

Browse files
authored
Fix false positives in no-unused-svelte-ignore (#88)
1 parent 2401f36 commit a192f35

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/rules/no-unused-svelte-ignore.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,14 @@ export default createRule("no-unused-svelte-ignore", {
113113
if (!ignoreComments.length) {
114114
return {}
115115
}
116-
for (const warning of getSvelteCompileWarnings(context, {
116+
const warnings = getSvelteCompileWarnings(context, {
117+
warnings: "onlyWarnings",
117118
removeComments: new Set(ignoreComments.map((i) => i.token)),
118-
})) {
119+
})
120+
if (!warnings) {
121+
return {}
122+
}
123+
for (const warning of warnings) {
119124
if (!warning.code) {
120125
continue
121126
}

src/rules/valid-compile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export default createRule("valid-compile", {
4949

5050
return {
5151
"Program:exit"() {
52-
report(getSvelteCompileWarnings(context, { ignoreWarnings }))
52+
report(
53+
getSvelteCompileWarnings(context, {
54+
warnings: ignoreWarnings ? "ignoreWarnings" : null,
55+
})!,
56+
)
5357
},
5458
}
5559
},

src/shared/svelte-compile-warns.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type Warning = {
2323
message: string
2424
} & Loc
2525
export type GetSvelteWarningsOption = {
26-
ignoreWarnings?: boolean
26+
warnings: "ignoreWarnings" | "onlyWarnings" | null
2727
removeComments?: Iterable<AST.Token | AST.Comment>
2828
}
2929

@@ -33,7 +33,7 @@ export type GetSvelteWarningsOption = {
3333
export function getSvelteCompileWarnings(
3434
context: RuleContext,
3535
option: GetSvelteWarningsOption,
36-
): Warning[] {
36+
): Warning[] | null {
3737
const sourceCode = context.getSourceCode()
3838
const text = !option.removeComments
3939
? sourceCode.text
@@ -259,10 +259,14 @@ export function getSvelteCompileWarnings(
259259
}
260260
}
261261
}
262-
263262
const code = remapContext.postprocess()
263+
const baseWarnings = getWarningsFromCode(code, option)
264+
if (!baseWarnings) {
265+
return null
266+
}
267+
264268
const warnings: Warning[] = []
265-
for (const warn of getWarningsFromCode(code, option)) {
269+
for (const warn of baseWarnings) {
266270
let loc: Loc | null = null
267271

268272
/** Get re-mapped location */
@@ -296,8 +300,10 @@ type TS = typeof typescript
296300
*/
297301
function getWarningsFromCode(
298302
code: string,
299-
{ ignoreWarnings }: { ignoreWarnings?: boolean },
300-
): Warning[] {
303+
{ warnings }: GetSvelteWarningsOption,
304+
): Warning[] | null {
305+
const ignoreWarnings = warnings === "ignoreWarnings"
306+
const onlyWarnings = warnings === "onlyWarnings"
301307
try {
302308
const result = compiler.compile(code, {
303309
generate: false,
@@ -309,6 +315,9 @@ function getWarningsFromCode(
309315
return result.warnings as Warning[]
310316
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
311317
} catch (e: any) {
318+
if (onlyWarnings) {
319+
return null
320+
}
312321
// console.log(code)
313322
if (!ignoreWarnings) {
314323
try {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
let foo
3+
</script>
4+
5+
<slot name="default" />
6+
7+
<!-- svelte-ignore a11y-autofocus -->
8+
<img src="foo" />

0 commit comments

Comments
 (0)