-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateRuleExamples.ts
More file actions
121 lines (93 loc) · 3.34 KB
/
Copy pathgenerateRuleExamples.ts
File metadata and controls
121 lines (93 loc) · 3.34 KB
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
import {nodeFs} from './convertEsmToCjs.js';
import * as rulesTests from './spec/rules/index.js';
const {readFileSync, writeFileSync} = nodeFs;
const startMarker = '<!-- begin auto-generated examples -->';
const endMarker = '<!-- end auto-generated examples -->';
type File = Readonly<{codes: string[]; filename: string; output: string | undefined}>;
for (const [ruleName, ruleTests] of Object.entries(rulesTests)) {
const invalidFiles: File[] = [];
const invalidFilesHash: Record<string, File> = {};
const validFiles: File[] = [];
const validFilesHash: Record<string, File> = {};
for (const {code, comment, filename, hideInDocs, output, showFixInDocs} of ruleTests.invalid) {
if (hideInDocs === true) {
continue;
}
if (comment?.includes('\n') === true) {
throw new Error(`Comment for "${code}" is multi-line: ${comment}`);
}
const fullCode = comment === undefined ? code : `// ${comment}\n${code}`;
if (code.includes('\n') || showFixInDocs === true) {
invalidFiles.push({
codes: [fullCode],
filename,
output: showFixInDocs === true ? output : undefined,
});
continue;
}
let file = invalidFilesHash[filename];
if (file === undefined) {
file = {codes: [], filename, output: undefined};
invalidFiles.push(file);
invalidFilesHash[filename] = file;
}
file.codes.push(fullCode);
}
for (const {code, comment, filename, hideInDocs} of ruleTests.valid) {
if (hideInDocs === true) {
continue;
}
if (comment?.includes('\n') === true) {
throw new Error(`Comment for "${code}" is multi-line: ${comment}`);
}
const fullCode = comment === undefined ? code : `// ${comment}\n${code}`;
if (code.includes('\n')) {
validFiles.push({codes: [fullCode], filename, output: undefined});
continue;
}
let file = validFilesHash[filename];
if (file === undefined) {
file = {codes: [], filename, output: undefined};
validFiles.push(file);
validFilesHash[filename] = file;
}
file.codes.push(fullCode);
}
const incorrectText =
'### ❌ Incorrect\n\n' +
invalidFiles
.map(
({codes, filename, output}) =>
'```ts\n' +
`// ${filename}\n\n` +
codes.join('\n\n') +
'\n```' +
(output === undefined
? ''
: `\n\n<details>\n<summary>Fixed</summary>\n\n` +
'```ts\n' +
output +
'\n```\n\n</details>'),
)
.join('\n\n');
const correctText =
'### ✅ Correct\n\n' +
validFiles
.map(({codes, filename}) => '```ts\n' + `// ${filename}\n\n` + codes.join('\n\n') + '\n```')
.join('\n\n');
const filePath = `./docs/rules/${ruleName}.md`;
const fileContent = readFileSync(filePath, {encoding: 'utf8'});
const startIndex = fileContent.indexOf(startMarker) + startMarker.length;
const endIndex = fileContent.indexOf(endMarker);
if (startIndex < startMarker.length) {
throw new Error(`Cannot find start marker ${startMarker} in ${filePath}`);
}
if (endIndex < 0) {
throw new Error(`Cannot find end marker ${endMarker} in ${filePath}`);
}
const newFileContent =
fileContent.slice(0, startIndex) +
`\n\n${incorrectText}\n\n${correctText}\n\n` +
fileContent.slice(endIndex);
writeFileSync(filePath, newFileContent);
}