Skip to content

Commit

Permalink
Merge pull request #181 from bmish/rule-no-meta
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Oct 28, 2022
2 parents f34e403 + 222c974 commit eed13a4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
15 changes: 8 additions & 7 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ export async function generate(
? // Object-style rule.
{
name,
description: rule.meta.docs?.description,
fixable: rule.meta.fixable
description: rule.meta?.docs?.description,
fixable: rule.meta?.fixable
? ['code', 'whitespace'].includes(rule.meta.fixable)
: false,
hasSuggestions: rule.meta.hasSuggestions ?? false,
requiresTypeChecking: rule.meta.docs?.requiresTypeChecking ?? false,
deprecated: rule.meta.deprecated ?? false,
schema: rule.meta.schema,
type: rule.meta.type,
hasSuggestions: rule.meta?.hasSuggestions ?? false,
requiresTypeChecking:
rule.meta?.docs?.requiresTypeChecking ?? false,
deprecated: rule.meta?.deprecated ?? false,
schema: rule.meta?.schema,
type: rule.meta?.type,
}
: // Deprecated function-style rule (does not support most of these features).
{
Expand Down
16 changes: 8 additions & 8 deletions lib/rule-notices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,23 @@ function getNoticesForRule(
configsEnabled.length > 0 ||
configsWarn.length > 0 ||
configsDisabled.length > 0,
[NOTICE_TYPE.DEPRECATED]: rule.meta.deprecated || false,
[NOTICE_TYPE.DEPRECATED]: rule.meta?.deprecated || false,

// FIXABLE_AND_HAS_SUGGESTIONS potentially replaces FIXABLE and HAS_SUGGESTIONS.
[NOTICE_TYPE.FIXABLE]:
Boolean(rule.meta.fixable) &&
Boolean(rule.meta?.fixable) &&
(!rule.meta.hasSuggestions ||
!ruleDocNotices.includes(NOTICE_TYPE.FIXABLE_AND_HAS_SUGGESTIONS)),
[NOTICE_TYPE.FIXABLE_AND_HAS_SUGGESTIONS]:
Boolean(rule.meta.fixable) && Boolean(rule.meta.hasSuggestions),
Boolean(rule.meta?.fixable) && Boolean(rule.meta?.hasSuggestions),
[NOTICE_TYPE.HAS_SUGGESTIONS]:
Boolean(rule.meta.hasSuggestions) &&
Boolean(rule.meta?.hasSuggestions) &&
(!rule.meta.fixable ||
!ruleDocNotices.includes(NOTICE_TYPE.FIXABLE_AND_HAS_SUGGESTIONS)),

[NOTICE_TYPE.REQUIRES_TYPE_CHECKING]:
rule.meta.docs?.requiresTypeChecking || false,
[NOTICE_TYPE.TYPE]: Boolean(rule.meta.type),
rule.meta?.docs?.requiresTypeChecking || false,
[NOTICE_TYPE.TYPE]: Boolean(rule.meta?.type),
};

// Recreate object using the ordering and presence of columns specified in ruleDocNotices.
Expand Down Expand Up @@ -326,8 +326,8 @@ function getRuleNoticeLines(
configsDisabled,
configEmojis,
urlConfigs,
replacedBy: rule.meta.replacedBy,
type: rule.meta.type as RULE_TYPE, // Convert union type to enum.
replacedBy: rule.meta?.replacedBy,
type: rule.meta?.type as RULE_TYPE, // Convert union type to enum.
})
: ruleNoticeStrOrFn
);
Expand Down
23 changes: 23 additions & 0 deletions test/lib/__snapshots__/generator-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,29 @@ exports[`generator #generate rule with long-enough description to require name c
"
`;

exports[`generator #generate rule with no meta object generates the documentation 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
✅ Enabled in the \`recommended\` configuration.
| Name | ✅ |
| :----------------------------- | :-- |
| [no-foo](docs/rules/no-foo.md) | ✅ |
<!-- end auto-generated rules list -->
"
`;

exports[`generator #generate rule with no meta object generates the documentation 2`] = `
"# test/no-foo
✅ This rule is enabled in the \`recommended\` config.
<!-- end auto-generated rule header -->
"
`;

exports[`generator #generate rule with type, type column enabled displays the type 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand Down
46 changes: 46 additions & 0 deletions test/lib/generator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,52 @@ describe('generator', function () {
});
});

describe('rule with no meta object', function () {
beforeEach(function () {
mockFs({
'package.json': JSON.stringify({
name: 'eslint-plugin-test',
main: 'index.js',
type: 'module',
}),

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

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

'docs/rules/no-foo.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('generates the documentation', async function () {
await generate('.');
expect(readFileSync('README.md', 'utf8')).toMatchSnapshot();
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});

describe('with `--url-configs` option', function () {
beforeEach(function () {
mockFs({
Expand Down

0 comments on commit eed13a4

Please sign in to comment.