-
-
Notifications
You must be signed in to change notification settings - Fork 482
/
oxlint-rules.cjs
106 lines (87 loc) · 2.84 KB
/
oxlint-rules.cjs
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
const { resolve } = require("node:path");
const { readFile } = require("node:fs/promises");
const readAllImplementedRuleNames = async () => {
const rulesFile = await readFile(
resolve("crates/oxc_linter/src/rules.rs"),
"utf8",
);
/** @type {Set<string>} */
const rules = new Set();
let found = false;
for (let line of rulesFile.split("\n")) {
line = line.trim();
// Skip commented out rules
if (line.startsWith("//")) continue;
if (line === "oxc_macros::declare_all_lint_rules! {") {
found = true;
continue;
}
if (found && line === "}") {
return rules;
}
if (found) {
const prefixedName = line
.replaceAll(",", "")
.replaceAll("::", "/")
.replaceAll("_", "-");
// Ignore no reference rules
if (prefixedName.startsWith("oxc/")) continue;
if (prefixedName.startsWith("deepscan/")) continue;
rules.add(prefixedName);
}
}
throw new Error("Failed to find the end of the rules list");
};
const NOT_SUPPORTED_RULE_NAMES = new Set([
"eslint/no-dupe-args", // superseded by strict mode
"eslint/no-octal", // superseded by strict mode
"eslint/no-with", // superseded by strict mode
"import/no-unresolved" // Will always contain false positives due to module resolution complexity
]);
/**
* @typedef {{
* docsUrl: string,
* isDeprecated: boolean,
* isRecommended: boolean,
* isImplemented: boolean,
* isNotSupported: boolean,
* }} RuleEntry
* @typedef {Map<string, RuleEntry>} RuleEntries
*/
/** @param {ReturnType<import("eslint").Linter["getRules"]>} loadedAllRules */
exports.createRuleEntries = (loadedAllRules) => {
/** @type {RuleEntries} */
const rulesEntry = new Map();
for (const [name, rule] of loadedAllRules) {
// Default eslint rules are not prefixed
const prefixedName = name.includes("/") ? name : `eslint/${name}`;
const docsUrl = rule.meta?.docs?.url ?? "";
const isDeprecated = rule.meta?.deprecated ?? false;
const isRecommended = rule.meta?.docs?.recommended ?? false;
rulesEntry.set(prefixedName, {
docsUrl,
isDeprecated,
isRecommended,
// Will be updated later
isImplemented: false,
isNotSupported: false,
});
}
return rulesEntry;
};
/** @param {RuleEntries} ruleEntries */
exports.updateImplementedStatus = async (ruleEntries) => {
const implementedRuleNames = await readAllImplementedRuleNames();
for (const name of implementedRuleNames) {
const rule = ruleEntries.get(name);
if (rule) rule.isImplemented = true;
else console.log(`👀 ${name} is implemented but not found in their rules`);
}
};
/** @param {RuleEntries} ruleEntries */
exports.updateNotSupportedStatus = (ruleEntries) => {
for (const name of NOT_SUPPORTED_RULE_NAMES) {
const rule = ruleEntries.get(name);
if (rule) rule.isNotSupported = true;
}
};