-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Error on rest parameter with trailing comma #22262
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18824,6 +18824,7 @@ namespace ts { | |
|
||
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type): Type { | ||
const properties = node.properties; | ||
checkGrammarForDisallowedTrailingComma(properties, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma); | ||
if (strictNullChecks && properties.length === 0) { | ||
return checkNonNullType(sourceType, node); | ||
} | ||
|
@@ -18882,6 +18883,8 @@ namespace ts { | |
} | ||
|
||
function checkArrayLiteralAssignment(node: ArrayLiteralExpression, sourceType: Type, checkMode?: CheckMode): Type { | ||
const elements = node.elements; | ||
checkGrammarForDisallowedTrailingComma(elements, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma); | ||
if (languageVersion < ScriptTarget.ES2015 && compilerOptions.downlevelIteration) { | ||
checkExternalEmitHelpers(node, ExternalEmitHelpers.Read); | ||
} | ||
|
@@ -18890,7 +18893,6 @@ namespace ts { | |
// present (aka the tuple element property). This call also checks that the parentType is in | ||
// fact an iterable or array (depending on target language). | ||
const elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false, /*allowAsyncIterables*/ false) || unknownType; | ||
const elements = node.elements; | ||
for (let i = 0; i < elements.length; i++) { | ||
checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, checkMode); | ||
} | ||
|
@@ -26067,11 +26069,9 @@ namespace ts { | |
return grammarErrorOnNode(asyncModifier, Diagnostics._0_modifier_cannot_be_used_here, "async"); | ||
} | ||
|
||
function checkGrammarForDisallowedTrailingComma(list: NodeArray<Node>): boolean { | ||
function checkGrammarForDisallowedTrailingComma(list: NodeArray<Node>, diag = Diagnostics.Trailing_comma_not_allowed): boolean { | ||
if (list && list.hasTrailingComma) { | ||
const start = list.end - ",".length; | ||
const end = list.end; | ||
return grammarErrorAtPos(list[0], start, end - start, Diagnostics.Trailing_comma_not_allowed); | ||
return grammarErrorAtPos(list[0], list.end - ",".length, ",".length, diag); | ||
} | ||
} | ||
|
||
|
@@ -26093,6 +26093,7 @@ namespace ts { | |
if (i !== (parameterCount - 1)) { | ||
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list); | ||
} | ||
checkGrammarForDisallowedTrailingComma(parameters, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say that this is the wrong span to error on. Either error on the comma, or the parameter itself. |
||
if (isBindingPattern(parameter.name)) { | ||
return grammarErrorOnNode(parameter.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); | ||
|
@@ -26706,6 +26707,7 @@ namespace ts { | |
if (node !== last(elements)) { | ||
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern); | ||
} | ||
checkGrammarForDisallowedTrailingComma(elements, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma); | ||
|
||
if (node.name.kind === SyntaxKind.ArrayBindingPattern || node.name.kind === SyntaxKind.ObjectBindingPattern) { | ||
return grammarErrorOnNode(node.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/baselines/reference/trailingCommasInBindingPatterns.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts(1,12): error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts(2,12): error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts(4,7): error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts(5,7): error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
|
||
|
||
==== tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts (4 errors) ==== | ||
const [...a,] = []; | ||
~ | ||
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
const {...b,} = {}; | ||
~ | ||
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
let c, d; | ||
([...c,] = []); | ||
~ | ||
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
({...d,} = {}); | ||
~ | ||
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
|
23 changes: 23 additions & 0 deletions
23
tests/baselines/reference/trailingCommasInBindingPatterns.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//// [trailingCommasInBindingPatterns.ts] | ||
const [...a,] = []; | ||
const {...b,} = {}; | ||
let c, d; | ||
([...c,] = []); | ||
({...d,} = {}); | ||
|
||
|
||
//// [trailingCommasInBindingPatterns.js] | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) | ||
t[p[i]] = s[p[i]]; | ||
return t; | ||
}; | ||
var a = [].slice(0); | ||
var b = __rest({}, []); | ||
var c, d; | ||
(c = [].slice(0)); | ||
(d = __rest({}, [])); |
17 changes: 17 additions & 0 deletions
17
tests/baselines/reference/trailingCommasInBindingPatterns.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
=== tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts === | ||
const [...a,] = []; | ||
>a : Symbol(a, Decl(trailingCommasInBindingPatterns.ts, 0, 7)) | ||
|
||
const {...b,} = {}; | ||
>b : Symbol(b, Decl(trailingCommasInBindingPatterns.ts, 1, 7)) | ||
|
||
let c, d; | ||
>c : Symbol(c, Decl(trailingCommasInBindingPatterns.ts, 2, 3)) | ||
>d : Symbol(d, Decl(trailingCommasInBindingPatterns.ts, 2, 6)) | ||
|
||
([...c,] = []); | ||
>c : Symbol(c, Decl(trailingCommasInBindingPatterns.ts, 2, 3)) | ||
|
||
({...d,} = {}); | ||
>d : Symbol(d, Decl(trailingCommasInBindingPatterns.ts, 2, 6)) | ||
|
28 changes: 28 additions & 0 deletions
28
tests/baselines/reference/trailingCommasInBindingPatterns.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
=== tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts === | ||
const [...a,] = []; | ||
>a : any[] | ||
>[] : undefined[] | ||
|
||
const {...b,} = {}; | ||
>b : {} | ||
>{} : {} | ||
|
||
let c, d; | ||
>c : any | ||
>d : any | ||
|
||
([...c,] = []); | ||
>([...c,] = []) : undefined[] | ||
>[...c,] = [] : undefined[] | ||
>[...c,] : undefined[] | ||
>...c : any | ||
>c : any | ||
>[] : undefined[] | ||
|
||
({...d,} = {}); | ||
>({...d,} = {}) : {} | ||
>{...d,} = {} : {} | ||
>{...d,} : any | ||
>d : any | ||
>{} : {} | ||
|
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts(5,20): error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
|
||
|
||
==== tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts (1 errors) ==== | ||
function f1(x,) {} | ||
|
||
f1(1,); | ||
|
||
function f2(...args,) {} | ||
~ | ||
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma. | ||
|
||
f2(...[],); | ||
|
||
// Not confused by overloads | ||
declare function f3(x, ): number; | ||
declare function f3(x, y,): string; | ||
|
||
<number>f3(1,); | ||
<string>f3(1, 2,); | ||
|
||
// Works for constructors too | ||
class X { | ||
constructor(a,) { } | ||
// See trailingCommasInGetter.ts | ||
set x(value,) { } | ||
} | ||
interface Y { | ||
new(x,); | ||
(x,); | ||
} | ||
|
||
new X(1,); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const [...a,] = []; | ||
const {...b,} = {}; | ||
let c, d; | ||
([...c,] = []); | ||
({...d,} = {}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this optional parameter used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the four new calls added in this PR.