Merge pull request #24672 from bakkot/binding-comma
Allow trailing commas after non-rest elements in destructuring
This commit is contained in:
commit
34dd7738c9
|
@ -20046,7 +20046,6 @@ namespace ts {
|
||||||
|
|
||||||
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type): Type {
|
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type): Type {
|
||||||
const properties = node.properties;
|
const properties = node.properties;
|
||||||
checkGrammarForDisallowedTrailingComma(properties, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
|
|
||||||
if (strictNullChecks && properties.length === 0) {
|
if (strictNullChecks && properties.length === 0) {
|
||||||
return checkNonNullType(sourceType, node);
|
return checkNonNullType(sourceType, node);
|
||||||
}
|
}
|
||||||
|
@ -20057,7 +20056,7 @@ namespace ts {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
|
/** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
|
||||||
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike, allProperties?: ReadonlyArray<ObjectLiteralElementLike>) {
|
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike, allProperties?: NodeArray<ObjectLiteralElementLike>) {
|
||||||
if (property.kind === SyntaxKind.PropertyAssignment || property.kind === SyntaxKind.ShorthandPropertyAssignment) {
|
if (property.kind === SyntaxKind.PropertyAssignment || property.kind === SyntaxKind.ShorthandPropertyAssignment) {
|
||||||
const name = property.name;
|
const name = property.name;
|
||||||
if (name.kind === SyntaxKind.ComputedPropertyName) {
|
if (name.kind === SyntaxKind.ComputedPropertyName) {
|
||||||
|
@ -20097,6 +20096,7 @@ namespace ts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const type = getRestType(objectLiteralType, nonRestNames, objectLiteralType.symbol);
|
const type = getRestType(objectLiteralType, nonRestNames, objectLiteralType.symbol);
|
||||||
|
checkGrammarForDisallowedTrailingComma(allProperties, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
|
||||||
return checkDestructuringAssignment(property.expression, type);
|
return checkDestructuringAssignment(property.expression, type);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -20106,7 +20106,6 @@ namespace ts {
|
||||||
|
|
||||||
function checkArrayLiteralAssignment(node: ArrayLiteralExpression, sourceType: Type, checkMode?: CheckMode): Type {
|
function checkArrayLiteralAssignment(node: ArrayLiteralExpression, sourceType: Type, checkMode?: CheckMode): Type {
|
||||||
const elements = node.elements;
|
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) {
|
if (languageVersion < ScriptTarget.ES2015 && compilerOptions.downlevelIteration) {
|
||||||
checkExternalEmitHelpers(node, ExternalEmitHelpers.Read);
|
checkExternalEmitHelpers(node, ExternalEmitHelpers.Read);
|
||||||
}
|
}
|
||||||
|
@ -20158,6 +20157,7 @@ namespace ts {
|
||||||
error((<BinaryExpression>restExpression).operatorToken, Diagnostics.A_rest_element_cannot_have_an_initializer);
|
error((<BinaryExpression>restExpression).operatorToken, Diagnostics.A_rest_element_cannot_have_an_initializer);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
checkGrammarForDisallowedTrailingComma(node.elements, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
|
||||||
return checkDestructuringAssignment(restExpression, createArrayType(elementType), checkMode);
|
return checkDestructuringAssignment(restExpression, createArrayType(elementType), checkMode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,4 +18,11 @@ tests/cases/conformance/es7/trailingCommasInBindingPatterns.ts(5,7): error TS101
|
||||||
({...d,} = {});
|
({...d,} = {});
|
||||||
~
|
~
|
||||||
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma.
|
!!! error TS1013: A rest parameter or binding pattern may not have a trailing comma.
|
||||||
|
|
||||||
|
// Allowed for non-rest elements
|
||||||
|
const [e,] = <any>[];
|
||||||
|
const {f,} = <any>{};
|
||||||
|
let g, h;
|
||||||
|
([g,] = <any>[]);
|
||||||
|
({h,} = <any>{});
|
||||||
|
|
|
@ -4,6 +4,13 @@ const {...b,} = {};
|
||||||
let c, d;
|
let c, d;
|
||||||
([...c,] = []);
|
([...c,] = []);
|
||||||
({...d,} = {});
|
({...d,} = {});
|
||||||
|
|
||||||
|
// Allowed for non-rest elements
|
||||||
|
const [e,] = <any>[];
|
||||||
|
const {f,} = <any>{};
|
||||||
|
let g, h;
|
||||||
|
([g,] = <any>[]);
|
||||||
|
({h,} = <any>{});
|
||||||
|
|
||||||
|
|
||||||
//// [trailingCommasInBindingPatterns.js]
|
//// [trailingCommasInBindingPatterns.js]
|
||||||
|
@ -21,3 +28,9 @@ var b = __rest({}, []);
|
||||||
var c, d;
|
var c, d;
|
||||||
(c = [].slice(0));
|
(c = [].slice(0));
|
||||||
(d = __rest({}, []));
|
(d = __rest({}, []));
|
||||||
|
// Allowed for non-rest elements
|
||||||
|
var e = [][0];
|
||||||
|
var f = {}.f;
|
||||||
|
var g, h;
|
||||||
|
(g = [][0]);
|
||||||
|
(h = {}.h);
|
||||||
|
|
|
@ -15,3 +15,20 @@ let c, d;
|
||||||
({...d,} = {});
|
({...d,} = {});
|
||||||
>d : Symbol(d, Decl(trailingCommasInBindingPatterns.ts, 2, 6))
|
>d : Symbol(d, Decl(trailingCommasInBindingPatterns.ts, 2, 6))
|
||||||
|
|
||||||
|
// Allowed for non-rest elements
|
||||||
|
const [e,] = <any>[];
|
||||||
|
>e : Symbol(e, Decl(trailingCommasInBindingPatterns.ts, 7, 7))
|
||||||
|
|
||||||
|
const {f,} = <any>{};
|
||||||
|
>f : Symbol(f, Decl(trailingCommasInBindingPatterns.ts, 8, 7))
|
||||||
|
|
||||||
|
let g, h;
|
||||||
|
>g : Symbol(g, Decl(trailingCommasInBindingPatterns.ts, 9, 3))
|
||||||
|
>h : Symbol(h, Decl(trailingCommasInBindingPatterns.ts, 9, 6))
|
||||||
|
|
||||||
|
([g,] = <any>[]);
|
||||||
|
>g : Symbol(g, Decl(trailingCommasInBindingPatterns.ts, 9, 3))
|
||||||
|
|
||||||
|
({h,} = <any>{});
|
||||||
|
>h : Symbol(h, Decl(trailingCommasInBindingPatterns.ts, 11, 2))
|
||||||
|
|
||||||
|
|
|
@ -26,3 +26,34 @@ let c, d;
|
||||||
>d : any
|
>d : any
|
||||||
>{} : {}
|
>{} : {}
|
||||||
|
|
||||||
|
// Allowed for non-rest elements
|
||||||
|
const [e,] = <any>[];
|
||||||
|
>e : any
|
||||||
|
><any>[] : any
|
||||||
|
>[] : undefined[]
|
||||||
|
|
||||||
|
const {f,} = <any>{};
|
||||||
|
>f : any
|
||||||
|
><any>{} : any
|
||||||
|
>{} : {}
|
||||||
|
|
||||||
|
let g, h;
|
||||||
|
>g : any
|
||||||
|
>h : any
|
||||||
|
|
||||||
|
([g,] = <any>[]);
|
||||||
|
>([g,] = <any>[]) : any
|
||||||
|
>[g,] = <any>[] : any
|
||||||
|
>[g,] : [any]
|
||||||
|
>g : any
|
||||||
|
><any>[] : any
|
||||||
|
>[] : undefined[]
|
||||||
|
|
||||||
|
({h,} = <any>{});
|
||||||
|
>({h,} = <any>{}) : any
|
||||||
|
>{h,} = <any>{} : any
|
||||||
|
>{h,} : { h: any; }
|
||||||
|
>h : any
|
||||||
|
><any>{} : any
|
||||||
|
>{} : {}
|
||||||
|
|
||||||
|
|
|
@ -3,3 +3,10 @@ const {...b,} = {};
|
||||||
let c, d;
|
let c, d;
|
||||||
([...c,] = []);
|
([...c,] = []);
|
||||||
({...d,} = {});
|
({...d,} = {});
|
||||||
|
|
||||||
|
// Allowed for non-rest elements
|
||||||
|
const [e,] = <any>[];
|
||||||
|
const {f,} = <any>{};
|
||||||
|
let g, h;
|
||||||
|
([g,] = <any>[]);
|
||||||
|
({h,} = <any>{});
|
||||||
|
|
Loading…
Reference in a new issue