Skip to content

Commit

Permalink
fix: don't exit with failure about missing rule option when rule opti…
Browse files Browse the repository at this point in the history
…on list was just generated
  • Loading branch information
bmish committed Oct 13, 2023
1 parent 7c07cae commit 71e816a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
20 changes: 12 additions & 8 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,28 @@ export async function generate(path: string, options?: GenerateOptions) {
urlRuleDoc
);

const contents = readFileSync(pathToDoc).toString();
const contentsOld = readFileSync(pathToDoc).toString();
const contentsNew = await postprocess(
updateRuleOptionsList(
replaceOrCreateHeader(contents, newHeaderLines, END_RULE_HEADER_MARKER),
replaceOrCreateHeader(
contentsOld,
newHeaderLines,
END_RULE_HEADER_MARKER
),
rule
),
resolve(pathToDoc)
);

if (check) {
if (contentsNew !== contents) {
if (contentsNew !== contentsOld) {
console.error(
`Please run eslint-doc-generator. A rule doc is out-of-date: ${relative(
getPluginRoot(path),
pathToDoc
)}`
);
console.error(diff(contentsNew, contents, { expand: false }));
console.error(diff(contentsNew, contentsOld, { expand: false }));
process.exitCode = 1;
}
} else {
Expand All @@ -209,7 +213,7 @@ export async function generate(path: string, options?: GenerateOptions) {
for (const section of ruleDocSectionInclude) {
expectSectionHeaderOrFail(
`\`${name}\` rule doc`,
contents,
contentsNew,
[section],
true
);
Expand All @@ -219,7 +223,7 @@ export async function generate(path: string, options?: GenerateOptions) {
for (const section of ruleDocSectionExclude) {
expectSectionHeaderOrFail(
`\`${name}\` rule doc`,
contents,
contentsNew,
[section],
false
);
Expand All @@ -229,15 +233,15 @@ export async function generate(path: string, options?: GenerateOptions) {
// Options section.
expectSectionHeaderOrFail(
`\`${name}\` rule doc`,
contents,
contentsNew,
['Options', 'Config'],
hasOptions(schema)
);
for (const { name: namedOption } of getAllNamedOptions(schema)) {
expectContentOrFail(
`\`${name}\` rule doc`,
'rule option',
contents,
contentsNew,
namedOption,
true
); // Each rule option is mentioned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ exports[`generate (rule options list) with no options generates the documentatio
"# test/no-foo
<!-- end auto-generated rule header -->
## Options
<!-- begin auto-generated rule options list -->
Expand Down
18 changes: 17 additions & 1 deletion test/lib/generate/rule-options-list-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { jest } from '@jest/globals';
import * as sinon from 'sinon';

const __dirname = dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -74,7 +75,10 @@ describe('generate (rule options list)', function () {
});

it('generates the documentation', async function () {
const consoleErrorStub = sinon.stub(console, 'error');
await generate('.');
expect(consoleErrorStub.callCount).toBe(0);
consoleErrorStub.restore();
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -126,7 +130,10 @@ describe('generate (rule options list)', function () {
});

it('generates the documentation', async function () {
const consoleErrorStub = sinon.stub(console, 'error');
await generate('.');
expect(consoleErrorStub.callCount).toBe(0);
consoleErrorStub.restore();
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -155,7 +162,7 @@ describe('generate (rule options list)', function () {

'README.md': '## Rules\n',

'docs/rules/no-foo.md': `## Options
'docs/rules/no-foo.md': `
<!-- begin auto-generated rule options list -->
<!-- end auto-generated rule options list -->`,

Expand All @@ -170,7 +177,10 @@ describe('generate (rule options list)', function () {
});

it('generates the documentation', async function () {
const consoleErrorStub = sinon.stub(console, 'error');
await generate('.');
expect(consoleErrorStub.callCount).toBe(0);
consoleErrorStub.restore();
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -211,7 +221,10 @@ describe('generate (rule options list)', function () {
});

it('generates the documentation', async function () {
const consoleErrorStub = sinon.stub(console, 'error');
await generate('.');
expect(consoleErrorStub.callCount).toBe(0);
consoleErrorStub.restore();
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -255,7 +268,10 @@ describe('generate (rule options list)', function () {
});

it('generates the documentation', async function () {
const consoleErrorStub = sinon.stub(console, 'error');
await generate('.');
expect(consoleErrorStub.callCount).toBe(0);
consoleErrorStub.restore();
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});
Expand Down

0 comments on commit 71e816a

Please sign in to comment.