Skip to content

Commit

Permalink
Merge pull request #67 from bmish/rule-severity-array
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Oct 6, 2022
2 parents 6bbbbf1 + d1b5ebc commit 1675710
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export function hasCustomConfigs(plugin: Plugin) {
);
}

const SEVERITY_ENABLED = new Set([2, 'error']);

/**
* Get config names that a given rule belongs to.
*/
Expand All @@ -20,7 +22,13 @@ export function getConfigsForRule(
for (const configName in configsToRules) {
const rules = configsToRules[configName];
const value = rules[`${pluginPrefix}/${ruleName}`];
const isEnabled = [2, 'error'].includes(value);
const isEnabled =
((typeof value === 'string' || typeof value === 'number') &&
SEVERITY_ENABLED.has(value)) ||
(typeof value === 'object' &&
Array.isArray(value) &&
value.length > 0 &&
SEVERITY_ENABLED.has(value[0]));

if (isEnabled) {
configNames.push(configName);
Expand Down
4 changes: 3 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
};

export type Rules = Record<string, string | number>;
type RuleSeverity = 'off' | 'error' | 'warn' | 0 | 1 | 2;

export type Rules = Record<string, RuleSeverity | [RuleSeverity, unknown]>;

export type Config = {
extends?: string[];
Expand Down
48 changes: 48 additions & 0 deletions test/lib/__snapshots__/generator-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,27 @@ exports[`generator #generate only a \`recommended\` config updates the documenta
"
`;

exports[`generator #generate rule config with options generates the documentation 1`] = `
"## Rules
<!-- begin rules list -->
| Rule | Description | ✅ | 🔧 | 💡 |
| ------------------------------ | ---------------------- | --- | --- | --- |
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | ✅ | | |
<!-- end rules list -->
"
`;

exports[`generator #generate rule config with options generates the documentation 2`] = `
"# Description of no-foo (\`test/no-foo\`)
✅ This rule is enabled in the \`recommended\` config.
<!-- end rule header -->
"
`;

exports[`generator #generate rule doc without header marker but pre-existing header updates the documentation 1`] = `
"# Description (\`test/no-foo\`)
Expand All @@ -327,6 +348,33 @@ Pre-existing notice about the rule being recommended.
Details."
`;

exports[`generator #generate rules that are disabled generates the documentation 1`] = `
"## Rules
<!-- begin rules list -->
| Rule | Description | ✅ | 🔧 | 💡 |
| ------------------------------ | ---------------------- | --- | --- | --- |
| [no-bar](docs/rules/no-bar.md) | Description of no-bar. | | | |
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | | |
<!-- end rules list -->
"
`;

exports[`generator #generate rules that are disabled generates the documentation 2`] = `
"# Description of no-foo (\`test/no-foo\`)
<!-- end rule header -->
"
`;

exports[`generator #generate rules that are disabled generates the documentation 3`] = `
"# Description of no-bar (\`test/no-bar\`)
<!-- end rule header -->
"
`;

exports[`generator #generate successful updates the documentation 1`] = `
"# eslint-plugin-test
Description.
Expand Down
106 changes: 106 additions & 0 deletions test/lib/generator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,5 +1481,111 @@ describe('generator', function () {
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});

describe('rule config with options', 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 of no-foo.' }, },
create(context) {},
schema: [{ /* some options */ }]
},
},
configs: {
recommended: {
rules: {
'test/no-foo': ['error', { /* some options */ }],
}
},
}
};`,

'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('rules that are disabled', 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 of no-foo.' }, },
create(context) {},
},
'no-bar': {
meta: { docs: { description: 'Description of no-bar.' }, },
create(context) {},
},
},
configs: {
recommended: {
rules: {
'test/no-foo': 'off',
'test/no-bar': 0,
}
},
}
};`,

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

'docs/rules/no-foo.md': '',
'docs/rules/no-bar.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();
expect(readFileSync('docs/rules/no-bar.md', 'utf8')).toMatchSnapshot();
});
});
});
});

0 comments on commit 1675710

Please sign in to comment.