Skip to content

Commit

Permalink
fix: sort configs, rules, lists case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Oct 26, 2022
1 parent 5e93e2c commit d4b9348
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/configs.ts
Expand Up @@ -33,7 +33,9 @@ export function getConfigsForRule(
}
}

return configNames.sort();
return configNames.sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase())
);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions lib/rule-list.ts
Expand Up @@ -169,7 +169,9 @@ function generateRulesListMarkdown(
[
listHeaderRow,
...details
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
.sort(({ name: a }, { name: b }) =>
a.toLowerCase().localeCompare(b.toLowerCase())
)
.map((rule: RuleDetails) =>
buildRuleRow(
columns,
Expand Down Expand Up @@ -239,8 +241,8 @@ function generateRulesListMarkdownWithSplitBy(

// For each possible non-disabled value, show a header and list of corresponding rules.
for (const value of [...values.values()]
.sort()
.filter((value) => !DISABLED_VALUES.has(value))) {
.filter((value) => !DISABLED_VALUES.has(value))
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))) {
const rulesForThisValue = details.filter(
(detail) => getPropertyFromRule(plugin, detail.name, splitBy) === value
);
Expand Down
65 changes: 65 additions & 0 deletions test/lib/__snapshots__/generator-test.ts.snap
Expand Up @@ -656,6 +656,45 @@ exports[`generator #generate shows column and notice for requiresTypeChecking up
"
`;

exports[`generator #generate sorting rules and configs case-insensitive sorts correctly 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
💼 Configurations enabled in.
| Name | 💼 |
| :------------------- | :------------------- |
| [a](docs/rules/a.md) | ![a][] ![B][] ![c][] |
| [B](docs/rules/B.md) | |
| [c](docs/rules/c.md) | |
<!-- end auto-generated rules list -->
"
`;

exports[`generator #generate sorting rules and configs case-insensitive sorts correctly 2`] = `
"# \`test/a\`
💼 This rule is enabled in the following configs: \`a\`, \`B\`, \`c\`.
<!-- end auto-generated rule header -->
"
`;

exports[`generator #generate sorting rules and configs case-insensitive sorts correctly 3`] = `
"# \`test/B\`
<!-- end auto-generated rule header -->
"
`;

exports[`generator #generate sorting rules and configs case-insensitive sorts correctly 4`] = `
"# \`test/c\`
<!-- end auto-generated rule header -->
"
`;

exports[`generator #generate splitting list by nested property meta.docs.category splits the list 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand Down Expand Up @@ -705,6 +744,32 @@ exports[`generator #generate splitting list by type splits the list 1`] = `
"
`;

exports[`generator #generate splitting list, ignores case splits the list 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
### a
| Name |
| :----------------------------- |
| [no-foo](docs/rules/no-foo.md) |
### B
| Name |
| :----------------------------- |
| [no-bar](docs/rules/no-bar.md) |
### c
| Name |
| :----------------------------- |
| [no-baz](docs/rules/no-baz.md) |
<!-- end auto-generated rules list -->
"
`;

exports[`generator #generate splitting list, with boolean splits the list 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand Down
94 changes: 94 additions & 0 deletions test/lib/generator-test.ts
Expand Up @@ -3365,6 +3365,50 @@ describe('generator', function () {
});
});

describe('splitting list, ignores case', 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: { foo: 'a' }, create(context) {} },
'no-bar': { meta: { foo: 'B' }, create(context) {} },
'no-baz': { meta: { foo: 'c' }, create(context) {} },
},
};`,

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

'docs/rules/no-foo.md': '',
'docs/rules/no-bar.md': '',
'docs/rules/no-baz.md': '',

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

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

it('splits the list', async function () {
await generate('.', {
splitBy: 'meta.foo',
});
expect(readFileSync('README.md', 'utf8')).toMatchSnapshot();
});
});

describe('splitting list, with unknown variable type', function () {
beforeEach(function () {
mockFs({
Expand Down Expand Up @@ -3525,5 +3569,55 @@ describe('generator', function () {
expect(readFileSync('docs/rules/foo.md', 'utf8')).toMatchSnapshot();
});
});

describe('sorting rules and configs case-insensitive', function () {
beforeEach(function () {
mockFs({
'package.json': JSON.stringify({
name: 'eslint-plugin-test',
main: 'index.js',
type: 'module',
}),

'index.js': `
export default {
rules: {
'c': { meta: { docs: {} }, create(context) {} },
'a': { meta: { docs: {} }, create(context) {} },
'B': { meta: { docs: {} }, create(context) {} },
},
configs: {
'c': { rules: { 'test/a': 'error', } },
'a': { rules: { 'test/a': 'error', } },
'B': { rules: { 'test/a': 'error', } },
}
};`,

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

'docs/rules/a.md': '',
'docs/rules/B.md': '',
'docs/rules/c.md': '',

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

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

it('sorts correctly', async function () {
await generate('.');
expect(readFileSync('README.md', 'utf8')).toMatchSnapshot();
expect(readFileSync('docs/rules/a.md', 'utf8')).toMatchSnapshot();
expect(readFileSync('docs/rules/B.md', 'utf8')).toMatchSnapshot();
expect(readFileSync('docs/rules/c.md', 'utf8')).toMatchSnapshot();
});
});
});
});

0 comments on commit d4b9348

Please sign in to comment.