Skip to content

Commit

Permalink
fix: initialize rule doc options section with --init-rule-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Oct 15, 2023
1 parent d43e57c commit 4e94f98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
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

0 comments on commit 4e94f98

Please sign in to comment.