/
get-auto-jsonc-rules-config.ts
147 lines (134 loc) · 4.85 KB
/
get-auto-jsonc-rules-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Linter } from "eslint"
import { existsSync, statSync } from "fs"
import { dirname, extname, resolve } from "path"
import type { RuleModule } from "../types"
let configResolver: (filePath: string) => Linter.Config, ruleNames: Set<string>
/**
* Get config resolver
*/
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
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],
]),
getEslintRecommendedConfig() {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
return require("../../conf/eslint-recommended.js")
},
getEslintAllConfig() {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
return require("../../conf/eslint-all.js")
},
// for v1.1.0
eslintRecommendedPath: require.resolve(
"../../conf/eslint-recommended.js",
),
eslintAllPath: require.resolve("../../conf/eslint-all.js"),
})
return (configResolver = (filePath: string) => {
const absolutePath = resolve(process.cwd(), filePath)
return configArrayFactory
.getConfigArrayForFile(absolutePath)
.extractConfig(absolutePath)
.toCompatibleObjectAsConfigFileContent()
})
} catch {
// ignore
}
try {
// For ESLint v6
// 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) => {
// Adjust the file name to avoid a crash.
// https://github.com/ota-meshi/eslint-plugin-jsonc/issues/28
let targetFilePath = filePath
const ext = extname(filePath)
while (!isValidFilename(targetFilePath)) {
const dir = dirname(targetFilePath)
if (dir === targetFilePath) {
return {}
}
targetFilePath = dir
if (ext && extname(targetFilePath) !== ext) {
targetFilePath += ext
}
}
return engine.getConfigForFile(targetFilePath)
})
} catch {
// ignore
}
return () => ({})
}
/**
* Checks if the given file name can get the configuration (for ESLint v6).
*/
function isValidFilename(filename: string) {
const dir = dirname(filename)
if (existsSync(dir) && statSync(dir).isDirectory()) {
if (existsSync(filename) && statSync(filename).isDirectory()) {
return false
}
return true
}
return false
}
/**
* Get config for the given filename
* @param filename
*/
function getConfig(filename: string): Linter.Config {
return getConfigResolver()(filename)
}
/**
* Get jsonc rule from the given base rule name
* @param filename
*/
function getJsoncRule(rule: string) {
ruleNames =
ruleNames ||
new Set(
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special
(require("./rules").rules as RuleModule[]).map(
(r) => r.meta.docs.ruleName,
),
)
return ruleNames.has(rule) ? `jsonc/${rule}` : null
}
/**
* Get additional jsonc rules config from fileName
* @param filename
*/
export function getAutoConfig(filename: string): {
[name: string]: Linter.RuleEntry
} {
const autoConfig: { [name: string]: Linter.RuleEntry } = {}
const config = getConfig(filename)
if (config.rules) {
for (const ruleName of Object.keys(config.rules)) {
const jsoncName = getJsoncRule(ruleName)
if (jsoncName && !config.rules[jsoncName]) {
const entry = config.rules[ruleName]
if (entry) {
const severity = Array.isArray(entry) ? entry[0] : entry
if (severity !== "off" && severity !== 0) {
autoConfig[jsoncName] = entry
}
}
}
}
}
return autoConfig
}