Skip to content

Commit 694d8ae

Browse files
committed
feat: show diff when docs out-of-sync with --check
1 parent 99ace45 commit 694d8ae

File tree

5 files changed

+63
-42
lines changed

5 files changed

+63
-42
lines changed

lib/generator.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { findSectionHeader, replaceOrCreateHeader } from './markdown.js';
1818
import { resolveConfigsToRules } from './config-resolution.js';
1919
import { RuleDocTitleFormat } from './rule-doc-title-format.js';
2020
import { parseConfigEmojiOptions } from './configs.js';
21+
import { diff } from 'jest-diff';
2122
import type { RuleDetails } from './types.js';
2223

2324
/**
@@ -188,6 +189,7 @@ export async function generate(
188189
pathToDoc
189190
)}`
190191
);
192+
console.error(diff(contentsNew, contents, { expand: false }));
191193
process.exitCode = 1;
192194
}
193195
} else {
@@ -251,6 +253,7 @@ export async function generate(
251253
pathToReadme
252254
)} is out-of-date.`
253255
);
256+
console.error(diff(readmeContentsNew, readmeContents, { expand: false }));
254257
process.exitCode = 1;
255258
}
256259
} else {

package-lock.json

+22-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040
"lint:types": "tsc",
4141
"prepublishOnly": "tsc",
4242
"release": "release-it",
43-
"test": "node --max-old-space-size=4096 --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
43+
"test": "node --max-old-space-size=4096 --experimental-vm-modules node_modules/jest/bin/jest.js --colors --coverage"
4444
},
4545
"dependencies": {
4646
"@typescript-eslint/utils": "^5.38.1",
4747
"camelcase": "^7.0.0",
4848
"commander": "^9.4.0",
49+
"jest-diff": "^29.2.1",
4950
"markdown-table": "^3.0.2",
5051
"type-fest": "^3.0.0"
5152
},

test/lib/__snapshots__/generator-test.ts.snap

+31-1
Original file line numberDiff line numberDiff line change
@@ -1082,11 +1082,41 @@ exports[`generator #generate with \`--url-configs\` option with only recommended
10821082
`;
10831083

10841084
exports[`generator #generate with --check prints the issues, exits with failure, and does not write changes 1`] = `
1085+
[
1086+
"- Expected
1087+
+ Received
1088+
1089+
- # Description for no-foo (\`test/no-foo\`)
1090+
-
1091+
- <!-- end auto-generated rule header -->
1092+
-
1093+
+ # test/no-foo",
1094+
]
1095+
`;
1096+
1097+
exports[`generator #generate with --check prints the issues, exits with failure, and does not write changes 2`] = `
1098+
[
1099+
"- Expected
1100+
+ Received
1101+
1102+
 ## Rules
1103+
- <!-- begin auto-generated rules list -->
1104+
-
1105+
- | Name | Description |
1106+
- | :----------------------------- | :---------------------- |
1107+
- | [no-foo](docs/rules/no-foo.md) | Description for no-foo. |
1108+
-
1109+
- <!-- end auto-generated rules list -->
1110+
",
1111+
]
1112+
`;
1113+
1114+
exports[`generator #generate with --check prints the issues, exits with failure, and does not write changes 3`] = `
10851115
"## Rules
10861116
"
10871117
`;
10881118

1089-
exports[`generator #generate with --check prints the issues, exits with failure, and does not write changes 2`] = `""`;
1119+
exports[`generator #generate with --check prints the issues, exits with failure, and does not write changes 4`] = `"# test/no-foo"`;
10901120

10911121
exports[`generator #generate with --config-emoji and removing default emoji for a config reverts to using a badge for the config 1`] = `
10921122
"## Rules

test/lib/generator-test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2728,7 +2728,7 @@ describe('generator', function () {
27282728

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

2731-
'docs/rules/no-foo.md': '',
2731+
'docs/rules/no-foo.md': '# test/no-foo',
27322732

27332733
// Needed for some of the test infrastructure to work.
27342734
node_modules: mockFs.load(
@@ -2745,7 +2745,7 @@ describe('generator', function () {
27452745
it('prints the issues, exits with failure, and does not write changes', async function () {
27462746
const consoleErrorStub = sinon.stub(console, 'error');
27472747
await generate('.', { check: true });
2748-
expect(consoleErrorStub.callCount).toBe(2);
2748+
expect(consoleErrorStub.callCount).toBe(4);
27492749
// Use join to handle both Windows and Unix paths.
27502750
expect(consoleErrorStub.firstCall.args).toStrictEqual([
27512751
`Please run eslint-doc-generator. A rule doc is out-of-date: ${join(
@@ -2754,9 +2754,11 @@ describe('generator', function () {
27542754
'no-foo.md'
27552755
)}`,
27562756
]);
2757-
expect(consoleErrorStub.secondCall.args).toStrictEqual([
2757+
expect(consoleErrorStub.secondCall.args).toMatchSnapshot(); // Diff
2758+
expect(consoleErrorStub.thirdCall.args).toStrictEqual([
27582759
'Please run eslint-doc-generator. README.md is out-of-date.',
27592760
]);
2761+
expect(consoleErrorStub.getCall(3).args).toMatchSnapshot(); // Diff
27602762
consoleErrorStub.restore();
27612763

27622764
expect(readFileSync('README.md', 'utf8')).toMatchSnapshot();

0 commit comments

Comments
 (0)