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

Avoid creating a duplicate title when rule doc is missing marker comment #12

Merged
merged 1 commit into from
Oct 1, 2022
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
5 changes: 5 additions & 0 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function replaceOrCreateHeader(
) {
const markerLineIndex = lines.indexOf(marker);

if (markerLineIndex === -1 && lines.length > 0 && lines[0].startsWith('# ')) {
// No marker present so delete any existing title before we add the new one.
lines.splice(0, 1);
}

// Replace header section (or create at top if missing).
lines.splice(0, markerLineIndex + 1, ...newHeaderLines);
}
Expand Down
15 changes: 15 additions & 0 deletions test/lib/__snapshots__/generator-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ exports[`generator #generate only a \`recommended\` config updates the documenta
"
`;

exports[`generator #generate rule doc without header marker but pre-existing header updates the documentation 1`] = `
"# Description. (\`no-foo\`)

✅ This rule is enabled in the \`recommended\` config.

<!-- end rule header -->

Pre-existing notice about the rule being recommended.

## Rule details

Details.
"
`;

exports[`generator #generate successful updates the documentation 1`] = `
"# eslint-plugin-test

Expand Down
54 changes: 54 additions & 0 deletions test/lib/generator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,60 @@ describe('generator', function () {
});
});

describe('rule doc without header marker but pre-existing header', function () {
beforeEach(function () {
mockFs({
'package.json': JSON.stringify({
name: 'eslint-plugin-test',
main: 'index.js',
type: 'module',
}),

'index.js': `
export default {
rules: {
'no-foo': {
meta: { docs: { description: 'Description.' }, },
create(context) {}
},
},
configs: {
recommended: {
rules: {
'test/no-foo': 'error',
}
}
}
};`,

'README.md': '<!-- begin rules list --><!-- end rules list -->',

'docs/rules/no-foo.md': outdent`
# Some pre-existing title.
Pre-existing notice about the rule being recommended.
## Rule details
Details.
`,

// Needed for some of the test infrastructure to work.
node_modules: mockFs.load(
resolve(__dirname, '..', '..', 'node_modules')
),
});
});

afterEach(function () {
mockFs.restore();
jest.resetModules();
});

it('updates the documentation', async function () {
await generate('.');

expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});

describe('deprecated rules', function () {
beforeEach(function () {
mockFs({
Expand Down