-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Add support for custom messages to restricted imports #9413
Add support for custom messages to restricted imports #9413
Conversation
Thanks for the pull request, @majapw! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
870c505
to
d99da30
Compare
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This follows the precedent from no-restricted-properties
, and is a great change!
Thanks for the PR. Have you seen #8400? It looks like a similar proposal was accepted to add both I think it would be fine to just add the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please sign our CLA? Unfortunately, we can't merge anything without signed CLA.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good aside from a couple of minor edge-cases.
function reportPath(node) { | ||
const importName = node.source.value.trim(); | ||
const customMessage = restrictedPathMessages[importName]; | ||
const message = customMessage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause the default message to get used if an empty string gets used as the custom message, which I'm assuming is unintended. (It's unlikely that users will provide an empty string very often, but I think it would be better to handle this case consistently with the case where non-empty strings are provided.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could that be resolved by modifying the JSON schema to forbid an empty message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(specifically, by adding minLength: 1
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would also work. I don't have any strong preferences for how empty strings are handled; I just want to make sure that the behavior is consistent with the rest of the rule (or that if the behavior is inconsistent, it's a design choice rather than a bug).
lib/rules/no-restricted-imports.js
Outdated
* @private | ||
*/ | ||
function isRestrictedPath(name) { | ||
return restrictedPathMessages.hasOwnProperty(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will cause an error to get thrown if someone uses the string "hasOwnProperty"
as a path. It would be better to use something like Object.prototype.hasOwnProperty.call(restrictedPathMessages, name)
.
d99da30
to
fa9567d
Compare
LGTM |
@not-an-aardvark I think I've addressed your concerns ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(ノ◕ヮ◕)ノ*:・゚✧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good aside from a typo in the documentation.
docs/rules/no-restricted-modules.md
Outdated
You may also specify a custom message for any paths you want to restrict as follows: | ||
|
||
```json | ||
"no-restricted-imports": ["error", [{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these examples should say no-restricted-modules
rather than no-restricted-imports
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh whoops! My copy-pasta bad
fa9567d
to
7de695d
Compare
LGTM |
@not-an-aardvark addressed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Thanks for contributing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just requesting some minor changes around doc typos and comment inconsistencies, otherwise looks great! Thanks for contributing!
docs/rules/no-restricted-imports.md
Outdated
}] | ||
``` | ||
|
||
The custom message will be appends to the default error message. Please note that you may not specify custom error messages for restricted patterns as a particular import may match more than one pattern. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should say "The custom message will be appended to the default error message."
docs/rules/no-restricted-modules.md
Outdated
}] | ||
``` | ||
|
||
The custom message will be appends to the default error message. Please note that you may not specify custom error messages for restricted patterns as a particular module may match more than one pattern. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should say "The custom message will be appended to the default error message."
lib/rules/no-restricted-imports.js
Outdated
} | ||
|
||
/** | ||
* Check if the given name is a restricted global name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this comment be edited? (This seems to have been copied from no-restricted-globals)
lib/rules/no-restricted-modules.js
Outdated
} | ||
|
||
/** | ||
* Check if the given name is a restricted global name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this comment be edited? (This seems to have been copied from no-restricted-globals)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from the few suggestions by @platinumazure, this LGTM. Thanks for contributing!
Let's make sure to update the corresponding issue after this lands so that it's clear that only custom messages for |
Following the precedent set by no-restricted-globals, anywhere you can pass a path string, you can also pass an object with a "name" and "message" key. This custom message will be appended to any error message triggered by an import of the "name" value. Custom messages are not supported with patterns.
7de695d
to
943964a
Compare
LGTM |
@platinumazure I believe I've addressed your comments as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for contributing!
Thanks for contributing to ESLint! |
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What rule do you want to change?
no-restricted-imports
andno-restricted-modules
(am interested in feedback first before making the changes tono-restricted-modules
as well)Does this change cause the rule to produce more or fewer warnings?
Same amount of errors but adds more nuance to the error messaging
How will the change be implemented? (New option, new default behavior, etc.)?
Modification to allowed options. Anywhere you can pass in a path string, you can now also pass in an object with a
name
andmessage
key where thename
corresponds to a path and themessage
is the custom message you want appended to the default error message.Please provide some example code that this change will affect:
All existing configurations will continue to work. However, the following will also be accepted:
or like this:
What does the rule currently do for this code?
errors on any restricted imports that are passed into the options.
What will the rule do after it's changed?
continue to error on any restricted imports that are passed into the options, but with more clarity on what the developer should do instead.
What changes did you make? (Give an overview)
Following the precedent set by no-restricted-globals, anywhere you can pass a path string, you can also pass an object with a "name" and "message" key. This custom message will be appended to any error message triggered by an import of the "name" value. Custom messages are not supported with patterns.
Is there anything you'd like reviewers to focus on?
See if this pattern (and naming scheme) is acceptable so as to do the same for
no-restricted-modules
FYI @ljharb @yzimet