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

tools: enable (sensible) additional eslint rules #16243

Closed

Conversation

apapirovski
Copy link
Member

@apapirovski apapirovski commented Oct 16, 2017

This PR proposes enabling some additional rules that the project either already follows or probably should follow. Here’s the full overview:

no-lonely-if

If an if statement is the only statement in the else block, it is often clearer to use an else if form.

This one required some fix ups in existing code but IMO it's a good rule to have. Often these end up being a result of refactoring where the maintainer doesn't spot that the inner conditional block can be pulled out into an else if. (I personally had this come up on a recent PR.)

https://eslint.org/docs/rules/no-lonely-if

for-direction

for loop with a stop condition that can never be reached, such as one with a counter that moves in the wrong direction, will run infinitely. While there are occasions when an infinite loop is intended, the convention is to construct such loops as while loops. More typically, an infinite for loop is a bug.

I don't think this is particularly controversial. If someone tries to write code that does go against this rule they should probably refactor or it'll be hard/impossible to follow in the future.

https://eslint.org/docs/rules/for-direction

accessor-pairs

It’s a common mistake in JavaScript to create an object with just a setter for a property but never have a corresponding getter defined for it. Without a getter, you cannot read the property, so it ends up not being used.

Again, not something that's an issue currently and if someone does do this, it's very likely an error or they should just go ahead and override.

https://eslint.org/docs/rules/accessor-pairs

symbol-description

This rule requires a description when creating symbols

This one is only enabled for lib since I don't think we particularly care about this for doc or test. We already follow it but will be good to have for any potential future contributors.

https://eslint.org/docs/rules/symbol-description

(Side-note: is this tools, build or something else? Had no clue what to label the commit with.)

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

doc, lib, test, tools

@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Oct 16, 2017
@@ -1149,3 +1151,4 @@ if (typeof Symbol !== 'undefined') {
}

assert.doesNotThrow(() => util.inspect(process));
/* eslint-enable accessor-pairs */
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure, but this line seems not needed since there is no code left for linting.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that was my thinking but I've noticed a bunch of our test files do this so I just followed the convention.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, maybe this is some over-caution for possible future code addition?

Copy link
Member Author

Choose a reason for hiding this comment

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

Possibly. If someone does know, I would love to know the reason. Or maybe we're all just following the lead of the first person that did this... 😂

Copy link
Member

Choose a reason for hiding this comment

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

I think the main reason is to try to limit the deactivation for any rule to the minimum required lines.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, in many tests such enabling comments are inserted in the middle of the code. But as the last line, they may be a bit confusing. However, they can have the same role as trailing commas in arrays and objects: safety device for future additions.

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Oct 16, 2017

A memo for a lander: all the commits may need to be squashed before landing to be self-sufficing.

@vsemozhetbyt
Copy link
Contributor

@@ -26,6 +26,8 @@ const JSStream = process.binding('js_stream').JSStream;
const util = require('util');
const vm = require('vm');

/* eslint-disable accessor-pairs */
Copy link
Contributor

Choose a reason for hiding this comment

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

Still a good idea to comment why:

// this test uses unmatched accessors to try and trip up `inspect`

@refack
Copy link
Contributor

refack commented Oct 16, 2017

(Side-note: is this tools, build or something else? Had no clue what to label the commit with.)

AFAICT we mark linting changes as tools

// userland ones. NEVER DO THIS. This is here only because this code needs
// to continue to work with older versions of Node.js that do not include
// the prependListener() method. The goal is to eventually remove this hack.
else if (!emitter._events || !emitter._events[event])
Copy link
Member

Choose a reason for hiding this comment

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

Nit: this could be an if instead of else if as there is a return statement in the previous if body.

Copy link
Member Author

@apapirovski apapirovski Oct 17, 2017

Choose a reason for hiding this comment

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

Nice catch! I'll update this later today.

Funny enough, there's actually a rule I want to introduce that deals with that but it requires changing more files (around 80 total instances, can't recall how many files) so I wanted to keep it for a separate PR in case there was push back.

Copy link
Member Author

Choose a reason for hiding this comment

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

Force pushed to address this.

Copy link
Contributor

Choose a reason for hiding this comment

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

May be a case for Code And Learn (nodejs/code-and-learn#72 (comment))

Enable additional rules that node either already adheres to
or it makes sense to do so going forward: for-direction,
accessor-pairs, no-lonely-if and symbol-description.

Refs: https://eslint.org/docs/rules/for-direction
Refs: https://eslint.org/docs/rules/accessor-pairs
Refs: https://eslint.org/docs/rules/no-lonely-if
Refs: https://eslint.org/docs/rules/symbol-description
@@ -128,16 +128,14 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
if (isWs)
continue;
lastPos = start = i;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO this spot makes more sense without the change, fwiw

apapirovski added a commit to apapirovski/node that referenced this pull request Oct 19, 2017
Enable additional rules that node either already adheres to
or it makes sense to do so going forward: for-direction,
accessor-pairs, no-lonely-if and symbol-description.

Fix all instances of no-lonely-if in lib & test and disable
accessor-pairs in test-util-inspect.

PR-URL: nodejs#16243
Refs: https://eslint.org/docs/rules/for-direction
Refs: https://eslint.org/docs/rules/accessor-pairs
Refs: https://eslint.org/docs/rules/no-lonely-if
Refs: https://eslint.org/docs/rules/symbol-description
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@apapirovski
Copy link
Member Author

Landed in 3c0ebf5

Sorry, @Fishrock123 I was already done by the time I saw your note. 😓

@apapirovski apapirovski deleted the additional-eslint-rules branch October 19, 2017 18:00
MylesBorins pushed a commit that referenced this pull request Oct 23, 2017
Enable additional rules that node either already adheres to
or it makes sense to do so going forward: for-direction,
accessor-pairs, no-lonely-if and symbol-description.

Fix all instances of no-lonely-if in lib & test and disable
accessor-pairs in test-util-inspect.

PR-URL: #16243
Refs: https://eslint.org/docs/rules/for-direction
Refs: https://eslint.org/docs/rules/accessor-pairs
Refs: https://eslint.org/docs/rules/no-lonely-if
Refs: https://eslint.org/docs/rules/symbol-description
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
addaleax pushed a commit to ayojs/ayo that referenced this pull request Oct 26, 2017
Enable additional rules that node either already adheres to
or it makes sense to do so going forward: for-direction,
accessor-pairs, no-lonely-if and symbol-description.

Fix all instances of no-lonely-if in lib & test and disable
accessor-pairs in test-util-inspect.

PR-URL: nodejs/node#16243
Refs: https://eslint.org/docs/rules/for-direction
Refs: https://eslint.org/docs/rules/accessor-pairs
Refs: https://eslint.org/docs/rules/no-lonely-if
Refs: https://eslint.org/docs/rules/symbol-description
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@MylesBorins
Copy link
Contributor

Should this be backported to v6.x-staging? If yes please follow the guide and raise a backport PR, if not let me know or add the dont-land-on label.

This should likely come with an update to ESLint

addaleax pushed a commit to ayojs/ayo that referenced this pull request Dec 7, 2017
Enable additional rules that node either already adheres to
or it makes sense to do so going forward: for-direction,
accessor-pairs, no-lonely-if and symbol-description.

Fix all instances of no-lonely-if in lib & test and disable
accessor-pairs in test-util-inspect.

PR-URL: nodejs/node#16243
Refs: https://eslint.org/docs/rules/for-direction
Refs: https://eslint.org/docs/rules/accessor-pairs
Refs: https://eslint.org/docs/rules/no-lonely-if
Refs: https://eslint.org/docs/rules/symbol-description
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants