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: require function declarations #12711

Closed
wants to merge 2 commits into from

Conversation

@Trott
Copy link
Member

commented Apr 28, 2017

Welp, not sure how this will be received, but since it is the predominant style in our code base and since we are occasionally offering nits to new contributors to use the style to avoid potential confusion over the Temporal Dead Zone....

In lib, doc, and test (but mostly test): Replace function expressions with function declarations in preparation for a lint rule requiring function declarations.

Then: Except for arrow functions, require function declarations instead of function expressions via linting. This is the predominant style in our code base (77 instances of expressions to 2344 instances of declarations).

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

tools test lib doc

@@ -592,7 +592,7 @@ Returns `true` if the given `object` is a `Function`. Otherwise, returns
const util = require('util');

function Foo() {}
const Bar = function() {};
const Bar = () => {};

This comment has been minimized.

Copy link
@hiroppy

hiroppy Apr 28, 2017

Member

common.noop ?

This comment has been minimized.

Copy link
@mscdex

mscdex Apr 28, 2017

Contributor

@abouthiroppy Are you confusing this doc file with a test?

This comment has been minimized.

Copy link
@hiroppy

hiroppy Apr 28, 2017

Member

@mscdex Oops, thanks 😓

.eslintrc.yaml Outdated
@@ -97,6 +97,7 @@ rules:
eol-last: 2
func-call-spacing: 2
func-name-matching: 2
func-style: [2, "declaration", {allowArrowFunctions: true}]

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor

Can the quotes be omitted here?

This comment has been minimized.

Copy link
@Trott

Trott Apr 28, 2017

Author Member

Can the quotes be omitted here?

Yes, indeed. Done!

test/addons-napi/test_async/test.js Outdated
@@ -9,5 +9,4 @@ test_async.Test(5, common.mustCall(function(err, val) {
process.nextTick(common.mustCall(function() {}));

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor

Can it also be common.mustCall()?

This comment has been minimized.

Copy link
@Trott

Trott Apr 28, 2017

Author Member

Can it also be common.mustCall()?

Sure seems like it! Done! Thanks!

@@ -68,8 +68,8 @@ const EventEmitter = require('events');
}

{
const listen1 = function listen1() {};
const listen2 = function listen2() {};
const listen1 = () => {};

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor

common.noop?

This comment has been minimized.

Copy link
@Trott

Trott Apr 28, 2017

Author Member

Not here or on the line below, and this is actually something that I've worried about for a while: common.noop doesn't really save us a lot of code and can lead to subtle bugs such as if it is used here.

If used here and the line below, then listen1 and listen2 are the same object, thus invalidating the test!

This could perhaps be fixed by making common.noop a getter that returns a different no-op function object each time it is called. But I kinda wonder if we might just be better off doing a global find/replace on common.noop and using () => {} instead.

/cc @nodejs/testing

This comment has been minimized.

Copy link
@aqrln

aqrln Apr 28, 2017

Member

@Trott on the other hand, test/parallel/test-event-emitter-remove-all-listeners relies on common.noop being the same object (discovered that by actually making common.noop a getter).

This comment has been minimized.

Copy link
@Trott

Trott Apr 28, 2017

Author Member

@aqrln In that case, I imagine explicitly defining const noop = () => {}; in the test itself would make it clear that everything is intentionally using the same function object and that it's not a mistake.

This comment has been minimized.

Copy link
@aqrln

aqrln Apr 28, 2017

Member

@Trott yeah, makes sense. I will follow up with a PR quickly.

This comment has been minimized.

Copy link
@jasnell

jasnell Apr 29, 2017

Member

I just simply would not use common.noop here. In fact, if memory serves correctly, when I opened the common.noop PR just a few weeks ago I intentionally left this test alone because it did not make sense to use it here... and that is perfectly fine. It's ok not to use it when it doesn't make sense to.

const listen1 = function listen1() {};
const listen2 = function listen2() {};
const listen1 = () => {};
const listen2 = () => {};

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor

common.noop?

@@ -296,7 +296,7 @@ assert.strictEqual(

// Function with properties
{
const value = function() {};
const value = () => {};

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor

common.noop?

This comment has been minimized.

Copy link
@Trott

Trott Apr 28, 2017

Author Member

This is another place where common.noop usage can lead to subtle bugs. The next line adds a property to the object, which would end up adding the property to common.noop which means possible side effects in other tests.

test/parallel/test-os.js Outdated
@@ -125,7 +125,7 @@ console.error(interfaces);
switch (platform) {
case 'linux':
{
const filter = function(e) { return e.address === '127.0.0.1'; };
const filter = (e) => { return e.address === '127.0.0.1'; };

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor

e => e.address === '127.0.0.1'?

This comment has been minimized.

Copy link
@Trott

Trott Apr 28, 2017

Author Member

Sure! It's the predominant style and if it inches us closer to being able to enable arrow-body-style in .eslintrc.yaml, then I'm all for it.

test/parallel/test-os.js Outdated
@@ -135,7 +135,7 @@ switch (platform) {
}
case 'win32':
{
const filter = function(e) { return e.address === '127.0.0.1'; };
const filter = (e) => { return e.address === '127.0.0.1'; };

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor

e => e.address === '127.0.0.1'?

This comment has been minimized.

Copy link
@Trott

Trott Apr 28, 2017

Author Member

e => e.address === '127.0.0.1'?

👍

test/parallel/test-preload.js Outdated
let option = '';
preloads.forEach(function(preload, index) {
option += '-r ' + preload + ' ';
});
return option;
};

const fixture = function(name) {
const fixture = (name) => {

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor
const fixture = name => path.join(common.fixturesDir, name);

?

test/parallel/test-readline-interface.js Outdated
@@ -288,7 +288,7 @@ function isWarned(emitter) {

// \t does not become part of the input when there is a completer function
fi = new FakeInput();
const completer = function(line) {
const completer = (line) => {

This comment has been minimized.

Copy link
@vsemozhetbyt

vsemozhetbyt Apr 28, 2017

Contributor
const completer = line => [[], line];

?

@vsemozhetbyt

This comment has been minimized.

Copy link
Contributor

commented Apr 28, 2017

@Trott Trott force-pushed the Trott:func-style branch 2 times, most recently Apr 28, 2017

Trott added some commits Apr 28, 2017

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.
tools: require function declarations
Except for arrow functions, require function declarations instead of
function expressions via linting. This is the predominant style in our
code base (77 instances of expressions to 2344 instances of
declarations).

@Trott Trott force-pushed the Trott:func-style branch to 791a56f Apr 28, 2017

@Trott

This comment has been minimized.

Copy link
Member Author

commented Apr 28, 2017

Arrow function and quotation mark nits applied.

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

aqrln added a commit to aqrln/node that referenced this pull request Apr 28, 2017

test: make common.noop a getter
Make `common.noop` a getter that returns a new function object each time
the propery is read, not the same one. The old behavior could possibly
lead to subtle and hard to catch bugs in some cases.

Refs: nodejs#12711 (comment)

@aqrln aqrln referenced this pull request Apr 28, 2017

Closed

test: make common.noop a getter #12732

4 of 4 tasks complete

@refack refack referenced this pull request Apr 30, 2017

Closed

readline: refactor readline module #12755

3 of 3 tasks complete

Trott added a commit to Trott/io.js that referenced this pull request May 1, 2017

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

Trott added a commit to Trott/io.js that referenced this pull request May 1, 2017

tools: require function declarations
Except for arrow functions, require function declarations instead of
function expressions via linting. This is the predominant style in our
code base (77 instances of expressions to 2344 instances of
declarations).

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
@Trott

This comment has been minimized.

Copy link
Member Author

commented May 1, 2017

Landed in a180259 and aea7269

@Trott Trott closed this May 1, 2017

anchnk added a commit to anchnk/node that referenced this pull request May 6, 2017

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

anchnk added a commit to anchnk/node that referenced this pull request May 6, 2017

tools: require function declarations
Except for arrow functions, require function declarations instead of
function expressions via linting. This is the predominant style in our
code base (77 instances of expressions to 2344 instances of
declarations).

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

@jasnell jasnell referenced this pull request May 11, 2017

Closed

8.0.0 Release Proposal #12220

@gibfahn gibfahn referenced this pull request Jun 15, 2017

Closed

Auditing for 6.11.1 #230

2 of 3 tasks complete
@gibfahn

This comment has been minimized.

Copy link
Member

commented Jun 18, 2017

@Trott would you be willing to backport this to v6.x-staging? If yes please follow the guide and raise a backport PR, if no let me know or add the dont-land-on label.

Trott added a commit to Trott/io.js that referenced this pull request Jun 19, 2017

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

Trott added a commit to Trott/io.js that referenced this pull request Jun 19, 2017

tools: require function declarations
Except for arrow functions, require function declarations instead of
function expressions via linting. This is the predominant style in our
code base (77 instances of expressions to 2344 instances of
declarations).

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
@Trott

This comment has been minimized.

Copy link
Member Author

commented Jun 19, 2017

@gibfahn Backported in #13774

gibfahn added a commit that referenced this pull request Jun 20, 2017

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: #12711
Backport-PR-URL: #13774
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

gibfahn added a commit that referenced this pull request Jun 20, 2017

tools: require function declarations
Except for arrow functions, require function declarations instead of
function expressions via linting. This is the predominant style in our
code base (77 instances of expressions to 2344 instances of
declarations).

PR-URL: #12711
Backport-PR-URL: #13774
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

MylesBorins added a commit that referenced this pull request Jul 11, 2017

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: #12711
Backport-PR-URL: #13774
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

MylesBorins added a commit that referenced this pull request Jul 11, 2017

tools: require function declarations
Except for arrow functions, require function declarations instead of
function expressions via linting. This is the predominant style in our
code base (77 instances of expressions to 2344 instances of
declarations).

PR-URL: #12711
Backport-PR-URL: #13774
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

@MylesBorins MylesBorins referenced this pull request Jul 18, 2017

Merged

v6.11.2 proposal #14356

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 12, 2018

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

This applies portions of a180259 that
are relevant to N-API.

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 31, 2018

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 10, 2018

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: nodejs#12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

MylesBorins added a commit that referenced this pull request Apr 16, 2018

test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

Backport-PR-URL: #19447
PR-URL: #12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>

@MylesBorins MylesBorins referenced this pull request Apr 16, 2018

Merged

v6.14.2 proposal #19996

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.