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

Skip to content

chore(website): generated rule Resources with source links #5516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions packages/website/plugins/generated-rule-docs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as fs from 'fs';
import type * as unist from 'unist';
import * as mdast from 'mdast';
import * as path from 'path';
import { format } from 'prettier';
import type { Plugin } from 'unified';
import { compile } from 'json-schema-to-typescript';
Expand All @@ -19,6 +21,13 @@ const COMPLICATED_RULE_OPTIONS = new Set([
'naming-convention',
]);

const sourceUrlPrefix =
'https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/';

const eslintPluginDirectory = path.resolve(
path.join(__dirname, '../../eslint-plugin'),
);

export const generatedRuleDocs: Plugin = () => {
return async (root, file) => {
if (file.stem == null) {
Expand Down Expand Up @@ -288,6 +297,65 @@ export const generatedRuleDocs: Plugin = () => {
type: 'paragraph',
} as mdast.Paragraph);
}

// 7. Also add a link to view the rule's source and test code
parent.children.push(
{
children: [
{
type: 'text',
value: 'Resources',
},
],
depth: 2,
type: 'heading',
} as mdast.Heading,
{
children: [
{
children: [
{
children: [
{
type: 'link',
url: `${sourceUrlPrefix}src/rules/${file.stem}.ts`,
children: [
{
type: 'text',
value: 'Rule source',
},
],
},
],
type: 'paragraph',
},
],
type: 'listItem',
},
{
children: [
{
children: [
{
type: 'link',
url: getUrlForRuleTest(file.stem),
children: [
{
type: 'text',
value: 'Test source',
},
],
},
],
type: 'paragraph',
},
],
type: 'listItem',
},
],
type: 'list',
} as mdast.List,
);
};
};

Expand All @@ -305,3 +373,16 @@ function createH2TextFilter(
node.children[0].type === 'text' &&
node.children[0].value === text;
}

function getUrlForRuleTest(ruleName: string): string {
for (const localPath of [
`tests/rules/${ruleName}.test.ts`,
`tests/rules/${ruleName}/`,
]) {
if (fs.existsSync(`${eslintPluginDirectory}/${localPath}`)) {
return `${sourceUrlPrefix}${localPath}`;
}
}

throw new Error(`Could not find test file for ${ruleName}.`);
}