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

Add support for custom messages to restricted imports #9413

Conversation

majapw
Copy link
Contributor

@majapw majapw commented Oct 12, 2017

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 and no-restricted-modules (am interested in feedback first before making the changes to no-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 and message key where the name corresponds to a path and the message 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:

"no-restricted-imports": ["error", [{
  "name": "import-foo",
  "message": "Please use import-bar instead."
}]]

or like this:

"no-restricted-imports": ["error", {
  "paths": [{
    "name": "import-foo",
    "message": "Please use import-bar instead."
  }]
}]

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

@jsf-clabot
Copy link

jsf-clabot commented Oct 12, 2017

CLA assistant check
All committers have signed the CLA.

@eslintbot
Copy link

Thanks for the pull request, @majapw! I took a look to make sure it's ready for merging and found some changes are needed:

  • The commit summary needs to begin with a tag (such as Fix: or Update:). Please check out our guide for how to properly format your commit summary and update it on this pull request.

Can you please update the pull request to address these?

(More information can be found in our pull request guide.)

@majapw majapw force-pushed the maja-add-custom-message-to-no-restricted-imports branch from 870c505 to d99da30 Compare October 12, 2017 00:58
@eslintbot
Copy link

LGTM

Copy link
Sponsor Contributor

@ljharb ljharb left a 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!

@not-an-aardvark
Copy link
Member

Thanks for the PR. Have you seen #8400? It looks like a similar proposal was accepted to add both paths and patterns options to no-restricted-imports, to allow for custom messages to be used.

I think it would be fine to just add the paths option in this PR, but I want to make sure this PR is compatible with the plan in #8400 so that we would still be able to add patterns later.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Oct 12, 2017

The plan for "paths" is identical in this PR to #8400; I definitely think this could be merged right now, and in no way would it block #8400.

Copy link
Member

@ilyavolodin ilyavolodin left a 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.

Copy link
Member

@not-an-aardvark not-an-aardvark left a 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
Copy link
Member

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.)

Copy link
Sponsor Contributor

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?

Copy link
Sponsor Contributor

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)

Copy link
Member

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).

* @private
*/
function isRestrictedPath(name) {
return restrictedPathMessages.hasOwnProperty(name);
Copy link
Member

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).

@majapw majapw force-pushed the maja-add-custom-message-to-no-restricted-imports branch from d99da30 to fa9567d Compare October 12, 2017 17:34
@eslintbot
Copy link

LGTM

@majapw
Copy link
Contributor Author

majapw commented Oct 12, 2017

@not-an-aardvark I think I've addressed your concerns (hasOwnProperty on the restrictedPathMessages object and add a minLength to the schema for the custom message). I've also duplicated the effort for no-restricted-modules. Please take another look!

Copy link
Sponsor Contributor

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ノ◕ヮ◕)ノ*:・゚✧

Copy link
Member

@not-an-aardvark not-an-aardvark left a 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.

You may also specify a custom message for any paths you want to restrict as follows:

```json
"no-restricted-imports": ["error", [{
Copy link
Member

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.

Copy link
Contributor Author

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

@majapw majapw force-pushed the maja-add-custom-message-to-no-restricted-imports branch from fa9567d to 7de695d Compare October 12, 2017 23:17
@eslintbot
Copy link

LGTM

@majapw
Copy link
Contributor Author

majapw commented Oct 12, 2017

@not-an-aardvark addressed!

Copy link
Member

@not-an-aardvark not-an-aardvark left a 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!

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Oct 12, 2017
Copy link
Member

@platinumazure platinumazure left a 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!

}]
```

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.
Copy link
Member

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."

}]
```

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.
Copy link
Member

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."

}

/**
* Check if the given name is a restricted global name.
Copy link
Member

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)

}

/**
* Check if the given name is a restricted global name.
Copy link
Member

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)

Copy link
Member

@kaicataldo kaicataldo left a 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!

@kaicataldo
Copy link
Member

kaicataldo commented Oct 13, 2017

Let's make sure to update the corresponding issue after this lands so that it's clear that only custom messages for patterns are needed.

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.
@majapw majapw force-pushed the maja-add-custom-message-to-no-restricted-imports branch from 7de695d to 943964a Compare October 13, 2017 23:41
@eslintbot
Copy link

LGTM

@majapw
Copy link
Contributor Author

majapw commented Oct 13, 2017

@platinumazure I believe I've addressed your comments as well.

Copy link
Member

@platinumazure platinumazure left a 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!

@kaicataldo kaicataldo merged commit b02fbb6 into eslint:master Oct 14, 2017
@kaicataldo
Copy link
Member

Thanks for contributing to ESLint!

@majapw majapw deleted the maja-add-custom-message-to-no-restricted-imports branch October 16, 2017 15:49
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Apr 13, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Apr 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants