Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9f4e2b

Browse files
chore(website): generated rule Resources with source links (typescript-eslint#5516)
1 parent fde1c63 commit f9f4e2b

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

packages/website/plugins/generated-rule-docs.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as fs from 'fs';
12
import type * as unist from 'unist';
23
import * as mdast from 'mdast';
4+
import * as path from 'path';
35
import { format } from 'prettier';
46
import type { Plugin } from 'unified';
57
import { compile } from 'json-schema-to-typescript';
@@ -19,6 +21,13 @@ const COMPLICATED_RULE_OPTIONS = new Set([
1921
'naming-convention',
2022
]);
2123

24+
const sourceUrlPrefix =
25+
'https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/';
26+
27+
const eslintPluginDirectory = path.resolve(
28+
path.join(__dirname, '../../eslint-plugin'),
29+
);
30+
2231
export const generatedRuleDocs: Plugin = () => {
2332
return async (root, file) => {
2433
if (file.stem == null) {
@@ -288,6 +297,65 @@ export const generatedRuleDocs: Plugin = () => {
288297
type: 'paragraph',
289298
} as mdast.Paragraph);
290299
}
300+
301+
// 7. Also add a link to view the rule's source and test code
302+
parent.children.push(
303+
{
304+
children: [
305+
{
306+
type: 'text',
307+
value: 'Resources',
308+
},
309+
],
310+
depth: 2,
311+
type: 'heading',
312+
} as mdast.Heading,
313+
{
314+
children: [
315+
{
316+
children: [
317+
{
318+
children: [
319+
{
320+
type: 'link',
321+
url: `${sourceUrlPrefix}src/rules/${file.stem}.ts`,
322+
children: [
323+
{
324+
type: 'text',
325+
value: 'Rule source',
326+
},
327+
],
328+
},
329+
],
330+
type: 'paragraph',
331+
},
332+
],
333+
type: 'listItem',
334+
},
335+
{
336+
children: [
337+
{
338+
children: [
339+
{
340+
type: 'link',
341+
url: getUrlForRuleTest(file.stem),
342+
children: [
343+
{
344+
type: 'text',
345+
value: 'Test source',
346+
},
347+
],
348+
},
349+
],
350+
type: 'paragraph',
351+
},
352+
],
353+
type: 'listItem',
354+
},
355+
],
356+
type: 'list',
357+
} as mdast.List,
358+
);
291359
};
292360
};
293361

@@ -305,3 +373,16 @@ function createH2TextFilter(
305373
node.children[0].type === 'text' &&
306374
node.children[0].value === text;
307375
}
376+
377+
function getUrlForRuleTest(ruleName: string): string {
378+
for (const localPath of [
379+
`tests/rules/${ruleName}.test.ts`,
380+
`tests/rules/${ruleName}/`,
381+
]) {
382+
if (fs.existsSync(`${eslintPluginDirectory}/${localPath}`)) {
383+
return `${sourceUrlPrefix}${localPath}`;
384+
}
385+
}
386+
387+
throw new Error(`Could not find test file for ${ruleName}.`);
388+
}

0 commit comments

Comments
 (0)