Skip to content

Commit

Permalink
Merge pull request #325 from nicolashenry/fix2
Browse files Browse the repository at this point in the history
feat(rule-finder): add --ext option
  • Loading branch information
ljharb authored Jun 7, 2020
2 parents c2d75fe + 2bd63ac commit 1433cce
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Then run it with: `$ npm run --silent eslint-find-option-rules` (the `--silent`

```
available options are -a|--all-available, -c|--current, -d|--deprecated, -p|--plugin, -u|--unused
available flags are -n|--no-error, --no-core, and -i/--include deprecated
available flags are -n|--no-error, --no-core, -i/--include deprecated, and --ext .js
```

By default it will error out only for `-d|--deprecated` and `-u|--unused`,
Expand All @@ -56,6 +56,8 @@ By default, core rules will be included in the output of `-a|--all-available`, `

By default, deprecated rules will be omitted from the output of `-a|--all-available`, `-p|--plugin` and `-u|--unused`. If you want to report on deprecated rules as well, use the `--include=deprecated` or `-i deprecated` flag.

By default, rules will be searched for files having `.js` extension. If you want to find rules using another extension (`.json` for example), use the `--ext .json` flag (or `--ext .js --ext .json` if you need multiple extensions).

**NOTE:** Deprecated rules are found by looking at the metadata of the rule definition. All core rules and many plugin rules use this flag to indicate deprecated rules. But if you find a plugin that does not mark their rules as deprecated in the rule metadata, please file a pull request with that project.

### Specify a file
Expand Down
7 changes: 6 additions & 1 deletion src/bin/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const argv = require('yargs')
choices: ['deprecated'],
type: 'string'
})
.option('ext', {
type: 'array',
default: ['.js']
})
.default('error', true)
.default('core', true)
.argv;
Expand All @@ -32,7 +36,8 @@ const cli = require('../lib/cli-util');
const specifiedFile = argv._[0];
const finderOptions = {
omitCore: !argv.core,
includeDeprecated: argv.include === 'deprecated'
includeDeprecated: argv.include === 'deprecated',
ext: argv.ext
};
const ruleFinder = getRuleFinder(specifiedFile, finderOptions);
const errorOut = argv.error && !argv.n;
Expand Down
23 changes: 19 additions & 4 deletions src/lib/rule-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function _getConfig(configFile, files) {
}

function _getCurrentNamesRules(config) {
return Object.keys(config.rules);
return config.rules ? Object.keys(config.rules) : [];
}

function _isDeprecated(rule) {
Expand Down Expand Up @@ -78,10 +78,25 @@ function _isNotCore(rule) {
return rule.indexOf('/') !== '-1';
}

function RuleFinder(specifiedFile, options) {
const {omitCore, includeDeprecated} = options;
function _escapeRegExp(str) {
return str.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
}

function _createExtensionRegExp(extensions) {
const normalizedExts = extensions.map(ext => _escapeRegExp(
ext.startsWith('.') ? ext.slice(1) : ext
));

return new RegExp(`.\\.(?:${normalizedExts.join("|")})$`);
}

function RuleFinder(specifiedFile, {omitCore, includeDeprecated, ext = ['.js']}) {
const configFile = _getConfigFile(specifiedFile);
const files = glob.sync('**/*.js', {dot: true, matchBase: true});

const extensionRegExp = _createExtensionRegExp(ext);
const files = glob.sync(`**/*`, {dot: true, matchBase: true})
.filter(file => extensionRegExp.test(file));

const config = _getConfig(configFile, files);
let currentRuleNames = _getCurrentNamesRules(config);
if (omitCore) {
Expand Down
17 changes: 17 additions & 0 deletions test/bin/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ describe('bin', () => {
assert.equal(exitStatus, 0);
});

it('option -a along with --ext', () => {
process.argv[2] = '-a';
process.argv[3] = '--ext .json';
proxyquire('../../src/bin/find', stub);
assert.ok(getAllAvailableRules.called);
assert.equal(exitStatus, 0);
});

it('option -a along with multi --ext', () => {
process.argv[2] = '-a';
process.argv[3] = '--ext .js';
process.argv[4] = '--ext .json';
proxyquire('../../src/bin/find', stub);
assert.ok(getAllAvailableRules.called);
assert.equal(exitStatus, 0);
});

it('option -u|--unused', () => {
process.argv[2] = '-u';
proxyquire('../../src/bin/find', stub);
Expand Down
29 changes: 29 additions & 0 deletions test/lib/rule-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,35 @@ describe('rule-finder', function() {
]);
});

it('specifiedFile (relative path) - current rules with ext', () => {
const ruleFinder = getRuleFinder(specifiedFileRelative, { ext: ['.json'] });
assertDeepEqual(ruleFinder.getCurrentRules(), [
'@scope-with-dash/foo-rule',
'@scope-with-dash/scoped-with-dash-plugin/foo-rule',
'@scope/foo-rule',
'@scope/scoped-plugin/foo-rule',
'bar-rule',
'foo-rule'
]);
});

it('specifiedFile (relative path) - current rules with ext without dot', () => {
const ruleFinder = getRuleFinder(specifiedFileRelative, { ext: ['json'] });
assertDeepEqual(ruleFinder.getCurrentRules(), [
'@scope-with-dash/foo-rule',
'@scope-with-dash/scoped-with-dash-plugin/foo-rule',
'@scope/foo-rule',
'@scope/scoped-plugin/foo-rule',
'bar-rule',
'foo-rule'
]);
});

it('specifiedFile (relative path) - current rules with ext not found', () => {
const ruleFinder = getRuleFinder(specifiedFileRelative, { ext: ['.ts'] });
assertDeepEqual(ruleFinder.getCurrentRules(), []);
});

it('specifiedFile (relative path) - current rule config', () => {
const ruleFinder = getRuleFinder(specifiedFileRelative);
assertDeepEqual(ruleFinder.getCurrentRulesDetailed(), {
Expand Down

0 comments on commit 1433cce

Please sign in to comment.