GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — tools: eslint rule for assert.throws -> common.expectsError by apapirovski · Pull Request #17557 · nodejs/node

Conversation

@apapirovski

@apapirovski apapirovski commented Dec 8, 2017

Copy link
Copy Markdown
Contributor

Pretty straight-forward — eslint rule that checks for usage of assert.throws(fn, common.expectsError(err)); and suggests using common.expectsError(fn, err); instead.

First commit fixes all remaining instances that violate this rule. Second commit introduces the rule & a test for it.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

test, tools

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot nodejs-github-bot added the tools Issues and PRs related to the tools directory. label Dec 8, 2017
cjihrig
cjihrig reviewed Dec 9, 2017
View reviewed changes
Comment thread tools/eslint-rules/prefer-common-expectserror.js Outdated

@cjihrig cjihrig Dec 9, 2017

Copy link
Copy Markdown
Contributor

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.

Could no-restricted-syntax handle this?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apapirovski apapirovski Dec 9, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

Hmm, possibly but it'll definitely be a pretty convoluted rule in that case. There's no "or" for AST selectors, as far as I know, so it would get super verbose and repetitive to account for all these possibilities. Even with "or" it would be super long.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cjihrig cjihrig Dec 9, 2017

Copy link
Copy Markdown
Contributor

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.

You don't need the "or" condition since that checks for a function as the first argument to assert.throws(), which is always required. This selector seems to match all of the cases you have in this PR:

"CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<3][arguments.1.type='CallExpression'][arguments.1.callee.object.name='common'][arguments.1.callee.property.name='expectsError']"

It's not beautiful, but it might be able to be simplified further.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

apapirovski reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@apapirovski apapirovski Dec 9, 2017

Copy link
Copy Markdown
Contributor Author

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.

Good point. That should work. Will update.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Trott Trott Dec 9, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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 possible problem with no-restricted-syntax here is that this is a rule that should only apply in test. I'm not sure if there's a way to specify no-restricted-syntax in test in a way that won't override no-restricted-syntax from the config file in the parent directory. /cc @not-an-aardvark

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apapirovski apapirovski Dec 9, 2017

Copy link
Copy Markdown
Contributor Author

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.

I'm guessing that's why we also have prefer-common-mustnotcall rule.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@not-an-aardvark not-an-aardvark Dec 9, 2017

Copy link
Copy Markdown
Contributor

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.

Unfortunately, there's not a good way to do that at the moment. As a workaround, you could copy-paste the configuration from the parent directory, add the new entry, and put the resulting config in test/.eslintrc.yaml. This would have the desired effect, but it could cause the two configs to get out of sync if someone updates .eslintrc.yaml later on and forgets to update the copied version in test/.eslintrc.yaml.

There is some discussion on improving this in eslint/eslint#9192.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cjihrig cjihrig Dec 9, 2017

Copy link
Copy Markdown
Contributor

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.

That's unfortunate. I'm not a fan of adding custom rules that we don't need, but keeping the two config files in sync seems like a worse idea.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Trott

Trott commented Dec 9, 2017

Copy link
Copy Markdown
Member

This might be overly ambitious but here's an idea: Instead of forcing common.expectsError() in all of test, implement the missing functionality in assert.throws() itself and then get rid of common.expectsError() altogether.

Doesn't have to be in this PR, though, especially since it might stall due to some resistance. Can be a future enhancement.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski
apapirovski force-pushed the patch-eslint-test-throws branch from ed808a6 to 229e049 Compare December 9, 2017 04:20
@apapirovski

apapirovski commented Dec 9, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

This might be overly ambitious but here's an idea: Instead of forcing common.expectsError() in all of test, implement the missing functionality in assert.throws() itself and then get rid of common.expectsError() altogether.

I've thought about that before but is that something we even want? The Node.js core error objects are pretty specific and the functionality within common.expectsError is pretty targeted towards those. We could always make it more generic but then it would lose quite a bit of the functionality.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski
apapirovski force-pushed the patch-eslint-test-throws branch 4 times, most recently from 155251f to 849a7a1 Compare December 9, 2017 16:29
@apapirovski

apapirovski commented Dec 9, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@cjihrig PTAL — I've updated it to use AST selectors instead. Thanks for the suggestion.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

BridgeAR
BridgeAR approved these changes Dec 9, 2017
View reviewed changes
apapirovski added 2 commits December 9, 2017 11:58
@apapirovski
test: use common.expectsError
9d11b02
@apapirovski
tools: prefer common.expectsError in tests
a8e3c37
Add lint rule to validate that common.expectsError is being used
instead of assert.throws(fn, common.expectsError(err));
@apapirovski
apapirovski force-pushed the patch-eslint-test-throws branch from 849a7a1 to a8e3c37 Compare December 9, 2017 16:59
@apapirovski

apapirovski commented Dec 9, 2017

Copy link
Copy Markdown
Contributor Author

CI: https://ci.nodejs.org/job/node-test-pull-request/12004/

(had an extra space in one of the error messages, force pushed a fix)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR

BridgeAR commented Dec 9, 2017

Copy link
Copy Markdown
Member

@Trott as @apapirovski already pointed out common.expectsError is very specific for Node.js. The functionality would definitely deviate from the current implementation and it would be specific for assert.throws and assert.doesNotThrow would not work that similar anymore.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR BridgeAR mentioned this pull request Dec 10, 2017
assert: .throws accept objects #17584
Closed
4 tasks done
cjihrig
cjihrig approved these changes Dec 11, 2017
View reviewed changes
@apapirovski

apapirovski commented Dec 11, 2017

Copy link
Copy Markdown
Contributor Author

Landed in 094bfaf and e51fb90

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski apapirovski closed this Dec 11, 2017
@apapirovski
apapirovski deleted the patch-eslint-test-throws branch December 11, 2017 23:26
apapirovski added a commit that referenced this pull request Dec 11, 2017
@apapirovski
test: replace assert.throws w/ common.expectsError
094bfaf
PR-URL: #17557
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
apapirovski added a commit that referenced this pull request Dec 11, 2017
@apapirovski
tools: prefer common.expectsError in tests
e51fb90
Add lint rule to validate that common.expectsError(fn, err) is being
used instead of assert.throws(fn, common.expectsError(err));

PR-URL: #17557
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
MylesBorins pushed a commit that referenced this pull request Dec 12, 2017
@apapirovski @MylesBorins
test: replace assert.throws w/ common.expectsError
167daa7
PR-URL: #17557
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
MylesBorins pushed a commit that referenced this pull request Dec 12, 2017
@apapirovski @MylesBorins
tools: prefer common.expectsError in tests
268ce28
Add lint rule to validate that common.expectsError(fn, err) is being
used instead of assert.throws(fn, common.expectsError(err));

PR-URL: #17557
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
MylesBorins pushed a commit that referenced this pull request Dec 12, 2017
@apapirovski @MylesBorins
test: replace assert.throws w/ common.expectsError
49d6628
PR-URL: #17557
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
MylesBorins pushed a commit that referenced this pull request Dec 12, 2017
@apapirovski @MylesBorins
tools: prefer common.expectsError in tests
aa32bd0
Add lint rule to validate that common.expectsError(fn, err) is being
used instead of assert.throws(fn, common.expectsError(err));

PR-URL: #17557
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Dec 12, 2017
v9.3.0 proposal #17631
Merged
@gibfahn gibfahn added backport-requested-v6.x labels Dec 20, 2017
@gibfahn

gibfahn commented Dec 20, 2017

Copy link
Copy Markdown
Member

Could this be backported to v8.x-staging and v6.x-staging?

guide

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

BridgeAR added a commit to BridgeAR/node that referenced this pull request Dec 21, 2017
@BridgeAR
assert: .throws accept objects
2d37491
From now on it is possible to use a validation object in throws
instead of the other possibilites.

PR-URL: nodejs#17584
Refs: nodejs#17557
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ron Korving <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 8, 2018
@BridgeAR
assert: .throws accept objects
7f558d5
From now on it is possible to use a validation object in throws
instead of the other possibilites.

PR-URL: nodejs#17584
Refs: nodejs#17557
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ron Korving <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
MylesBorins pushed a commit that referenced this pull request Mar 15, 2018
@BridgeAR @MylesBorins
assert: .throws accept objects
03c17ab
From now on it is possible to use a validation object in throws
instead of the other possibilites.

Backport-PR-URL: #19230
PR-URL: #17584
Refs: #17557
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ron Korving <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
MylesBorins pushed a commit that referenced this pull request Mar 20, 2018
@BridgeAR @MylesBorins
assert: .throws accept objects
ebd60fa
From now on it is possible to use a validation object in throws
instead of the other possibilites.

Backport-PR-URL: #19230
PR-URL: #17584
Refs: #17557
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ron Korving <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Oct 2, 2018
@BridgeAR
assert: .throws accept objects
f656075
From now on it is possible to use a validation object in throws
instead of the other possibilites.

PR-URL: nodejs#17584
Refs: nodejs#17557
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ron Korving <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
MylesBorins pushed a commit that referenced this pull request Oct 31, 2018
@BridgeAR @MylesBorins
assert: .throws accept objects
f2af930
From now on it is possible to use a validation object in throws
instead of the other possibilites.

Backport-PR-URL: #23223
PR-URL: #17584
Refs: #17557
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ron Korving <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Trott Trott Trott left review comments
@cjihrig cjihrig cjihrig approved these changes
@BridgeAR BridgeAR BridgeAR approved these changes
+1 more reviewer
@not-an-aardvark not-an-aardvark not-an-aardvark left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

tools Issues and PRs related to the tools directory.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@apapirovski @Trott @BridgeAR @gibfahn @cjihrig @not-an-aardvark @nodejs-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.