question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: enhance `no-restricted-syntax` to support custom error messages

See original GitHub issue

What version are you using? v3.18.0

What did you do?

/* eslint no-restricted-syntax: ["error", "CallExpression[callee.name='setTimeout'][arguments.length!=2]"] */
setTimeout(() => {}, 100, 'foo');

What happened? I got the following error message:

  2:1  error  Using 'CallExpression[callee.name='setTimeout'][arguments.length!=2]' is not allowed  no-restricted-syntax

What did you expect to happen? I would like to be able to provide custom error messages for the syntax patterns I prohibit using no-restricted-syntax. For the example above:

/*
 * eslint no-restricted-syntax: [
 *     "error",
 *     {
 *         "pattern": "CallExpression[callee.name='setTimeout'][arguments.length!=2]",
 *         "message": "invoking setTimeout with more than 2 arguments is not allowed"
 *     }
 * ]
 */
setTimeout(() => {}, 100, 'foo');

Would provide the following error message:

  2:1  error  invoking setTimeout with more than 2 arguments is not allowed  no-restricted-syntax

Rationale With the release of the AST selectors feature, the no-restricted-syntax rule just got super powerful, which is awesome. However, when a complex selector is used to prohibit a certain syntax pattern, the error message can be quite cryptic for devs that are not familiar with the AST selectors.

This proposal aims to allow the customization of the error messages for each of the syntax patterns configured for no-restricted-syntax.

This proposal would be backwards compatible, since the rule logic can check if the passed-in option is a string (old format) or an object (new format). For example:

"no-restricted-syntax": [
    "error",
    "WithStatement",
    "FunctionDeclaration[params.length>3]",
    {
        "pattern": "CallExpression[callee.name='setTimeout'][arguments.length!=2]",
        "message": "invoking setTimeout with more than 2 arguments is not allowed"
    }
]    

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
not-an-aardvarkcommented, Mar 20, 2017

This is probably a record for the shortest time ever between when an issue was created and accepted. 🎉

@vitorbal Sure, go for it. I’ll assign the issue to you as a champion if you’re also implementing it.

1reaction
not-an-aardvarkcommented, Mar 20, 2017

👍

I’d been planning to suggest this as well, and I’m willing to champion it. One bikeshed is that maybe the option should be called selector instead of pattern.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-restricted-syntax - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Use eslint no-restricted-syntax to restrict library use
Unfortunately this gives me the following error message: Syntax error in selector "CallExpression[callee=MemberExpression[Identifier[name='$'], ...
Read more >
Error Messages | Help - Zoho Deluge
Deluge error messages are classified into Save Error messages and Runtime Error ... This type of errors mostly occurs due to incorrect syntax...
Read more >
Upgrade Compatibility Tool Error Messages | Adobe Commerce
This error message reference provides information about errors that can occur while executing the Upgrade Compatibility Tool.
Read more >
Windows Installer Error Messages (for Developers)
The error codes numbered greater than 2000 are internal errors and do not have authored strings, but these can occur if the installation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found