Repository: ota-meshi/eslint-plugin-svelte
Pull request #202 (2022-07-29 02:13:20)

Fix false positives for files other than `*.svelte` in `svelte/no-unused-svelte-ignore` rule
This PR fixes a false positive in the `svelte/no-unused-svelte-ignore` rule for JS files.
The rule should not check for files other than those parsed by svelte-eslint-parser.

I also changed the test utility so that it can target fixtures other than .svelte.




Patch:
diff --git a/src/rules/no-unused-svelte-ignore.ts b/src/rules/no-unused-svelte-ignore.ts
index 65e5d2b..2d8c66e 100644
--- a/src/rules/no-unused-svelte-ignore.ts
+++ b/src/rules/no-unused-svelte-ignore.ts
@@ -19,6 +19,9 @@ export default createRule("no-unused-svelte-ignore", {
   },
 
   create(context) {
+    if (!context.parserServices.isSvelte) {
+      return {}
+    }
     const sourceCode = context.getSourceCode()
 
     const ignoreComments: IgnoreItem[] = []


Test patch:
diff --git a/tests/fixtures/rules/no-unused-svelte-ignore/valid/ignore-js-input.js b/tests/fixtures/rules/no-unused-svelte-ignore/valid/ignore-js-input.js
new file mode 100644
index 0000000..ae6e570
--- /dev/null
+++ b/tests/fixtures/rules/no-unused-svelte-ignore/valid/ignore-js-input.js
@@ -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" />
diff --git a/tests/utils/utils.ts b/tests/utils/utils.ts
index ebdf917..56b7069 100644
--- a/tests/utils/utils.ts
+++ b/tests/utils/utils.ts
@@ -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[]
@@ -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")
@@ -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)
@@ -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)
 
@@ -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 })
 }