-
Notifications
You must be signed in to change notification settings - Fork 887
trailingComma is required after rest parameter #3147
Comments
Is there an official spec that states this? |
Maybe we need a new tslint option for that. |
@ajafff tc39/proposal-object-rest-spread#47 (comment) But there is a chance that TS Spec is not aligned with ES spec in this case. UPD. const [a,b,...c,] = [1,2,3] // Destructuring rest variables must be in the last position of the expression
function err(a, ...rest,) {} // The rest parameter must be the last parameter in a formals list. Chrome: const [a,b,...c,] = [1,2,3] // Uncaught SyntaxError: Rest element must be last element
function err(a, ...rest,) {} // Uncaught SyntaxError: Rest parameter must be last formal parameter Option to skip trailing coma for rest looks like good idea, probably turned on by default. |
Ah, now I see. The issue title was misleading, so I updated it. I agree that there should be an option that overrides the value specified for the options To whoever implements this: If the object or array is the target of the assignment, the trailing comma is not allowed, but on the right side it is. ({a, b, ...c,} = {a, b, ...c,}); // error only on c before the equals token By just looking at the AST, both object literals are identical. You can use |
For TS, it doesn't feel right to fix this problem with a lint rule -- the compiler should instead be fixed to emit ES spec compliant JS. However, it does make sense to fix this for use cases where TSLint is run on JS files. |
@adidahiya are you suggesting that this should not be enabled by an option but rather depend on the file name / type? Regarding compiler output: I tested this with |
Linting a ts file has nothing to do with the compilation output. |
@ajafff no, sorry, I don't mean to suggest that we should change the behavior based on file name / type. I do agree that consistency between TS / JS files is good, so the new option should apply to both kinds of files. @thgreasi your initial post doesn't sound like a stylistic concern to me since you mention the spec. TS is lenient and allows the trailing comma, so I don't quite agree with this statement for TS files: "It shouldn't error since trailing commas are not allowed after spreaded parameters." |
It seems this should be re-opened as Typescript 2.9 no longer allows the comma on the end of spread operators. |
Sound so, thankfully all that has to be done is to just set the default value for the new option to also prevent trailing commas and ignore the user preference. |
Since TypeScript now disallows commas after an object rest param, I submitted a followup: #4172. |
Bug Report
TypeScript code being linted
with
tslint.json
configuration:Actual behavior
Error: x:y trailing-comma Missing trailing comma
on the line of...rest: any[]
Expected behavior
It shouldn't error since trailing commas are not allowed after spreaded parameters.
See: prettier/prettier#1079 (comment)
The text was updated successfully, but these errors were encountered: