Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add importNames support for patterns in no-restricted-imports #16059

Merged
merged 7 commits into from Jul 2, 2022
12 changes: 12 additions & 0 deletions docs/src/rules/no-restricted-imports.md
Expand Up @@ -109,6 +109,18 @@ Pattern matches can also be configured to be case-sensitive:
}]
```

Pattern matches can restrict specific import names only, similar to the `paths` option:

```json
"no-restricted-imports": ["error", {
"patterns": [{
"group": ["utils/*"],
"importNames": ["isEmpty"],
"message": "Use 'isEmpty' from lodash instead."
}]
}]
```
brandongregoryscott marked this conversation as resolved.
Show resolved Hide resolved

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

```json
Expand Down
66 changes: 55 additions & 11 deletions lib/rules/no-restricted-imports.js
Expand Up @@ -58,6 +58,12 @@ const arrayOfStringsOrObjectPatterns = {
items: {
type: "object",
properties: {
importNames: {
type: "array",
items: {
type: "string"
}
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
},
group: {
type: "array",
items: {
Expand Down Expand Up @@ -102,6 +108,10 @@ module.exports = {
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
patternWithCustomMessage: "'{{importSource}}' import is restricted from being used by a pattern. {{customMessage}}",

patternAndImportName: "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern.",
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
patternAndImportNameWithCustomMessage: "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}",

everything: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.",
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
everythingWithCustomMessage: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}",
Expand Down Expand Up @@ -159,9 +169,10 @@ module.exports = {
}

// relative paths are supported for this rule
const restrictedPatternGroups = restrictedPatterns.map(({ group, message, caseSensitive }) => ({
const restrictedPatternGroups = restrictedPatterns.map(({ group, message, caseSensitive, importNames }) => ({
matcher: ignore({ allowRelativePaths: true, ignorecase: !caseSensitive }).add(group),
customMessage: message
customMessage: message,
importNames
}));

// if no imports are restricted we don't need to check
Expand Down Expand Up @@ -234,20 +245,53 @@ module.exports = {
/**
* Report a restricted path specifically for patterns.
* @param {node} node representing the restricted path reference
* @param {Object} group contains a Ignore instance for paths, and the customMessage to show if it fails
* @param {Object} group contains an Ignore instance for paths, the customMessage to show on failure,
* and any restricted import names that have been specified in the config
* @param {Map<string,Object[]>} importNames Map of import names that are being imported
* @returns {void}
* @private
*/
function reportPathForPatterns(node, group) {
function reportPathForPatterns(node, group, importNames) {
const importSource = node.source.value.trim();

context.report({
node,
messageId: group.customMessage ? "patternWithCustomMessage" : "patterns",
data: {
importSource,
customMessage: group.customMessage
const customMessage = group.customMessage;
const restrictedImportNames = group.importNames || [];

/*
* If we are not restricting to any specific import names and just the pattern itself,
* report the error and move on
*/
if (restrictedImportNames.length < 1 || importNames.has("*")) {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
context.report({
node,
messageId: customMessage ? "patternWithCustomMessage" : "patterns",
data: {
importSource,
customMessage
}
});
return;
}

restrictedImportNames.forEach(importName => {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
if (!importNames.has(importName)) {
return;
}

const specifiers = importNames.get(importName);

specifiers.forEach(specifier => {
context.report({
node,
messageId: customMessage ? "patternAndImportNameWithCustomMessage" : "patternAndImportName",
loc: specifier.loc,
data: {
importSource,
customMessage,
importName
}
});
});
});
}

Expand Down Expand Up @@ -304,7 +348,7 @@ module.exports = {
checkRestrictedPathAndReport(importSource, importNames, node);
restrictedPatternGroups.forEach(group => {
if (isRestrictedPattern(importSource, group)) {
reportPathForPatterns(node, group);
reportPathForPatterns(node, group, importNames);
}
});
}
Expand Down
75 changes: 75 additions & 0 deletions tests/lib/rules/no-restricted-imports.js
Expand Up @@ -262,6 +262,26 @@ ruleTester.run("no-restricted-imports", rule, {
importNames: ["DisallowedObject"]
}]
}]
},
{
code: "import { Bar } from '../../my/relative-module';",
options: [{
patterns: [{
group: ["**/my/relative-module"],
importNames: ["Foo"]
}]
}]
},
{

// Default import should not be reported - just drop the importNames option for this behavior
code: "import Foo from '../../my/relative-module';",
options: [{
patterns: [{
group: ["**/my/relative-module"],
importNames: ["Foo"]
}]
}]
}
],
invalid: [{
Expand Down Expand Up @@ -1094,6 +1114,61 @@ ruleTester.run("no-restricted-imports", rule, {
column: 1,
endColumn: 45
}]
},
{
code: "import { Foo } from '../../my/relative-module';",
options: [{
patterns: [{
group: ["**/my/relative-module"],
importNames: ["Foo"]
}]
}],
errors: [{
type: "ImportDeclaration",
line: 1,
column: 10,
endColumn: 13
}]
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
},
{
code: "import { Foo, Bar } from '../../my/relative-module';",
options: [{
patterns: [{
group: ["**/my/relative-module"],
importNames: ["Foo", "Bar"],
message: "Import from @/utils instead."
}]
}],
errors: [{
type: "ImportDeclaration",
line: 1,
column: 10,
endColumn: 13,
message: "'Foo' import from '../../my/relative-module' is restricted from being used by a pattern. Import from @/utils instead."
}, {
type: "ImportDeclaration",
line: 1,
column: 15,
endColumn: 18,
message: "'Bar' import from '../../my/relative-module' is restricted from being used by a pattern. Import from @/utils instead."
}]
},
{

// Star import should be reported for consistency with `paths` option (see: https://github.com/eslint/eslint/pull/16059#discussion_r908749964)
code: "import * as Foo from '../../my/relative-module';",
options: [{
patterns: [{
group: ["**/my/relative-module"],
importNames: ["Foo"]
}]
}],
errors: [{
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 49
}]
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
]
});