Skip to content

Commit

Permalink
fix: no-restricted-imports support casing
Browse files Browse the repository at this point in the history
Path matching in `.gitignore` can be case-sensitive, and
ignore@5.2.0 supports this as well as maintains support for
relative paths, whose support was dropped after 4.0.6 but
restored in 5.2.0 (this was a blocker for past upgrades).

By default, this rule remains case-insensitive to maintain
backwards compatibility with earlier versions. Only by explicitly
specifying it in a custom pattern object can it be triggered.
  • Loading branch information
gfyoung committed Dec 28, 2021
1 parent 51c37b1 commit 4e57bc9
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
29 changes: 29 additions & 0 deletions docs/rules/no-restricted-imports.md
Expand Up @@ -91,6 +91,17 @@ or like this if you want to apply a custom message to pattern matches:

The custom message will be appended to the default error message.

Pattern matches can also be customized to be case-sensitive:

```json
"no-restricted-imports": ["error", {
"patterns": [{
"group": ["import1/private/prefix[A-Z]*"],
"caseSensitive": true
}]
}]
```

To restrict the use of all Node.js core imports (via <https://github.com/nodejs/node/tree/master/lib>):

```json
Expand Down Expand Up @@ -172,6 +183,15 @@ import * as Foo from "foo";
import pick from 'lodash/pick';
```

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
group: ["foo[A-Z]*"],
caseSensitive: true
}]}]*/

import pick from 'fooBar';
```

Examples of **correct** code for this rule:

```js
Expand Down Expand Up @@ -214,6 +234,15 @@ import { AllowedObject as DisallowedObject } from "foo";
import lodash from 'lodash';
```

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
group: ["foo[A-Z]*"],
caseSensitive: true
}]}]*/

import pick from 'food';
```

## When Not To Use It

Don't use this rule or don't include a module in the list for this rule if you want to be able to import a module in your project without an ESLint error or warning.
9 changes: 7 additions & 2 deletions lib/rules/no-restricted-imports.js
Expand Up @@ -8,7 +8,7 @@
// Rule Definition
//------------------------------------------------------------------------------

const ignore = require("ignore");
const ignore = require("../shared/ignore");

const arrayOfStringsOrObjects = {
type: "array",
Expand Down Expand Up @@ -63,6 +63,9 @@ const arrayOfStringsOrObjectPatterns = {
message: {
type: "string",
minLength: 1
},
caseSensitive: {
type: "boolean"
}
},
additionalProperties: false,
Expand Down Expand Up @@ -145,7 +148,9 @@ module.exports = {
const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || [];
const restrictedPatternGroups = restrictedPatterns.length > 0 && typeof restrictedPatterns[0] === "string"
? [{ matcher: ignore().add(restrictedPatterns) }]
: restrictedPatterns.map(({ group, message }) => ({ matcher: ignore().add(group), customMessage: message }));
: restrictedPatterns.map(({ group, message, caseSensitive }) => ({
matcher: ignore({ ignorecase: !caseSensitive }).add(group), customMessage: message
}));

// if no imports are restricted we don't need to check
if (Object.keys(restrictedPaths).length === 0 && restrictedPatternGroups.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-restricted-modules.js
Expand Up @@ -9,7 +9,7 @@
// Rule Definition
//------------------------------------------------------------------------------

const ignore = require("ignore");
const ignore = require("../shared/ignore");

const arrayOfStrings = {
type: "array",
Expand Down
24 changes: 24 additions & 0 deletions lib/shared/ignore.js
@@ -0,0 +1,24 @@
/**
* @fileoverview Create instance of Ignore validator.
* @author gfyoung
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const ignoreFactory = require("ignore");

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = (additionalOptions = {}) => {
const ignore = ignoreFactory({
allowRelativePaths: true,
...additionalOptions
});

return ignore;
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -67,7 +67,7 @@
"functional-red-black-tree": "^1.0.1",
"glob-parent": "^6.0.1",
"globals": "^13.6.0",
"ignore": "^4.0.6",
"ignore": "^5.2.0",
"import-fresh": "^3.0.0",
"imurmurhash": "^0.1.4",
"is-glob": "^4.0.0",
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/no-restricted-imports.js
Expand Up @@ -54,6 +54,16 @@ ruleTester.run("no-restricted-imports", rule, {
code: "import withPatterns from \"foo/bar\";",
options: [{ patterns: [{ group: ["foo/*", "!foo/bar"], message: "foo is forbidden, use bar instead" }] }]
},
{
code: "import withPatternsCaseSensitive from 'foo';",
options: [{
patterns: [{
group: ["FOO"],
message: "foo is forbidden, use bar instead",
caseSensitive: true
}]
}]
},
{
code: "import AllowedObject from \"foo\";",
options: [{
Expand Down Expand Up @@ -288,6 +298,16 @@ ruleTester.run("no-restricted-imports", rule, {
column: 1,
endColumn: 36
}]
}, {
code: "import withPatternsCaseInsensitive from 'foo';",
options: [{ patterns: [{ group: ["FOO"] }] }],
errors: [{
message: "'foo' import is restricted from being used by a pattern.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 47
}]
}, {
code: "import withGitignores from \"foo/bar\";",
options: [{ patterns: ["foo/*", "!foo/baz"] }],
Expand Down

0 comments on commit 4e57bc9

Please sign in to comment.