-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
tools: enforce throw new Error()
with lint rule
#3714
Conversation
|
||
'ThrowStatement': function(node) { | ||
if (node.argument.type === 'CallExpression') { | ||
if (node.argument.callee.name === '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.
These can be combined into a single if
.
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.
Done! Thanks!
LGTM |
return { | ||
'ThrowStatement': function(node) { | ||
const arg = node.argument; | ||
if ((arg.type === 'CallExpression') && (arg.callee.name === '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.
Small nit, but I don't think either set of internal parens are necessary.
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.
/^[A-Z\w]*Error^/.test(arg.callee.name)
should catch all other error types.
Edit: not quite. Should be: start - optional capital followed by \w* - Error - end
, I think.
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.
@cjihrig I'll make that adjustment.
@Fishrock123 You're after this: /^([A-Z]\w*)?Error$/
That assumes FooError()
will never be the name of a function that returns an Error object and will only be a constructor for an Error object.
LGTM. Is it possible to extend this rule to the other built in error types ( |
LGTM, we should add the other error types too though. maybe including custom errors? |
Maybe the types that the rule applies to should be a configuration option. Then you just have to check if |
There are only five Error types listed in the docs so we could just hard-code those, allow them to be passed as options, or go with the regular expression solution above ( EDIT: Actually any of the three solutions seem fine to me. I don't care, as long as there's consensus from everyone else. I think any of them will work just fine for our situation. |
I would prefer the array config solution because then the rule can be more easily extended |
Maybe an array of strings and/or regexps? I think some simple regexps (e.g. |
@mscdex suggestion sounds like a winner to me. |
I like the strings and regexp idea in theory, but in practice:
I'm inclined to go with an array of strings, at least initially. |
Pushed a new commit with the "accept an array of Error types to check" feature. PTAL |
You're right. ESLint does the string to regex conversion instead. |
module.exports.schema = { | ||
'type': 'array', | ||
'items': [ | ||
{ |
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.
Can you indent this and the following two lines?
LGTM. I'm fine with merging the rule with its current functionality. |
LGTM |
@sindresorhus I proposed it but it was rejected. I guess the next best thing would be to publish as a plugin so others can use it in their own projects. I'll get on that... |
@sindresorhus A first pass is now available: https://www.npmjs.com/package/eslint-plugin-new-with-error The documentation (and probably implementation) are lacking, but feel free to open issues left and right and that should be sufficient to shame me into improving it. :-D |
In preparation for a lint rule that will enforce `throw new Error()` over `throw Error()`, fix the handful of instances in the code that use `throw Error()`.
Add linting rule requiring `throw new Error()` over `throw Error()`.
Rebased and squashed down to two commits (one for the code changes and a second for the lint rule itself). One more CI before landing: https://ci.nodejs.org/job/node-test-pull-request/698/ |
Never mind the ubuntu1410 slave fail. I removed it while your tests were running. |
In preparation for a lint rule that will enforce `throw new Error()` over `throw Error()`, fix the handful of instances in the code that use `throw Error()`. PR-URL: nodejs#3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: nodejs#3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
In preparation for a lint rule that will enforce `throw new Error()` over `throw Error()`, fix the handful of instances in the code that use `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
In preparation for a lint rule that will enforce `throw new Error()` over `throw Error()`, fix the handful of instances in the code that use `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
In preparation for a lint rule that will enforce `throw new Error()` over `throw Error()`, fix the handful of instances in the code that use `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
@evanlucas left a comment on another PR pointing out that the convention in the code base is
throw new Error()
overthrow Error().
This PR changes the five instances ofthrow Error()
in the code base and enforces it with a linting rule.