diff --git a/files/empty.json b/files/empty.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/files/empty.json @@ -0,0 +1 @@ +{} diff --git a/lib/utils/get-auto-jsonc-rules-config.ts b/lib/utils/get-auto-jsonc-rules-config.ts index 14a14c53..ff9c175c 100644 --- a/lib/utils/get-auto-jsonc-rules-config.ts +++ b/lib/utils/get-auto-jsonc-rules-config.ts @@ -1,22 +1,57 @@ -import type { Linter, CLIEngine } from "eslint" +import type { Linter } from "eslint" import { existsSync, statSync } from "fs" -import { dirname } from "path" +import { dirname, resolve } from "path" import type { RuleModule } from "../types" -let engine: CLIEngine, ruleNames: Set +let configResolver: (filePath: string) => Linter.Config, ruleNames: Set /** - * Get CLIEngine instance + * Get config resolver */ -function getCLIEngine() { - if (engine) { - return engine +function getConfigResolver(): (filePath: string) => Linter.Config { + if (configResolver) { + return configResolver } + // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special - engine = new (require("eslint").CLIEngine)({}) - // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special - engine.addPlugin("eslint-plugin-jsonc", require("..")) - return engine + const plugin = require("..") + try { + // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special + const eslintrc = require("@eslint/eslintrc") + const configArrayFactory = + new eslintrc.Legacy.CascadingConfigArrayFactory({ + additionalPluginPool: new Map([ + ["eslint-plugin-jsonc", plugin], + ]), + eslintRecommendedPath: require.resolve( + "../../files/empty.json", + ), + eslintAllPath: require.resolve("../../files/empty.json"), + }) + + return (configResolver = (filePath: string) => { + const absolutePath = resolve(process.cwd(), filePath) + return configArrayFactory + .getConfigArrayForFile(absolutePath) + .extractConfig(absolutePath) + .toCompatibleObjectAsConfigFileContent() + }) + } catch { + // ignore + } + try { + // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special + const eslint = require("eslint") + const engine = new eslint.CLIEngine({}) + engine.addPlugin("eslint-plugin-jsonc", plugin) + return (configResolver = (filePath) => { + return engine.getConfigForFile(filePath) + }) + } catch { + // ignore + } + + return () => ({}) } /** @@ -47,7 +82,7 @@ function getConfig(filename: string): Linter.Config { // eslint-disable-next-line no-param-reassign -- ignore filename = dir } - return getCLIEngine().getConfigForFile(filename) + return getConfigResolver()(filename) } /** diff --git a/package.json b/package.json index 758987db..e6cb88b2 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "types": "index.d.ts", "files": [ "dist", - "index.d.ts" + "index.d.ts", + "files" ], "scripts": { "prebuild": "npm run -s clean", diff --git a/tests/fixtures/auto/test01/.eslintrc.json b/tests/fixtures/auto/test01/.eslintrc.json index bf0629f1..31eb6a67 100644 --- a/tests/fixtures/auto/test01/.eslintrc.json +++ b/tests/fixtures/auto/test01/.eslintrc.json @@ -1,5 +1,6 @@ { "root": true, + "extends": ["eslint:recommended"], "rules": { "array-bracket-newline": "error", "array-bracket-spacing": "error",