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

test, tools: add common.noop and common.mustNotCall lint rule #12027

Closed
wants to merge 2 commits into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Mar 24, 2017

Throughout the tests, creation of non-op functions is fairly extensive, and it is not uncommon to find common.mustCall(() => {}) or common.mustCall(function() {}), or noop = () => {}; type declarations throughout.

This PR introduces a common.noop non-op function that is used as an alternative to redeclaring nonops all the time.

The common.mustCall() method is also modified such that the fn argument defaults to common.noop if undefined, making it unnecessary to pass in a function when a nonop is needed.

There were also a couple of places where common.mustCall(fn, 0) was used to identify a function that should not be called. These are replace with common.mustNotCall() and a lint rule is added.

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

test, tools

@nodejs-github-bot nodejs-github-bot added the test Issues and PRs related to the tests. label Mar 24, 2017
@@ -0,0 +1,31 @@
/**
* @fileoverview Prefer common.mustNotCall(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Indicate what you are preferring it over. Maybe Prefer common.mustNotCall(msg) over common.mustCall(fn, 0) or whatever.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Ah, I see it's also in the message supplied to the user. Might not be terrible to put it in the comment overview too, though.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Contributor

@Fishrock123 Fishrock123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me.

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

(LGTM but I'd like more eyes, large changes XD)

if (node.callee.property &&
node.callee.property.name === 'mustCall') {
if (node.arguments.length >= 2) {
if (node.arguments[1].value == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using nested ifs instead of lots of && because it looks cleaner?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you at least break these out into one or more helper functions (isCommonMethod(), isMustCall())?

@gibfahn
Copy link
Member

gibfahn commented Mar 25, 2017

cc/ @not-an-aardvark, @silverwind, @targos for the eslint side

Copy link
Contributor

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter rule LGTM

if (node.callee.property &&
node.callee.property.name === 'mustCall') {
if (node.arguments.length >= 2) {
if (node.arguments[1].value == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you at least break these out into one or more helper functions (isCommonMethod(), isMustCall())?

@jasnell
Copy link
Member Author

jasnell commented Mar 25, 2017

@cjihrig ... updated! new and improved!

@cjihrig
Copy link
Contributor

cjihrig commented Mar 25, 2017

Beautiful. One thing I just noticed - do you think it's worth adjusting the argument checking in the lint rule, since common.mustCall(0) would be possible now too?

@jasnell
Copy link
Member Author

jasnell commented Mar 25, 2017

Good point lol

@gibfahn
Copy link
Member

gibfahn commented Mar 25, 2017

common.mustCall(0) would be possible now too?

Out of interest, is there a reason to allow common.mustCall(0)? If the linter tells you to replace that with common.mustCall() that makes sense to me.

@cjihrig
Copy link
Contributor

cjihrig commented Mar 25, 2017

The point is that common.mustCall(0) should be caught by the lint rule. It should be replaced with common.mustNotCall(). common.mustCall(0) was not a valid use of mustCall() prior to this PR.

Export a new common.noop no-operation function for general use.
Allow using common.mustCall() without a fn argument to simplify
test cases.

Replace various non-op functions throughout tests with common.noop
Prefer using `common.mustNotCall()` over `common.mustCall(fn, 0)`
@jasnell
Copy link
Member Author

jasnell commented Mar 26, 2017

@cjihrig ... updated to catch common.mustCall(0)

@jasnell
Copy link
Member Author

jasnell commented Mar 26, 2017

@cjihrig
Copy link
Contributor

cjihrig commented Mar 26, 2017

The rule LGTM. I didn't re-review everything else. I'm assuming that part didn't change.

@jasnell
Copy link
Member Author

jasnell commented Mar 26, 2017

Only CI failure is unrelated.

jasnell added a commit that referenced this pull request Mar 26, 2017
Export a new common.noop no-operation function for general use.
Allow using common.mustCall() without a fn argument to simplify
test cases.

Replace various non-op functions throughout tests with common.noop

PR-URL: #12027
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
jasnell added a commit that referenced this pull request Mar 26, 2017
Prefer using `common.mustNotCall()` over `common.mustCall(fn, 0)`

PR-URL: #12027
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
@jasnell
Copy link
Member Author

jasnell commented Mar 26, 2017

Landed in 4f2e372...20b1823

@MylesBorins
Copy link
Contributor

This will need to be manually backported to v7.x

@sam-github
Copy link
Contributor

@jasnell this needs backporting, because its absence blocks #11876 (and will probably block more tests from backporting)

@gibfahn
Copy link
Member

gibfahn commented Jun 17, 2017

@jasnell would you mind backporting to v6.x-staging?

guide.

@MylesBorins
Copy link
Contributor

ping @jasnell one more time for backport

MylesBorins pushed a commit that referenced this pull request Aug 14, 2017
Export a new common.noop no-operation function for general use.
Allow using common.mustCall() without a fn argument to simplify
test cases.

Replace various non-op functions throughout tests with common.noop

PR-URL: #12027
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
MylesBorins pushed a commit that referenced this pull request Aug 14, 2017
Prefer using `common.mustNotCall()` over `common.mustCall(fn, 0)`

PR-URL: #12027
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
MylesBorins pushed a commit that referenced this pull request Aug 16, 2017
Export a new common.noop no-operation function for general use.
Allow using common.mustCall() without a fn argument to simplify
test cases.

Replace various non-op functions throughout tests with common.noop

PR-URL: #12027
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
MylesBorins pushed a commit that referenced this pull request Aug 16, 2017
Prefer using `common.mustNotCall()` over `common.mustCall(fn, 0)`

PR-URL: #12027
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Aug 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.