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: lint for function argument alignment #6268

Closed
wants to merge 2 commits into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Apr 19, 2016

Checklist
  • tests and code linting passes
  • the commit message follows commit guidelines
Affected core subsystem(s)

tools benchmark lib test

Description of change
In function calls that span multiple lines, apply a custom lint rule to
enforce argument alignment.

With this rule, the following code will be flagged as an error by the
linter because the arguments on the second line start in a different
column than on the first line:

    myFunction(a, b,
      c, d);

The following code will not be flagged as an error by the linter:

    myFunction(a, b,
               c, d);

@Trott Trott added test Issues and PRs related to the tests. benchmark Issues and PRs related to the benchmark subsystem. tools Issues and PRs related to the tools directory. lib / src Issues and PRs related to general changes in the lib or src directory. labels Apr 19, 2016
@Trott
Copy link
Member Author

Trott commented Apr 19, 2016

Not sure if folks will like this one or not, but this is the other thing I always get nits about (hi, @bnoordhuis!). So rather than keep messing it up every time the rest of my life and spending valuable time force pushing to fix it, maybe the linter can tell me when I've messed it up and no one has to know.

@Trott Trott force-pushed the align-multiline-function-call branch from 94c16d3 to 9f9cb35 Compare April 19, 2016 04:51
@jasnell
Copy link
Member

jasnell commented Apr 19, 2016

I'm generally +1 on this. LGTM but let's here from others.

@Trott Trott force-pushed the align-multiline-function-call branch 2 times, most recently from 220e717 to 716614f Compare April 21, 2016 03:40
@Trott
Copy link
Member Author

Trott commented Apr 21, 2016

rebased, force pushed...

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

@Trott
Copy link
Member Author

Trott commented Apr 21, 2016

Only failure in CI is a known problem test that @cjihrig has a pending pull request that fixes it.

@Trott
Copy link
Member Author

Trott commented Apr 21, 2016

/bump @nodejs/collaborators

Any enthusiastic endorsements or horrified objections to this?

internalUtil.deprecate(function() {
return require('tls').createSecureContext;
}, 'crypto.createCredentials is deprecated. ' +
'Use tls.createSecureContext instead.'));
Copy link
Member

Choose a reason for hiding this comment

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

the alignment seems wrong to me here

Copy link
Member

Choose a reason for hiding this comment

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

@tergos do you mean wrong as in you disagree with it personally?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure. It looks wrong to me because both strings are not aligned to each other

Copy link
Member Author

@Trott Trott Apr 21, 2016

Choose a reason for hiding this comment

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

@targos Simplifying a bit, this is what the above code boils down to:

func1(arg1String,
      arg2Func(function() {
        return foo;
      }, innerArgString));

I wonder if the real obstacle to readability is the string concatenation (which makes it look like two string arguments when it's just one).

Also, the final closing parenthesis might make more sense on its own line.

Would removing the concatenation and moving the parenthesis help? Or is the issue something else?

Copy link
Member Author

Choose a reason for hiding this comment

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

@targos @benjamingr I've rebased, rewritten these lines to hopefully conform more to expectations, and force pushed. Here's what they look like now:

// Legacy API
function defineLegacyGetter(legacyName, tlsName) {
  function wrapped() {
    return require('tls')[tlsName];
  }
  const msg = `crypto.${legacyName} is deprecated. Use tls.${tlsName} instead.`;
  exports.__defineGetter__(legacyName, internalUtil.deprecate(wrapped, msg));
}
defineLegacyGetter('createCredentials', 'createSecureContext');
defineLegacyGetter('Credentials', 'SecureContext');

@targos
Copy link
Member

targos commented Apr 21, 2016

I'm also +1 on the change. I think it improves readability.
@Trott did you try to propose this rule upstream ?

@claudiorodriguez
Copy link
Contributor

+1 on this change - I remember seeing different styles all around, sticking to a single one and enforcing it with linting will definitely help readability while switching files.

@benjamingr
Copy link
Member

+1 and generally LGTM.

}, /Got unwanted exception. user message/,
'a.doesNotThrow ignores user message');
assert.throws(
() => {
Copy link
Member

Choose a reason for hiding this comment

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

Was moving the arrow a line down necessary here for the rule?

Copy link
Member Author

Choose a reason for hiding this comment

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

It wasn't the only option, but yes. Moving it meant that the next line didn't have to be indented so much that it exceeded the 80-character limit on line length.

Copy link
Member

Choose a reason for hiding this comment

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

I'm generally not a fan of moving the first argument down if it can be avoided but can live with this.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jasnell There are certainly other options, like assigning the function to a variable so you can just do assert.throws(foo,...).

Copy link
Member Author

Choose a reason for hiding this comment

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

By the way, the previous indentation is, in my opinion at least, unfriendly to the reader:

assert.throws(() => {
  assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
}, /Got unwanted exception. user message/,
   'a.doesNotThrow ignores user message');

I think moving the arrow function down one line is worth getting code that is easier to understand at a glance:

assert.throws(
  () => {
    assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
  },
  /Got unwanted exception. user message/,
  'a.doesNotThrow ignores user message'
);

@benjamingr
Copy link
Member

This is going through a lot of code though, I wonder if we can't do this more incrementally.

@Trott
Copy link
Member Author

Trott commented Apr 21, 2016

@targos asked:

@Trott did you try to propose this rule upstream ?

Not yet, but that was the plan. There's a loophole in the implementation (for a common case with timers) that will probably be deemed larger than it needs to be. I suspect that if the upstream project wants the rule, they will want that tightened up a bit, which would be nice.

@Trott
Copy link
Member Author

Trott commented Apr 21, 2016

This is going through a lot of code though, I wonder if we can't do this more incrementally.

Do you mean multiple PRs? Or just split this PR into more commits? Or...

@Trott Trott force-pushed the align-multiline-function-call branch from 716614f to 277aa63 Compare April 22, 2016 04:29
@Trott
Copy link
Member Author

Trott commented Apr 23, 2016

Rule proposed upstream: eslint/eslint#5946

@Trott Trott force-pushed the align-multiline-function-call branch from 277aa63 to 76a7bc4 Compare April 24, 2016 20:27
Trott added 2 commits April 25, 2016 16:37
In preparation for a lint rule that will enforce alignment of arguments
in function calls that span multiple lines, align them in files where
they are not currently aligned.
In function calls that span multiple lines, apply a custom lint rule to
enforce argument alignment.

With this rule, the following code will be flagged as an error by the
linter because the arguments on the second line start in a different
column than on the first line:

    myFunction(a, b,
      c, d);

The following code will not be flagged as an error by the linter:

    myFunction(a, b,
               c, d);
@Trott Trott force-pushed the align-multiline-function-call branch from 76a7bc4 to 74adfbc Compare April 25, 2016 23:37
@estliberitas estliberitas force-pushed the master branch 2 times, most recently from 7da4fd4 to c7066fb Compare April 26, 2016 05:23
@Trott
Copy link
Member Author

Trott commented Apr 26, 2016

I'm going to close this in favor of #6390 which is a more lenient version of this rule and therefore has a smaller (and hopefully uncontroversial) changeset.

@Trott Trott closed this Apr 26, 2016
@Trott Trott deleted the align-multiline-function-call branch January 13, 2022 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
benchmark Issues and PRs related to the benchmark subsystem. lib / src Issues and PRs related to general changes in the lib or src directory. test Issues and PRs related to the tests. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants