Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.2k
tools: enforce throw new Error() with lint rule#3714
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
tools/eslint-rules/new-with-error.js Outdated
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!
evanlucas commented Nov 8, 2015
LGTM |
tools/eslint-rules/new-with-error.js Outdated
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.
cjihrig commented Nov 8, 2015
LGTM. Is it possible to extend this rule to the other built in error types ( |
Fishrock123 commented Nov 8, 2015
LGTM, we should add the other error types too though. maybe including custom errors? |
cjihrig commented Nov 8, 2015
Maybe the types that the rule applies to should be a configuration option. Then you just have to check if |
Trott commented Nov 8, 2015
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. |
cjihrig commented Nov 8, 2015
I would prefer the array config solution because then the rule can be more easily extended |
mscdex commented Nov 8, 2015
Maybe an array of strings and/or regexps? I think some simple regexps (e.g. |
cjihrig commented Nov 8, 2015
@mscdex suggestion sounds like a winner to me. |
Trott commented Nov 9, 2015
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. |
Trott commented Nov 9, 2015
Pushed a new commit with the "accept an array of Error types to check" feature. PTAL |
cjihrig commented Nov 9, 2015
You're right. ESLint does the string to regex conversion instead. |
tools/eslint-rules/new-with-error.js Outdated
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?
cjihrig commented Nov 9, 2015
LGTM. I'm fine with merging the rule with its current functionality. |
jasnell commented Nov 9, 2015
LGTM |
Trott commented Nov 10, 2015
@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... |
cjihrig commented Nov 10, 2015
Trott commented Nov 10, 2015
@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()`.
Trott commented Nov 11, 2015
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/ |
jbergstroem commented Nov 11, 2015
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: nodejs#3714 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
Trott commented Nov 11, 2015
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
@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.