Skip to content
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

Initialize rule doc options section when using --init-rule-docs #485

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
parseRuleListColumnsOption,
parseConfigEmojiOptions,
} from './option-parsers.js';
import { END_RULE_HEADER_MARKER } from './comment-markers.js';
import {
BEGIN_RULE_OPTIONS_LIST_MARKER,
END_RULE_HEADER_MARKER,
END_RULE_OPTIONS_LIST_MARKER,
} from './comment-markers.js';
import {
replaceOrCreateHeader,
expectContentOrFail,
Expand Down Expand Up @@ -148,6 +152,7 @@ export async function generate(path: string, options?: GenerateOptions) {
const schema = rule.meta?.schema;
const description = rule.meta?.docs?.description;
const pathToDoc = replaceRulePlaceholder(join(path, pathRuleDoc), name);
const ruleHasOptions = hasOptions(schema);

if (!existsSync(pathToDoc)) {
if (!initRuleDocs) {
Expand All @@ -157,7 +162,12 @@ export async function generate(path: string, options?: GenerateOptions) {
}

mkdirSync(dirname(pathToDoc), { recursive: true });
writeFileSync(pathToDoc, '');
writeFileSync(
pathToDoc,
ruleHasOptions
? `\n## Options\n\n${BEGIN_RULE_OPTIONS_LIST_MARKER}\n${END_RULE_OPTIONS_LIST_MARKER}\n`
: ''
);
initializedRuleDoc = true;
}

Expand Down Expand Up @@ -235,7 +245,7 @@ export async function generate(path: string, options?: GenerateOptions) {
`\`${name}\` rule doc`,
contentsNew,
['Options', 'Config'],
hasOptions(schema)
ruleHasOptions
);
for (const { name: namedOption } of getAllNamedOptions(schema)) {
expectContentOrFail(
Expand Down
17 changes: 17 additions & 0 deletions test/lib/generate/__snapshots__/file-paths-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ exports[`generate (file paths) missing rule doc when initRuleDocs is true create
"
`;

exports[`generate (file paths) missing rule doc when initRuleDocs is true creates the rule doc 2`] = `
"# test/no-bar

<!-- end auto-generated rule header -->

## Options

<!-- begin auto-generated rule options list -->

| Name |
| :-------- |
| \`option1\` |

<!-- end auto-generated rule options list -->
"
`;

exports[`generate (file paths) multiple rules lists generates the documentation 1`] = `
"<!-- begin auto-generated rules list -->

Expand Down
7 changes: 6 additions & 1 deletion test/lib/generate/file-paths-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('generate (file paths)', function () {
meta: { },
create(context) {}
},
'no-bar': {
meta: { schema: [{ type: 'object', properties: { option1: {} } }] },
create(context) {}
},
},
};`,

Expand All @@ -46,7 +50,7 @@ describe('generate (file paths)', function () {
it('throws an error', async function () {
// Use join to handle both Windows and Unix paths.
await expect(generate('.')).rejects.toThrow(
`Could not find rule doc: ${join('docs', 'rules', 'no-foo.md')}`
`Could not find rule doc: ${join('docs', 'rules', 'no-bar.md')}`
);
});
});
Expand All @@ -55,6 +59,7 @@ describe('generate (file paths)', function () {
it('creates the rule doc', async function () {
await generate('.', { initRuleDocs: true });
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
expect(readFileSync('docs/rules/no-bar.md', 'utf8')).toMatchSnapshot(); // Should add options section.
});
});
});
Expand Down
Loading