8000 Merge branch 'master' of https://github.com/Microsoft/TypeScript into… · rvdn/TypeScript@3e5711f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e5711f

Browse files
committed
Merge branch 'master' of https://github.com/Microsoft/TypeScript into defaultInitializer
2 parents 4673d61 + d0bc070 commit 3e5711f

12 files changed

+144
-49
lines changed

src/compiler/checker.ts

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10449,9 +10449,17 @@ namespace ts {
1044910449
// TS 1.0 spec (April 2014): 8.3.2
1045010450
// Constructors of classes with no extends clause may not contain super calls, whereas
1045110451
// constructors of derived classes must contain at least one super call somewhere in their function body.
10452-
if (getClassExtendsHeritageClauseElement(<ClassDeclaration>node.parent)) {
10452+
let containingClassDecl = <ClassDeclaration>node.parent;
10453+
if (getClassExtendsHeritageClauseElement(containingClassDecl)) {
10454+
let containingClassSymbol = getSymbolOfNode(containingClassDecl);
10455+
let containingClassInstanceType = <InterfaceType>getDeclaredTypeOfSymbol(containingClassSymbol);
10456+
let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType);
1045310457

1045410458
if (containsSuperCall(node.body)) {
10459+
if (baseConstructorType === nullType) {
10460+
error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
10461+
}
10462+
1045510463
// The first statement in the body of a constructor (excluding prologue directives) must be a super call
1045610464
// if both of the following are true:
1045710465
// - The containing class is a derived class.
@@ -10484,7 +10492,7 @@ namespace ts {
1048410492
}
1048510493
}
1048610494
}
10487-
else {
10495+
else if (baseConstructorType !== nullType) {
1048810496
error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call);
1048910497
}
1049010498
}
@@ -12777,28 +12785,7 @@ namespace ts {
1277712785
}
1277812786
let initializer = member.initializer;
1277912787
if (initializer) {
12780-
autoValue = getConstantValueForEnumMemberInitializer(initializer);
12781-
if (autoValue === undefined) {
12782-
if (enumIsConst) {
12783-
error(initializer, Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression);
12784-
}
12785-
else if (!ambient) {
12786-
// Only here do we need to check that the initializer is assignable to the enum type.
12787-
// If it is a constant value (not undefined), it is syntactically constrained to be a number.
12788-
// Also, we do not need to check this for ambients because there is already
12789-
// a syntax error if it is not a constant.
12790-
checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined);
12791-
}
12792-
}
12793-
else if (enumIsConst) {
12794-
if (isNaN(autoValue)) {
12795-
error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN);
12796-
}
12797-
else if (!isFinite(autoValue)) {
12798-
error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value);
12799-
}
12800-
}
12801-
12788+
autoValue = computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient);
1280212789
}
1280312790
else if (ambient && !enumIsConst) {
1280412791
autoValue = undefined;
@@ -12812,8 +12799,36 @@ namespace ts {
1281212799
nodeLinks.flags |= NodeCheckFlags.EnumValuesComputed;
1281312800
}
1281412801

12815-
function getConstantValueForEnumMemberInitializer(initializer: Expression): number {
12816-
return evalConstant(initializer);
12802+
function computeConstantValueForEnumMemberInitializer(initializer: Expression, enumType: Type, enumIsConst: boolean, ambient: boolean): number {
12803+
// Controls if error should be reported after evaluation of constant value is completed
12804+
// Can be false if another more precise error was already reported during evaluation.
12805+
let reportError = true;
12806+
let value = evalConstant(initializer);
12807+
12808+
if (reportError) {
12809+
if (value === undefined) {
12810+
if (enumIsConst) {
12811+
error(initializer, Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression);
12812+
}
12813+
else if (!ambient) {
12814+
// Only here do we need to check that the initializer is assignable to the enum type.
12815+
// If it is a constant value (not undefined), it is syntactically constrained to be a number.
12816+
// Also, we do not need to check this for ambients because there is already
12817+
// a syntax error if it is not a constant.
12818+
checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined);
12819+
}
12820+
}
12821+
else if (enumIsConst) {
12822+
if (isNaN(value)) {
12823+
error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN);
12824+
}
12825+
else if (!isFinite(value)) {
12826+
error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value);
12827+
}
12828+
}
12829+
}
12830+
12831+
return value;
1281712832

1281812833
function evalConstant(e: Node): number {
1281912834
switch (e.kind) {
@@ -12922,6 +12937,8 @@ namespace ts {
1292212937

1292312938
// illegal case: forward reference
1292412939
if (!isDefinedBefore(propertyDecl, member)) {
12940+
reportError = false;
12941+
error(e, Diagnostics.A_member_initializer_in_a_const_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_const_enums);
1292512942
return undefined;
1292612943
}
1292712944

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 3 additions & 1 deletion
8000
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ namespace ts {
425425
JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property: { code: 2607, category: DiagnosticCategory.Error, key: "JSX element class does not support attributes because it does not have a '{0}' property" },
426426
The_global_type_JSX_0_may_not_have_more_than_one_property: { code: 2608, category: DiagnosticCategory.Error, key: "The global type 'JSX.{0}' may not have more than one property" },
427427
Cannot_emit_namespaced_JSX_elements_in_React: { code: 2650, category: DiagnosticCategory.Error, key: "Cannot emit namespaced JSX elements in React" },
428-
Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead: { code: 2651, category: DiagnosticCategory.Error, key: "Merged declaration '{0}' cannot include a default export declaration. Consider adding a separate 'export default {0}' declaration instead." },
428+
A_member_initializer_in_a_const_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_const_enums: { code: 2651, category: DiagnosticCategory.Error, key: "A member initializer in a 'const' enum declaration cannot reference members declared after it, including members defined in other 'const' enums." },
429+
Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead: { code: 2652, category: DiagnosticCategory.Error, key: "Merged declaration '{0}' cannot include a default export declaration. Consider adding a separate 'export default {0}' declaration instead." },
429430
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
430431
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
431432
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
@@ -614,5 +615,6 @@ namespace ts {
614615
Expected_corresponding_JSX_closing_tag_for_0: { code: 17002, category: DiagnosticCategory.Error, key: "Expected corresponding JSX closing tag for '{0}'." },
615616
JSX_attribute_expected: { code: 17003, category: DiagnosticCategory.Error, key: "JSX attribute expected." },
616617
Cannot_use_JSX_unless_the_jsx_flag_is_provided: { code: 17004, category: DiagnosticCategory.Error, key: "Cannot use JSX unless the '--jsx' flag is provided." },
618+
A_constructor_cannot_contain_a_super_call_when_its_class_extends_null: { code: 17005, category: DiagnosticCategory.Error, key: "A constructor cannot contain a 'super' call when its class extends 'null'" },
617619
};
618620
}

src/compiler/diagnosticMessages.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"Unterminated string literal.": {
33
"category": "Error",
44
"code": 1002
@@ -1689,9 +1689,13 @@
16891689
"category": "Error",
16901690
"code": 2650
16911691
},
1692-
"Merged declaration '{0}' cannot include a default export declaration. Consider adding a separate 'export default {0}' declaration instead.": {
1692+
"A member initializer in a 'const' enum declaration cannot reference members declared after it, including members defined in other 'const' enums.": {
16931693
"category": "Error",
16941694
"code": 2651
1695+
},
1696+
"Merged declaration '{0}' cannot include a default export declaration. Consider adding a separate 'export default {0}' declaration instead.": {
1697+
"category": "Error",
1698+
"code": 2652
16951699
},
16961700
"Import declaration '{0}' is using private name '{1}'.": {
16971701
"category": "Error",
@@ -2449,5 +2453,9 @@
24492453
"Cannot use JSX unless the '--jsx' flag is provided.": {
24502454
"category": "Error",
24512455
"code": 17004
2456+
},
2457+
"A constructor cannot contain a 'super' call when its class extends 'null'": {
2458+
"category": "Error",
2459+
"code": 17005
24522460
}
24532461
}

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,10 +1779,10 @@ namespace ts {
17791779
StringLike = String | StringLiteral,
17801780
NumberLike = Number | Enum,
17811781
ObjectType = Class | Interface | Reference | Tuple | Anonymous,
1782-
UnionOrIntersection = Union | Intersection,
1782+
UnionOrIntersection = Union | Intersection,
17831783
StructuredType = ObjectType | Union | Intersection,
17841784
/* @internal */
1785-
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
1785+
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
17861786
}
17871787

17881788
// Properties common to all types
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/compiler/classExtendsNull.ts(2,5): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
2+
3+
4+
==== tests/cases/compiler/classExtendsNull.ts (1 errors) ====
5+
class C extends null {
6+
constructor() {
7+
~~~~~~~~~~~~~~~
8+
super();
9+
~~~~~~~~~~~~~~~~
10+
return Object.create(null);
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
}
13+
~~~~~
14+
!!! error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
15+
}
16+
17+
class D extends null {
18+
constructor() {
19+
return Object.create(null);
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [classExtendsNull.ts]
2+
class C extends null {
3+
constructor() {
4+
super();
5+
return Object.create(null);
6+
}
7+
}
8+
9+
class D extends null {
10+
constructor() {
11+
return Object.create(null);
12+
}
13+
}
14+
15+
//// [classExtendsNull.js]
16+
var __extends = (this && this.__extends) || function (d, b) {
17+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
18+
function __() { this.constructor = d; }
19+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20+
};
21+
var C = (function (_super) {
22+
__extends(C, _super);
23+
function C() {
24+
_super.call(this);
25+
return Object.create(null);
26+
}
27+
return C;
28+
})(null);
29+
var D = (function (_super) {
30+
__extends(D, _super);
31+
function D() {
32+
return Object.create(null);
33+
}
34+
return D;
35+
})(null);

tests/baselines/reference/constEnumErrors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/compiler/constEnumErrors.ts(1,12): error TS2300: Duplicate identifier 'E'.
22
tests/cases/compiler/constEnumErrors.ts(5,8): error TS2300: Duplicate identifier 'E'.
3-
tests/cases/compiler/constEnumErrors.ts(12,9): error TS2474: In 'const' enum declarations member initializer must be constant expression.
3+
tests/cases/compiler/constEnumErrors.ts(12,9): error TS2651: A member initializer in a 'const' enum declaration cannot reference members declared after it, including members defined in other 'const' enums.
44
tests/cases/compiler/constEnumErrors.ts(14,9): error TS2474: In 'const' enum declarations member initializer must be constant expression.
55
tests/cases/compiler/constEnumErrors.ts(15,10): error TS2474: In 'const' enum declarations member initializer must be constant expression.
66
tests/cases/compiler/constEnumErrors.ts(22,13): error TS2476: A const enum member can only be accessed using a string literal.
@@ -31,7 +31,7 @@ tests/cases/compiler/constEnumErrors.ts(42,9): error TS2478: 'const' enum member
3131
// forward reference to the element of the same enum
3232
X = Y,
3333
~
34-
!!! error TS2474: In 'const' enum declarations member initializer must be constant expression.
34+
!!! error TS2651: A member initializer in a 'const' enum declaration cannot reference members declared after it, including members defined in other 'const' enums.
3535
// forward reference to the element of the same enum
3636
Y = E1.Z,
3737
~~~~

tests/baselines/reference/defaultExportsCannotMerge01.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/es6/modules/m1.ts(2,25): error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
2-
tests/cases/conformance/es6/modules/m1.ts(11,18): error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
1+
tests/cases/conformance/es6/modules/m1.ts(2,25): error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
2+
tests/cases/conformance/es6/modules/m1.ts(11,18): error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
33
tests/cases/conformance/es6/modules/m2.ts(5,8): error TS2304: Cannot find name 'Entity'.
44
tests/cases/conformance/es6/modules/m2.ts(6,8): error TS2503: Cannot find namespace 'Entity'.
55
tests/cases/conformance/es6/modules/m2.ts(8,8): error TS2339: Property 'x' does not exist on type '() => number'.
@@ -10,7 +10,7 @@ tests/cases/conformance/es6/modules/m2.ts(9,8): error TS2339: Property 'y' does
1010

1111
export default function Decl() {
1212
~~~~
13-
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
13+
!!! error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
1414
return 0;
1515
}
1616

@@ -21,7 +21,7 @@ tests/cases/conformance/es6/modules/m2.ts(9,8): error TS2339: Property 'y' does
2121

2222
export namespace Decl {
2323
~~~~
24-
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
24+
!!! error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
2525
export var x = 10;
2626
export var y = 20;
2727

tests/baselines/reference/defaultExportsCannotMerge02.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/es6/modules/m1.ts(2,22): error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
2-
tests/cases/conformance/es6/modules/m1.ts(5,18): error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
1+
tests/cases/conformance/es6/modules/m1.ts(2,22): error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
2+
tests/cases/conformance/es6/modules/m1.ts(5,18): error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
33
tests/cases/conformance/es6/modules/m2.ts(3,1): error TS2348: Value of type 'typeof Decl' is not callable. Did you mean to include 'new'?
44
tests/cases/conformance/es6/modules/m2.ts(6,8): error TS2503: Cannot find namespace 'Entity'.
55
tests/cases/conformance/es6/modules/m2.ts(8,13): error TS2339: Property 'p1' does not exist on type 'Decl'.
@@ -10,12 +10,12 @@ tests/cases/conformance/es6/modules/m2.ts(8,20): error TS2339: Property 'p2' doe
1010

1111
export default class Decl {
1212
~~~~
13-
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
13+
!!! error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
1414
}
1515

1616
export interface Decl {
1717
~~~~
18-
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
18+
!!! error TS2652: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
1919
p1: number;
2020
p2: number;
2121
}

0 commit comments

Comments
 (0)
0