diff --git a/src/rules/no-unused-svelte-ignore.ts b/src/rules/no-unused-svelte-ignore.ts index afc02c08d..55284ade3 100644 --- a/src/rules/no-unused-svelte-ignore.ts +++ b/src/rules/no-unused-svelte-ignore.ts @@ -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 } diff --git a/src/rules/valid-compile.ts b/src/rules/valid-compile.ts index 24d7ce49d..9ac3322cc 100644 --- a/src/rules/valid-compile.ts +++ b/src/rules/valid-compile.ts @@ -49,7 +49,11 @@ export default createRule("valid-compile", { return { "Program:exit"() { - report(getSvelteCompileWarnings(context, { ignoreWarnings })) + report( + getSvelteCompileWarnings(context, { + warnings: ignoreWarnings ? "ignoreWarnings" : null, + })!, + ) }, } }, diff --git a/src/shared/svelte-compile-warns.ts b/src/shared/svelte-compile-warns.ts index 4e641859c..e351de1ea 100644 --- a/src/shared/svelte-compile-warns.ts +++ b/src/shared/svelte-compile-warns.ts @@ -23,7 +23,7 @@ export type Warning = { message: string } & Loc export type GetSvelteWarningsOption = { - ignoreWarnings?: boolean + warnings: "ignoreWarnings" | "onlyWarnings" | null removeComments?: Iterable } @@ -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 @@ -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 */ @@ -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, @@ -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 { diff --git a/tests/fixtures/rules/no-unused-svelte-ignore/valid/has-error-input.svelte b/tests/fixtures/rules/no-unused-svelte-ignore/valid/has-error-input.svelte new file mode 100644 index 000000000..9db2a4149 --- /dev/null +++ b/tests/fixtures/rules/no-unused-svelte-ignore/valid/has-error-input.svelte @@ -0,0 +1,8 @@ + + + + + +