Skip to content

Commit 3f3b9f3

Browse files
author
Andy Hanson
committed
Error on binding patterns and improve error location
1 parent 0a32978 commit 3f3b9f3

13 files changed

+72
-28
lines changed

Diff for: src/compiler/checker.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -26067,11 +26067,9 @@ namespace ts {
2606726067
return grammarErrorOnNode(asyncModifier, Diagnostics._0_modifier_cannot_be_used_here, "async");
2606826068
}
2606926069

26070-
function checkGrammarForDisallowedTrailingComma(list: NodeArray<Node>): boolean {
26070+
function checkGrammarForDisallowedTrailingComma(list: NodeArray<Node>, diag = Diagnostics.Trailing_comma_not_allowed): boolean {
2607126071
if (list && list.hasTrailingComma) {
26072-
const start = list.end - ",".length;
26073-
const end = list.end;
26074-
return grammarErrorAtPos(list[0], start, end - start, Diagnostics.Trailing_comma_not_allowed);
26072+
return grammarErrorAtPos(list[0], list.end - ",".length, ",".length, diag);
2607526073
}
2607626074
}
2607726075

@@ -26093,9 +26091,7 @@ namespace ts {
2609326091
if (i !== (parameterCount - 1)) {
2609426092
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list);
2609526093
}
26096-
if (parameters.hasTrailingComma) {
26097-
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_rest_parameter_may_not_have_a_trailing_comma);
26098-
}
26094+
checkGrammarForDisallowedTrailingComma(parameters, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
2609926095

2610026096
if (isBindingPattern(parameter.name)) {
2610126097
return grammarErrorOnNode(parameter.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
@@ -26709,6 +26705,7 @@ namespace ts {
2670926705
if (node !== last(elements)) {
2671026706
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern);
2671126707
}
26708+
checkGrammarForDisallowedTrailingComma(elements, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
2671226709

2671326710
if (node.name.kind === SyntaxKind.ArrayBindingPattern || node.name.kind === SyntaxKind.ObjectBindingPattern) {
2671426711
return grammarErrorOnNode(node.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);

Diff for: src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"category": "Error",
2828
"code": 1012
2929
},
30-
"A rest parameter may not have a trailing comma.": {
30+
"A rest parameter or binding pattern may not have a trailing comma.": {
3131
"category": "Error",
3232
"code": 1013
3333
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts(1,12): error TS1013: A rest parameter or binding pattern may not have a trailing comma.
2+
tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts(2,12): error TS1013: A rest parameter or binding pattern may not have a trailing comma.
3+
4+
5+
==== tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts (2 errors) ====
6+
const [...a,] = [];
7+
~
8+
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma.
9+
const {...b,} = {};
10+
~
11+
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma.
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [trailingCommasInBindingPatterns.ts]
2+
const [...a,] = [];
3+
const {...b,} = {};
4+
5+
6+
//// [trailingCommasInBindingPatterns.js]
7+
var __rest = (this && this.__rest) || function (s, e) {
8+
var t = {};
9+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
10+
t[p] = s[p];
11+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
12+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
13+
t[p[i]] = s[p[i]];
14+
return t;
15+
};
16+
var a = [].slice(0);
17+
var b = __rest({}, []);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts ===
2+
const [...a,] = [];
3+
>a : Symbol(a, Decl(trailingCommasInBindingPatterns.ts, 0, 7))
4+
5+
const {...b,} = {};
6+
>b : Symbol(b, Decl(trailingCommasInBindingPatterns.ts, 1, 7))
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts ===
2+
const [...a,] = [];
3+
>a : any[]
4+
>[] : undefined[]
5+
6+
const {...b,} = {};
7+
>b : {}
8+
>{} : {}
9+

Diff for: tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts(5,13): error TS1013: A rest parameter may not have a trailing comma.
1+
tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts(5,20): error TS1013: A rest parameter or binding pattern may not have a trailing comma.
22

33

44
==== tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts (1 errors) ====
@@ -7,8 +7,8 @@ tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts(5,
77
f1(1,);
88

99
function f2(...args,) {}
10-
~~~
11-
!!! error TS1013: A rest parameter may not have a trailing comma.
10+
~
11+
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma.
1212

1313
f2(...[],);
1414

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
tests/cases/compiler/unusedLocalsAndObjectSpread2.ts(5,6): error TS6133: 'rest' is declared but its value is never read.
22
tests/cases/compiler/unusedLocalsAndObjectSpread2.ts(8,10): error TS6133: 'foo' is declared but its value is never read.
3-
tests/cases/compiler/unusedLocalsAndObjectSpread2.ts(12,8): error TS6133: 'rest' is declared but its value is never read.
3+
tests/cases/compiler/unusedLocalsAndObjectSpread2.ts(12,12): error TS6133: 'rest' is declared but its value is never read.
44

55

66
==== tests/cases/compiler/unusedLocalsAndObjectSpread2.ts (3 errors) ====
77
declare let props: any;
88
const {
99
children, // here!
1010
active: _a, // here!
11-
...rest,
11+
...rest
1212
~~~~
1313
!!! error TS6133: 'rest' is declared but its value is never read.
1414
} = props;
@@ -19,10 +19,10 @@ tests/cases/compiler/unusedLocalsAndObjectSpread2.ts(12,8): error TS6133: 'rest'
1919
const {
2020
children,
2121
active: _a,
22-
...rest,
23-
~~~~
22+
...rest
23+
~~~~
2424
!!! error TS6133: 'rest' is declared but its value is never read.
25-
} = props;
25+
} = props;
2626
}
2727

2828
export const asdf = 123;

Diff for: tests/baselines/reference/unusedLocalsAndObjectSpread2.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ declare let props: any;
33
const {
44
children, // here!
55
active: _a, // here!
6-
...rest,
6+
...rest
77
} = props;
88

99
function foo() {
1010
const {
1111
children,
1212
active: _a,
13-
...rest,
14-
} = props;
13+
...rest
14+
} = props;
1515
}
1616

1717
export const asdf = 123;

Diff for: tests/baselines/reference/unusedLocalsAndObjectSpread2.symbols

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
active: _a, // here!
1010
>_a : Symbol(_a, Decl(unusedLocalsAndObjectSpread2.ts, 2, 13))
1111

12-
...rest,
12+
...rest
1313
>rest : Symbol(rest, Decl(unusedLocalsAndObjectSpread2.ts, 3, 15))
1414

1515
} = props;
@@ -25,10 +25,10 @@ function foo() {
2525
active: _a,
2626
>_a : Symbol(_a, Decl(unusedLocalsAndObjectSpread2.ts, 9, 17))
2727

28-
...rest,
28+
...rest
2929
>rest : Symbol(rest, Decl(unusedLocalsAndObjectSpread2.ts, 10, 19))
3030

31-
} = props;
31+
} = props;
3232
>props : Symbol(props, Decl(unusedLocalsAndObjectSpread2.ts, 0, 11))
3333
}
3434

Diff for: tests/baselines/reference/unusedLocalsAndObjectSpread2.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
>active : any
1111
>_a : any
1212

13-
...rest,
13+
...rest
1414
>rest : any
1515

1616
} = props;
@@ -27,10 +27,10 @@ function foo() {
2727
>active : any
2828
>_a : any
2929

30-
...rest,
30+
...rest
3131
>rest : any
3232

33-
} = props;
33+
} = props;
3434
>props : any
3535
}
3636

Diff for: tests/cases/compiler/unusedLocalsAndObjectSpread2.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ declare let props: any;
44
const {
55
children, // here!
66
active: _a, // here!
7-
...rest,
7+
...rest
88
} = props;
99

1010
function foo() {
1111
const {
1212
children,
1313
active: _a,
14-
...rest,
15-
} = props;
14+
...rest
15+
} = props;
1616
}
1717

1818
export const asdf = 123;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const [...a,] = [];
2+
const {...b,} = {};

0 commit comments

Comments
 (0)