Skip to content

Fix false positives for files other than *.svelte in svelte/no-unused-svelte-ignore rule #202

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
Jul 29, 2022
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
3 changes: 3 additions & 0 deletions src/rules/no-unused-svelte-ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default createRule("no-unused-svelte-ignore", {
},

create(context) {
if (!context.parserServices.isSvelte) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return {}
}
const sourceCode = context.getSourceCode()

const ignoreComments: IgnoreItem[] = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// <!-- svelte-ignore a11y-autofocus a11y-missing-attribute -->
// <img src="foo" alt="Foo" />
/* svelte-ignore a11y-autofocus a11y-missing-attribute */
// <img src="foo" />
32 changes: 13 additions & 19 deletions tests/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function loadTestCases(
valid?: (RuleTester.ValidTestCase | string)[]
invalid?: RuleTester.InvalidTestCase[]
}
filter: (file: string) => boolean
filter?: (file: string) => boolean
},
): {
valid: RuleTester.ValidTestCase[]
Expand All @@ -88,8 +88,8 @@ export function loadTestCases(
.filter(filter)
.map((inputFile) => {
const config = getConfig(ruleName, inputFile)
const errorFile = inputFile.replace(/input\.svelte$/u, "errors.json")
const outputFile = inputFile.replace(/input\.svelte$/u, "output.svelte")
const errorFile = inputFile.replace(/input\.[a-z]+$/u, "errors.json")
const outputFile = inputFile.replace(/input\.[a-z]+$/u, "output.svelte")
let errors
try {
errors = fs.readFileSync(errorFile, "utf8")
Expand Down Expand Up @@ -147,7 +147,7 @@ function* itrListupInput(rootDir: string): IterableIterator<string> {
continue
}
const abs = path.join(rootDir, filename)
if (filename.endsWith("input.svelte")) {
if (path.basename(filename, path.extname(filename)).endsWith("input")) {
yield abs
} else if (fs.statSync(abs).isDirectory()) {
yield* itrListupInput(abs)
Expand All @@ -161,8 +161,8 @@ function writeFixtures(
{ force }: { force?: boolean } = {},
) {
const linter = getLinter(ruleName)
const errorFile = inputFile.replace(/input\.svelte$/u, "errors.json")
const outputFile = inputFile.replace(/input\.svelte$/u, "output.svelte")
const errorFile = inputFile.replace(/input\.[a-z]+$/u, "errors.json")
const outputFile = inputFile.replace(/input\.[a-z]+$/u, "output.svelte")

const config = getConfig(ruleName, inputFile)

Expand Down Expand Up @@ -222,23 +222,17 @@ function getConfig(ruleName: string, inputFile: string) {
const filename = inputFile.slice(inputFile.indexOf(ruleName))
const code = fs.readFileSync(inputFile, "utf8")
let config
let configFile: string = inputFile.replace(/input\.svelte$/u, "config.json")
let configFile: string = inputFile.replace(/input\.[a-z]+$/u, "config.json")
if (!fs.existsSync(configFile)) {
configFile = path.join(path.dirname(inputFile), "_config.json")
}
if (fs.existsSync(configFile)) {
config = JSON.parse(fs.readFileSync(configFile, "utf8"))
}
if (config && typeof config === "object") {
return Object.assign(
{ parser: require.resolve("svelte-eslint-parser") },
config,
{ code, filename },
)
}
// default
return Object.assign(
{ parser: require.resolve("svelte-eslint-parser") },
{ code, filename },
)
const parser =
path.extname(filename) === ".svelte"
? require.resolve("svelte-eslint-parser")
: undefined

return Object.assign({ parser }, config, { code, filename })
}