Skip to content

Improved checking of destructuring with literal initializers #4598

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

Merged
merged 17 commits into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 108 additions & 39 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ namespace ts {
The_arguments_object_cannot_be_referenced_in_an_async_arrow_function_Consider_using_a_standard_async_function_expression: { code: 2522, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an async arrow function. Consider using a standard async function expression." },
yield_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2523, category: DiagnosticCategory.Error, key: "'yield' expressions cannot be used in a parameter initializer." },
await_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2524, category: DiagnosticCategory.Error, key: "'await' expressions cannot be used in a parameter initializer." },
Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value: { code: 2525, category: DiagnosticCategory.Error, key: "Initializer provides no value for this binding element and the binding element has no default value." },
JSX_element_attributes_type_0_must_be_an_object_type: { code: 2600, category: DiagnosticCategory.Error, key: "JSX element attributes type '{0}' must be an object type." },
The_return_type_of_a_JSX_element_constructor_must_return_an_object_type: { code: 2601, category: DiagnosticCategory.Error, key: "The return type of a JSX element constructor must return an object type." },
JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist: { code: 2602, category: DiagnosticCategory.Error, key: "JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist." },
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,10 @@
"category": "Error",
"code": 2524
},
"Initializer provides no value for this binding element and the binding element has no default value.": {
"category": "Error",
"code": 2525
},
"JSX element attributes type '{0}' must be an object type.": {
"category": "Error",
"code": 2600
Expand Down
13 changes: 8 additions & 5 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,7 @@ namespace ts {
resolvedExports?: SymbolTable; // Resolved exports of module
exportsChecked?: boolean; // True if exports of external module have been checked
isNestedRedeclaration?: boolean; // True if symbol is block scoped redeclaration
bindingElement?: BindingElement; // Binding element associated with property symbol
}

/* @internal */
Expand Down Expand Up @@ -1811,11 +1812,14 @@ namespace ts {
PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType
}

export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;

// Properties common to all types
export interface Type {
flags: TypeFlags; // Flags
/* @internal */ id: number; // Unique ID
symbol?: Symbol; // Symbol associated with type (if any)
flags: TypeFlags; // Flags
/* @internal */ id: number; // Unique ID
symbol?: Symbol; // Symbol associated with type (if any)
pattern?: DestructuringPattern; // Destructuring pattern represented by type (if any)
}

/* @internal */
Expand Down Expand Up @@ -1864,8 +1868,7 @@ namespace ts {
}

export interface TupleType extends ObjectType {
elementTypes: Type[]; // Element types
baseArrayType: TypeReference; // Array<T> where T is best common type of element types
elementTypes: Type[]; // Element types
}

export interface UnionOrIntersectionType extends Type {
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/arrowFunctionExpressions.types
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ var p8 = ({ a = 1 }) => { };
>1 : number

var p9 = ({ a: { b = 1 } = { b: 1 } }) => { };
>p9 : ({ a: { b = 1 } = { b: 1 } }: { a?: { b: number; }; }) => void
>({ a: { b = 1 } = { b: 1 } }) => { } : ({ a: { b = 1 } = { b: 1 } }: { a?: { b: number; }; }) => void
>p9 : ({ a: { b = 1 } = { b: 1 } }: { a?: { b?: number; }; }) => void
>({ a: { b = 1 } = { b: 1 } }) => { } : ({ a: { b = 1 } = { b: 1 } }: { a?: { b?: number; }; }) => void
>a : any
>b : number
>1 : number
>{ b: 1 } : { b: number; }
>{ b: 1 } : { b?: number; }
>b : number
>1 : number

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/declarationEmitDestructuring2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function h1(_a) {

//// [declarationEmitDestructuring2.d.ts]
declare function f({x, y: [a, b, c, d]}?: {
x: number;
y: [number, number, number, number];
x?: number;
y?: [number, number, number, number];
}): void;
declare function g([a, b, c, d]?: [number, number, number, number]): void;
declare function h([a, [b], [[c]], {x, y: [a, b, c], z: {a1, b1}}]: [any, [any], [[any]], {
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/declarationEmitDestructuring4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/declarationEmitDestructuring4.ts(9,22): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.


==== tests/cases/compiler/declarationEmitDestructuring4.ts (1 errors) ====
// For an array binding pattern with empty elements,
// we will not make any modification and will emit
// the similar binding pattern users' have written
function baz([]) { }
function baz1([] = [1,2,3]) { }
function baz2([[]] = [[1,2,3]]) { }

function baz3({}) { }
function baz4({} = { x: 10 }) { }
~
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.


21 changes: 0 additions & 21 deletions tests/baselines/reference/declarationEmitDestructuring4.symbols

This file was deleted.

32 changes: 0 additions & 32 deletions tests/baselines/reference/declarationEmitDestructuring4.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts(4,6): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts(4,11): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts(4,16): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.


==== tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts (3 errors) ====
var [x10, [y10, [z10]]] = [1, ["hello", [true]]];

var [x11 = 0, y11 = ""] = [1, "hello"];
var [a11, b11, c11] = [];
~~~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
~~~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
~~~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.

var [a2, [b2, { x12, y12: c2 }]=["abc", { x12: 10, y12: false }]] = [1, ["hello", { x12: 5, y12: true }]];

var [x13, y13] = [1, "hello"];
var [a3, b3] = [[x13, y13], { x: x13, y: y13 }];

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(2,13): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(2,19): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(3,23): error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(4,16): error TS2353: Object literal may only specify known properties, and 'x5' does not exist in type '{ y5: any; }'.
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(6,27): error TS2353: Object literal may only specify known properties, and 'y7' does not exist in type '{ x7: any; }'.
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(7,20): error TS2353: Object literal may only specify known properties, and 'x8' does not exist in type '{ y8: any; }'.


==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (6 errors) ====

var { } = { x: 5, y: "hello" };
~
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
~
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
var { x4 } = { x4: 5, y4: "hello" };
~~
!!! error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.
var { y5 } = { x5: 5, y5: "hello" };
~~
!!! error TS2353: Object literal may only specify known properties, and 'x5' does not exist in type '{ y5: any; }'.
var { x6, y6 } = { x6: 5, y6: "hello" };
var { x7: a1 } = { x7: 5, y7: "hello" };
~~
!!! error TS2353: Object literal may only specify known properties, and 'y7' does not exist in type '{ x7: any; }'.
var { y8: b1 } = { x8: 5, y8: "hello" };
~~
!!! error TS2353: Object literal may only specify known properties, and 'x8' does not exist in type '{ y8: any; }'.
var { x9: a2, y9: b2 } = { x9: 5, y9: "hello" };

var { a: x11, b: { a: y11, b: { a: z11 }}} = { a: 1, b: { a: "hello", b: { a: true } } };

function f15() {
var a4 = "hello";
var b4 = 1;
var c4 = true;
return { a4, b4, c4 };
}
var { a4, b4, c4 } = f15();

module m {
export var { a4, b4, c4 } = f15();
}
Loading