Skip to content
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

Replace ESLint CLIEngine with @eslint/eslintrc #100

Merged
merged 1 commit into from
Jun 13, 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
1 change: 1 addition & 0 deletions files/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
59 changes: 47 additions & 12 deletions lib/utils/get-auto-jsonc-rules-config.ts
Original file line number Diff line number Diff line change
@@ -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<string>
let configResolver: (filePath: string) => Linter.Config, ruleNames: Set<string>

/**
* 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 () => ({})
}

/**
Expand Down Expand Up @@ -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)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"types": "index.d.ts",
"files": [
"dist",
"index.d.ts"
"index.d.ts",
"files"
],
"scripts": {
"prebuild": "npm run -s clean",
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/auto/test01/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"root": true,
"extends": ["eslint:recommended"],
"rules": {
"array-bracket-newline": "error",
"array-bracket-spacing": "error",
Expand Down