Skip to content

Fix false positives in no-unused-svelte-ignore #88

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 1 commit into from
Dec 24, 2021
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
9 changes: 7 additions & 2 deletions src/rules/no-unused-svelte-ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ export default createRule("no-unused-svelte-ignore", {
if (!ignoreComments.length) {
return {}
}
for (const warning of getSvelteCompileWarnings(context, {
const warnings = getSvelteCompileWarnings(context, {
warnings: "onlyWarnings",
removeComments: new Set(ignoreComments.map((i) => i.token)),
})) {
})
if (!warnings) {
return {}
}
for (const warning of warnings) {
if (!warning.code) {
continue
}
Expand Down
6 changes: 5 additions & 1 deletion src/rules/valid-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export default createRule("valid-compile", {

return {
"Program:exit"() {
report(getSvelteCompileWarnings(context, { ignoreWarnings }))
report(
getSvelteCompileWarnings(context, {
warnings: ignoreWarnings ? "ignoreWarnings" : null,
})!,
)
},
}
},
Expand Down
21 changes: 15 additions & 6 deletions src/shared/svelte-compile-warns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type Warning = {
message: string
} & Loc
export type GetSvelteWarningsOption = {
ignoreWarnings?: boolean
warnings: "ignoreWarnings" | "onlyWarnings" | null
removeComments?: Iterable<AST.Token | AST.Comment>
}

Expand All @@ -33,7 +33,7 @@ export type GetSvelteWarningsOption = {
export function getSvelteCompileWarnings(
context: RuleContext,
option: GetSvelteWarningsOption,
): Warning[] {
): Warning[] | null {
const sourceCode = context.getSourceCode()
const text = !option.removeComments
? sourceCode.text
Expand Down Expand Up @@ -259,10 +259,14 @@ export function getSvelteCompileWarnings(
}
}
}

const code = remapContext.postprocess()
const baseWarnings = getWarningsFromCode(code, option)
if (!baseWarnings) {
return null
}

const warnings: Warning[] = []
for (const warn of getWarningsFromCode(code, option)) {
for (const warn of baseWarnings) {
let loc: Loc | null = null

/** Get re-mapped location */
Expand Down Expand Up @@ -296,8 +300,10 @@ type TS = typeof typescript
*/
function getWarningsFromCode(
code: string,
{ ignoreWarnings }: { ignoreWarnings?: boolean },
): Warning[] {
{ warnings }: GetSvelteWarningsOption,
): Warning[] | null {
const ignoreWarnings = warnings === "ignoreWarnings"
const onlyWarnings = warnings === "onlyWarnings"
try {
const result = compiler.compile(code, {
generate: false,
Expand All @@ -309,6 +315,9 @@ function getWarningsFromCode(
return result.warnings as Warning[]
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
} catch (e: any) {
if (onlyWarnings) {
return null
}
// console.log(code)
if (!ignoreWarnings) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
let foo
</script>

<slot name="default" />

<!-- svelte-ignore a11y-autofocus -->
<img src="foo" />