Skip to content

Commit eed13a4

Browse files
authored
Merge pull request #181 from bmish/rule-no-meta
2 parents f34e403 + 222c974 commit eed13a4

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

lib/generator.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,16 @@ export async function generate(
113113
? // Object-style rule.
114114
{
115115
name,
116-
description: rule.meta.docs?.description,
117-
fixable: rule.meta.fixable
116+
description: rule.meta?.docs?.description,
117+
fixable: rule.meta?.fixable
118118
? ['code', 'whitespace'].includes(rule.meta.fixable)
119119
: false,
120-
hasSuggestions: rule.meta.hasSuggestions ?? false,
121-
requiresTypeChecking: rule.meta.docs?.requiresTypeChecking ?? false,
122-
deprecated: rule.meta.deprecated ?? false,
123-
schema: rule.meta.schema,
124-
type: rule.meta.type,
120+
hasSuggestions: rule.meta?.hasSuggestions ?? false,
121+
requiresTypeChecking:
122+
rule.meta?.docs?.requiresTypeChecking ?? false,
123+
deprecated: rule.meta?.deprecated ?? false,
124+
schema: rule.meta?.schema,
125+
type: rule.meta?.type,
125126
}
126127
: // Deprecated function-style rule (does not support most of these features).
127128
{

lib/rule-notices.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,23 +217,23 @@ function getNoticesForRule(
217217
configsEnabled.length > 0 ||
218218
configsWarn.length > 0 ||
219219
configsDisabled.length > 0,
220-
[NOTICE_TYPE.DEPRECATED]: rule.meta.deprecated || false,
220+
[NOTICE_TYPE.DEPRECATED]: rule.meta?.deprecated || false,
221221

222222
// FIXABLE_AND_HAS_SUGGESTIONS potentially replaces FIXABLE and HAS_SUGGESTIONS.
223223
[NOTICE_TYPE.FIXABLE]:
224-
Boolean(rule.meta.fixable) &&
224+
Boolean(rule.meta?.fixable) &&
225225
(!rule.meta.hasSuggestions ||
226226
!ruleDocNotices.includes(NOTICE_TYPE.FIXABLE_AND_HAS_SUGGESTIONS)),
227227
[NOTICE_TYPE.FIXABLE_AND_HAS_SUGGESTIONS]:
228-
Boolean(rule.meta.fixable) && Boolean(rule.meta.hasSuggestions),
228+
Boolean(rule.meta?.fixable) && Boolean(rule.meta?.hasSuggestions),
229229
[NOTICE_TYPE.HAS_SUGGESTIONS]:
230-
Boolean(rule.meta.hasSuggestions) &&
230+
Boolean(rule.meta?.hasSuggestions) &&
231231
(!rule.meta.fixable ||
232232
!ruleDocNotices.includes(NOTICE_TYPE.FIXABLE_AND_HAS_SUGGESTIONS)),
233233

234234
[NOTICE_TYPE.REQUIRES_TYPE_CHECKING]:
235-
rule.meta.docs?.requiresTypeChecking || false,
236-
[NOTICE_TYPE.TYPE]: Boolean(rule.meta.type),
235+
rule.meta?.docs?.requiresTypeChecking || false,
236+
[NOTICE_TYPE.TYPE]: Boolean(rule.meta?.type),
237237
};
238238

239239
// Recreate object using the ordering and presence of columns specified in ruleDocNotices.
@@ -326,8 +326,8 @@ function getRuleNoticeLines(
326326
configsDisabled,
327327
configEmojis,
328328
urlConfigs,
329-
replacedBy: rule.meta.replacedBy,
330-
type: rule.meta.type as RULE_TYPE, // Convert union type to enum.
329+
replacedBy: rule.meta?.replacedBy,
330+
type: rule.meta?.type as RULE_TYPE, // Convert union type to enum.
331331
})
332332
: ruleNoticeStrOrFn
333333
);

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,29 @@ exports[`generator #generate rule with long-enough description to require name c
477477
"
478478
`;
479479

480+
exports[`generator #generate rule with no meta object generates the documentation 1`] = `
481+
"## Rules
482+
<!-- begin auto-generated rules list -->
483+
484+
✅ Enabled in the \`recommended\` configuration.
485+
486+
| Name | ✅ |
487+
| :----------------------------- | :-- |
488+
| [no-foo](docs/rules/no-foo.md) | ✅ |
489+
490+
<!-- end auto-generated rules list -->
491+
"
492+
`;
493+
494+
exports[`generator #generate rule with no meta object generates the documentation 2`] = `
495+
"# test/no-foo
496+
497+
✅ This rule is enabled in the \`recommended\` config.
498+
499+
<!-- end auto-generated rule header -->
500+
"
501+
`;
502+
480503
exports[`generator #generate rule with type, type column enabled displays the type 1`] = `
481504
"## Rules
482505
<!-- begin auto-generated rules list -->

test/lib/generator-test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,52 @@ describe('generator', function () {
22102210
});
22112211
});
22122212

2213+
describe('rule with no meta object', function () {
2214+
beforeEach(function () {
2215+
mockFs({
2216+
'package.json': JSON.stringify({
2217+
name: 'eslint-plugin-test',
2218+
main: 'index.js',
2219+
type: 'module',
2220+
}),
2221+
2222+
'index.js': `
2223+
export default {
2224+
rules: {
2225+
'no-foo': { create(context) {} },
2226+
},
2227+
configs: {
2228+
recommended: {
2229+
rules: {
2230+
'test/no-foo': 'error',
2231+
}
2232+
},
2233+
}
2234+
};`,
2235+
2236+
'README.md': '## Rules\n',
2237+
2238+
'docs/rules/no-foo.md': '',
2239+
2240+
// Needed for some of the test infrastructure to work.
2241+
node_modules: mockFs.load(
2242+
resolve(__dirname, '..', '..', 'node_modules')
2243+
),
2244+
});
2245+
});
2246+
2247+
afterEach(function () {
2248+
mockFs.restore();
2249+
jest.resetModules();
2250+
});
2251+
2252+
it('generates the documentation', async function () {
2253+
await generate('.');
2254+
expect(readFileSync('README.md', 'utf8')).toMatchSnapshot();
2255+
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
2256+
});
2257+
});
2258+
22132259
describe('with `--url-configs` option', function () {
22142260
beforeEach(function () {
22152261
mockFs({

0 commit comments

Comments
 (0)