Skip to content

Commit

Permalink
Remove regular expression support from no-extraneous-dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
knpwrs committed Aug 29, 2016
1 parent 8ff322d commit 8d16c08
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 66 deletions.
18 changes: 3 additions & 15 deletions docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,13 @@ You can also use globs instead of literal booleans:
"import/no-extraneous-dependencies": ["error", {"devDependencies": "*.test.js"}]
```

You can also use regular expressions instead of literal booleans or globs (note: the string must begin and end with `/`):
When using a glob, the setting will be activated if the name of the file being linted matches the given glob. In addition, you can use an array to specify multiple globs:

```js
"import/no-extraneous-dependencies": ["error", {"devDependencies": "/test|spec/"}]
"import/no-extraneous-dependencies": ["error", {"devDependencies": ['*.test.js', '*.spec.js']}]
```

If you are using JavaScript configuration (e.g., `.eslintrc.js`), then you can use a RegExp literal instead of a string:

```js
"import/no-extraneous-dependencies": ["error", {"devDependencies": /test|spec/}]
```

When using a glob or regular expression, the setting will be activated if the name of the file being linted matches the given glob or regular expression. In addition, you can use an array to specify multiple globs or regular expressions:

```js
"import/no-extraneous-dependencies": ["error", {"devDependencies": [/test/, '/spec/', '*.test.js', '*.spec.js']}]
```

When using an array of globs or regular expressions, the setting will be activated if the name of the file being linted matches a single glob or regular expression in the array.
When using an array of globs, the setting will be activated if the name of the file being linted matches a single glob in the array.

## Rule Details

Expand Down
21 changes: 4 additions & 17 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,7 @@ function testConfig(config, filename) {
return config
}
// Account for the possibility of an array for multiple configuration
return [].concat(config).some(c => {
if (typeof c === 'object') {
// `c` is a literal RegExp
return c.test(filename)
}
// By this point `c` must be a string.
if (/^\/.+\/$/.test(c)) {
// `c` is a string representation of a RegExp.
const pattern = new RegExp(c.substring(1, c.length - 1))
return pattern.test(filename)
}
// `c` must be a string representing a glob
return minimatch(filename, c)
})
return [].concat(config).some(c => minimatch(filename, c))
}

module.exports = function (context) {
Expand Down Expand Up @@ -126,9 +113,9 @@ module.exports.schema = [
{
'type': 'object',
'properties': {
'devDependencies': { 'type': ['boolean', 'string', 'object', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'string', 'object', 'array'] },
'peerDependencies': { 'type': ['boolean', 'string', 'object', 'array'] },
'devDependencies': { 'type': ['boolean', 'string', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'string', 'array'] },
'peerDependencies': { 'type': ['boolean', 'string', 'array'] },
},
'additionalProperties': false,
},
Expand Down
35 changes: 1 addition & 34 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ ruleTester.run('no-extraneous-dependencies', rule, {
code: 'import "importType"',
settings: { 'import/resolver': { node: { paths: [ path.join(__dirname, '../../files') ] } } },
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: /test|spec/}],
filename: 'foo.test.js',
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: '/test|spec/'}],
filename: 'foo.spec.js',
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: '*.spec.js'}],
Expand All @@ -55,11 +45,6 @@ ruleTester.run('no-extraneous-dependencies', rule, {
options: [{devDependencies: ['*.test.js', '*.spec.js']}],
filename: 'foo.spec.js',
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: [/\.test\.js$/, /\.spec\.js$/]}],
filename: 'foo.spec.js',
}),
],
invalid: [
test({
Expand Down Expand Up @@ -114,24 +99,6 @@ ruleTester.run('no-extraneous-dependencies', rule, {
message: '\'glob\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: /test|spec/}],
filename: 'foo.tes.js',
errors: [{
ruleId: 'no-extraneous-dependencies',
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: '/test|spec/'}],
filename: 'foo.tes.js',
errors: [{
ruleId: 'no-extraneous-dependencies',
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: '*.test.js'}],
Expand All @@ -143,7 +110,7 @@ ruleTester.run('no-extraneous-dependencies', rule, {
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: ['*.test.js', /test\.js$/, '/test/']}],
options: [{devDependencies: ['*.test.js', '*.spec.js']}],
filename: 'foo.tes.js',
errors: [{
ruleId: 'no-extraneous-dependencies',
Expand Down

0 comments on commit 8d16c08

Please sign in to comment.