forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtslint-rule-begone.js
87 lines (73 loc) · 2.63 KB
/
tslint-rule-begone.js
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
import { parse } from "comment-json";
import fs from "fs";
import * as path from "path";
import sh from "shelljs";
/**
* @param {string} filePath
*/
const parseAndReadFileContents = filePath => {
try {
return parse(fs.readFileSync(filePath).toString()) ?? {};
} catch {
return {};
}
};
/**
* @param {string} filePath
* @param {unknown} contents
*/
const writeFileFormatted = (filePath, contents) => {
fs.writeFileSync(
filePath,
JSON.stringify(contents, null, 4),
);
};
const [, , ...tslintRuleNames] = process.argv;
for (const tslintRuleName of tslintRuleNames) {
console.log(`Clearing ${tslintRuleName} comment directives...`);
sh.exec(
`find types -path '*/node_modules' -prune -o -iname '*.ts' -type f -print | xargs sed -i '' '/tslint:disable[: ]${tslintRuleName}/d'`,
);
sh.exec(
`find types -path '*/node_modules' -prune -o -iname '*.ts' -type f -print | xargs sed -i '' '/tslint:enable[: ]${tslintRuleName}/d'`,
);
sh.exec(
`find types -path '*/node_modules' -prune -o -iname '*.ts' -type f -print | xargs sed -i '' '/tslint:disable-next-line[: ]${tslintRuleName}/d'`,
);
sh.exec(
`find types -path '*/node_modules' -prune -o -iname '*.ts' -type f -print | xargs sed -i '' '/tslint:disable-line[: ]${tslintRuleName}/d'`,
);
}
console.log("Done clearing comment directives.");
const typeNames = fs.readdirSync("types");
for (const typeName of typeNames) {
const typeDirectory = path.join("types", typeName);
try {
typeNames.push(...fs.readdirSync(typeDirectory).map(childDirectory => path.join(typeName, childDirectory)));
} catch {}
const tslintFilePath = path.join(typeDirectory, "tslint.json");
/** @type {Partial<{ extends: string; rules?: { [s:string]: boolean }}>} */
const tslintData = parseAndReadFileContents(tslintFilePath);
if (!tslintData?.rules) {
continue;
}
let matchedTSLintRule = false;
for (const tslintRuleName of tslintRuleNames) {
if (tslintData.rules[tslintRuleName] !== undefined) {
delete tslintData.rules[tslintRuleName];
matchedTSLintRule = true;
}
}
if (!matchedTSLintRule) {
continue;
}
console.log(`Re-writing ${typeName}...`);
if (Object.keys(tslintData.rules).length === 0) {
console.log(`\t${tslintFilePath} has no remaining rules; deleting rules property.`);
delete tslintData.rules;
} else {
console.log(`\t${tslintFilePath} has remaining rules.`);
}
writeFileFormatted(tslintFilePath, tslintData);
console.log(`\t(Re-)wrote ${tslintFilePath}.`);
}