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

Proposal: support custom message for all no-restricted-* rules and possibly others #8400

Closed
dwelle opened this issue Apr 3, 2017 · 16 comments
Assignees
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 help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules

Comments

@dwelle
Copy link
Contributor

dwelle commented Apr 3, 2017

The basic idea is to allow custom message for rules which may not be self-evident and may demand a description of why they exist.

Semi-duplicate of #8315, but I'm opening a new issue to suggest this for (IMO) all the rules that really need this, and to discuss what other rules we could implement this for.

I think all no-restricted* rules need this. no-restricted-syntax and no-restricted-properties rules already have this, but these don't:

  • no-restricted-modules
  • no-restricted-globals
  • no-restricted-imports

Syntax is comparative to the rules where it's already implemented, but the naming of the keys is up for bikeshed. Proposals:

  • no-restricted-globals - either a name, variable or identifier for the main key:

    /*
     * eslint no-restricted-globals: [
     *     "error",
     *     {
     *         "name": "self",
     *         "message": "some explanation"
     *     }
     * ]
     */
    
  • no-restricted-modules && no-restricted-imports

    /*
     * eslint no-restricted-modules: [
     *     "error",
     *     {
     *         "name": "myModule",
     *         "message": "some explanation"
     *     }
     * ]
     */
    
    // and
    
    /*
     * eslint no-restricted-modules: [
     *     "error",
     *     {
     *         "paths": [{ "path": "myModule", "message": "some message" }],
     *         "patterns": [{ "pattern": "import2/*", "message": "some message" }]
     *     }
     * ]
     */
    

It may be a good idea to implement custom messages for other rules, as well. Thoughts?

@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Apr 3, 2017
@dwelle dwelle changed the title Proposal: support custom message for all no-restricted-* and possibly others Proposal: support custom message for all no-restricted-* rules and possibly others Apr 3, 2017
@not-an-aardvark
Copy link
Member

Thanks for the proposal. This seems like a good idea to me. 👍

@not-an-aardvark not-an-aardvark added enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Apr 4, 2017
@not-an-aardvark
Copy link
Member

@eslint/eslint-team This issue has three 👍s -- it just needs a champion to be accepted.

@vitorbal
Copy link
Member

Should #8315 be closed in favor of this once it gets accepted?

@not-an-aardvark
Copy link
Member

Yes, I think that sounds reasonable.

@kaicataldo
Copy link
Member

There's already a PR for this #8315: #8932

I think it's totally fine to do this in multiple PRs, just wanted to raise awareness so that we can ensure all the APIs match for clarity.

@platinumazure
Copy link
Member

platinumazure commented Jul 13, 2017

I'll champion. Marking as accepted.

I think I like a name key better for no-restricted-globals, so I'll update that in my PR at some point.

@platinumazure platinumazure self-assigned this Jul 13, 2017
@platinumazure platinumazure added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jul 13, 2017
@platinumazure
Copy link
Member

platinumazure commented Sep 21, 2017

Okay, finally coming back around to this.

Current status:

  • no-restricted-imports still needs to be augmented to support custom messages
  • no-restricted-modules still needs to be augmented to support custom messages

It's not immediately clear how we should add custom message support for patterns. @ljharb Any ideas on that?

PRs would be very welcome!

@platinumazure platinumazure added the help wanted The team would welcome a contribution from the community for this issue label Sep 21, 2017
@not-an-aardvark
Copy link
Member

It's not immediately clear how we should add custom message support for patterns.

What do you mean by "patterns"?

@ljharb
Copy link
Sponsor Contributor

ljharb commented Sep 22, 2017

@platinumazure the OP seems to have that answered for patterns; can you elaborate on the confusion?

@dwelle
Copy link
Contributor Author

dwelle commented Sep 22, 2017

Not sure if I can elaborate on the patterns. Is this a question for implem or API?

@platinumazure
Copy link
Member

I'm having trouble with it conceptually since patterns allow for negation which represent patterns that should specifically not be flagged by the rule.

Take this example: no-restricted-imports: ["error", { patterns: ["lib/vendor/**", "!lib/vendor/conf.json", "foo/**"] }]

Seems the first two patterns are related and should have one message between them, ideally (the second pattern is sort of a modification of the first, really). The last gets its own message. Or, if we really want to do one message per pattern, the second one doesn't need a message because it's not actually forbidding anything.

So when implementing a { pattern, message } block in the schema, should that actually be { patterns, message } so we can encourage users to group their patterns that are logically connected? Or can we have one pattern string with multiple patterns (e.g., using comma delimiting)? I'm just not sure what the best way to handle it is.

Similar questions arise for no-restricted-modules.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Sep 22, 2017

That's a good point. I'd say grouping patterns with a message in an object, and allowing multiple objects, makes sense.

@dwelle
Copy link
Contributor Author

dwelle commented Sep 23, 2017

patterns: [{ patterns, message }, { patterns, message }] seems good to me.

So, for it to not be a breaking change, we need to allow mixed objects and strings in the top-level patterns, I suppose: (patterns: [{ patterns, message }, "path/to/module"].

@platinumazure
Copy link
Member

@dwelle Thanks, I agree. I think I'll support a top-level paths and patterns array (similar to current schema) and then each item in the array can be a single string pattern/path (with no message), or an object with paths/patterns and a message... I'll work on a PR today and we'll see what people think.

kaicataldo pushed a commit that referenced this issue Oct 14, 2017
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.
@kaicataldo
Copy link
Member

kaicataldo commented Oct 14, 2017

Support for custom messages in paths has landed as of #9413. We still need to implement custom messages for patterns for no-restricted-modules and no-restricted-imports.

@platinumazure
Copy link
Member

I'm going to close this and create a new issue for whether we need to support custom messages for patterns in no-restricted-modules and no-restricted-imports. If users don't seem to need them, then we can regard this as solved (until users start needing them).

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jul 15, 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 Jul 15, 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 help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

7 participants