/
docs.test.ts
170 lines (147 loc) · 5.15 KB
/
docs.test.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import fs from 'fs';
import path from 'path';
import marked from 'marked';
import rules from '../src/rules';
const docsRoot = path.resolve(__dirname, '../docs/rules');
const rulesData = Object.entries(rules);
function createRuleLink(ruleName: string): string {
return `[\`@typescript-eslint/${ruleName}\`](./docs/rules/${ruleName}.md)`;
}
function parseReadme(): {
base: marked.Tokens.Table;
extension: marked.Tokens.Table;
} {
const readmeRaw = fs.readFileSync(
path.resolve(__dirname, '../README.md'),
'utf8',
);
const readme = marked.lexer(readmeRaw, {
gfm: true,
silent: false,
});
// find the table
const rulesTables = readme.filter(
(token): token is marked.Tokens.Table => token.type === 'table',
);
if (rulesTables.length !== 2) {
throw Error('Could not find both rules tables in README.md');
}
return {
base: rulesTables[0],
extension: rulesTables[1],
};
}
describe('Validating rule docs', () => {
it('All rules must have a corresponding rule doc', () => {
const files = fs.readdirSync(docsRoot);
const ruleFiles = Object.keys(rules)
.map(rule => `${rule}.md`)
.sort();
expect(files.sort()).toEqual(ruleFiles);
});
for (const [ruleName, rule] of rulesData) {
const filePath = path.join(docsRoot, `${ruleName}.md`);
it(`Description of ${ruleName}.md must match`, () => {
// validate if description of rule is same as in docs
const file = fs.readFileSync(filePath, 'utf-8');
const tokens = marked.lexer(file, {
gfm: true,
silent: false,
});
// Rule title not found.
// Rule title does not match the rule metadata.
expect(tokens[0]).toMatchObject({
type: 'heading',
depth: 1,
text: `${rule.meta.docs?.description} (\`${ruleName}\`)`,
});
});
}
});
describe('Validating rule metadata', () => {
for (const [ruleName, rule] of rulesData) {
describe(`${ruleName}`, () => {
it('`name` field in rule must match the filename', () => {
// validate if rule name is same as url
// there is no way to access this field but its used only in generation of docs url
expect(
rule.meta.docs?.url.endsWith(`rules/${ruleName}.md`),
).toBeTruthy();
});
it('`requiresTypeChecking` should be set if the rule uses type information', () => {
// quick-and-dirty check to see if it uses parserServices
// not perfect but should be good enough
const ruleFileContents = fs.readFileSync(
path.resolve(__dirname, `../src/rules/${ruleName}.ts`),
);
expect(ruleFileContents.includes('getParserServices')).toEqual(
rule.meta.docs?.requiresTypeChecking ?? false,
);
});
});
}
});
describe('Validating README.md', () => {
const rulesTables = parseReadme();
const notDeprecated = rulesData.filter(([, rule]) => !rule.meta.deprecated);
const baseRules = notDeprecated.filter(
([, rule]) => !rule.meta.docs?.extendsBaseRule,
);
const extensionRules = notDeprecated.filter(
([, rule]) => rule.meta.docs?.extendsBaseRule,
);
it('All non-deprecated base rules should have a row in the base rules table, and the table should be ordered alphabetically', () => {
const baseRuleNames = baseRules
.map(([ruleName]) => ruleName)
.sort()
.map(createRuleLink);
expect(rulesTables.base.cells.map(row => row[0])).toStrictEqual(
baseRuleNames,
);
});
it('All non-deprecated extension rules should have a row in the base rules table, and the table should be ordered alphabetically', () => {
const extensionRuleNames = extensionRules
.map(([ruleName]) => ruleName)
.sort()
.map(createRuleLink);
expect(rulesTables.extension.cells.map(row => row[0])).toStrictEqual(
extensionRuleNames,
);
});
for (const [ruleName, rule] of notDeprecated) {
describe(`Checking rule ${ruleName}`, () => {
const ruleRow: string[] | undefined = (rule.meta.docs?.extendsBaseRule
? rulesTables.extension.cells
: rulesTables.base.cells
).find(row => row[0].includes(`/${ruleName}.md`));
if (!ruleRow) {
// rule is in the wrong table, the first two tests will catch this, so no point in creating noise;
// these tests will ofc fail in that case
return;
}
it('Link column should be correct', () => {
expect(ruleRow[0]).toEqual(createRuleLink(ruleName));
});
it('Description column should be correct', () => {
expect(ruleRow[1]).toEqual(rule.meta.docs?.description);
});
it('Recommended column should be correct', () => {
expect(ruleRow[2]).toEqual(
rule.meta.docs?.recommended ? ':heavy_check_mark:' : '',
);
});
it('Fixable column should be correct', () => {
expect(ruleRow[3]).toEqual(
rule.meta.fixable !== undefined ? ':wrench:' : '',
);
});
it('Requiring type information column should be correct', () => {
expect(ruleRow[4]).toEqual(
rule.meta.docs?.requiresTypeChecking === true
? ':thought_balloon:'
: '',
);
});
});
}
});