Tslint: Trailing comma incorrectly identified inside multiline arrow function

Created on 20 May 2017  ·  4Comments  ·  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.2.0
  • __TypeScript version__: 2.3
  • __Running TSLint via__: CLI

TypeScript code being linted

commitsTableStore.setCommitRows(data.commits.map(commit =>
  new CommitRow(commit) // missing trailing comma reported here
));

with tslint.json configuration:

"trailing-comma": [true, {"multiline": "always"}]

Actual behavior

Missing trailing comma reported.

Expected behavior

No linting error reported.

More info

It's caused by the "functions" checker – TSLint thinks that this is a multiline function call.

Not A Bug

Most helpful comment

Thanks for all the context. I have to say it's quite unintuitive at first but follows some internal logic. And I didn't know you can put trailing commas in function calls just like that :)

For reference, this is how Prettier handles the situation with --trailing-comma=es5 configuration: https://goo.gl/Juay94

commitsTableStore.setCommitRows(
  data.commits.map(
    commit => new CommitRow(commit) // missing trailing comma reported here
  )
);

All 4 comments

huh, you're right, that is a false positive. accepting bugfix PRs

Cross posting my comment from https://github.com/palantir/tslint/pull/2988#issuecomment-312840722:

IMO this is not a bug or false positive.

The above example is considered "multiline" for a reason. The docs are very clear on that:

An array is considered “multiline” if its closing bracket is on a line after the last array element. The same general logic is followed for object literals, function typings, named import statements and function parameters.

To avoid this, you can simply rewrite it as follows:

commitsTableStore.setCommitRows(data.commits.map(commit =>
  new CommitRow(commit)));

//or
commitsTableStore.setCommitRows(data.commits.map((commit =>
  new CommitRow(commit)
))); //  note the extra parens around the arrow function

If we want to implement an exception for such cases, this would need a proposal what exacltly should be handled different. And it should be implemented behind an option.

ah, I see your point @ajafff, and I now agree, this is not a bug or false positive. as per https://github.com/palantir/tslint/issues/2909#issuecomment-307898234, we don't have plans to to check the arity of the function being called in the trailing-comma implementation. your proposed workarounds are good 👍

Thanks for all the context. I have to say it's quite unintuitive at first but follows some internal logic. And I didn't know you can put trailing commas in function calls just like that :)

For reference, this is how Prettier handles the situation with --trailing-comma=es5 configuration: https://goo.gl/Juay94

commitsTableStore.setCommitRows(
  data.commits.map(
    commit => new CommitRow(commit) // missing trailing comma reported here
  )
);
Was this page helpful?
0 / 5 - 0 ratings