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

Skip to content

Commit 40d6c7f

Browse files
committed
Merge branch 'master' of https://github.com/Microsoft/TypeScript into defaultInitializer
2 parents 628d63c + 266e5e6 commit 40d6c7f

34 files changed

+913
-131
lines changed

src/compiler/checker.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4381,11 +4381,11 @@ namespace ts {
43814381
return getInferredType(context, i);
43824382
}
43834383
}
4384-
return t;
4384+
return t;
43854385
};
43864386

43874387
mapper.context = context;
4388-
return mapper;
4388+
return mapper;
43894389
}
43904390

43914391
function identityMapper(type: Type): Type {
@@ -7341,10 +7341,9 @@ namespace ts {
73417341
* For example, in the element <MyClass>, the element instance type is `MyClass` (not `typeof MyClass`).
73427342
*/
73437343
function getJsxElementInstanceType(node: JsxOpeningLikeElement) {
7344-
if (!(getNodeLinks(node).jsxFlags & JsxFlags.ClassElement)) {
7345-
// There is no such thing as an instance type for a non-class element
7346-
return undefined;
7347-
}
7344+
// There is no such thing as an instance type for a non-class element. This
7345+
// line shouldn't be hit.
7346+
Debug.assert(!!(getNodeLinks(node).jsxFlags & JsxFlags.ClassElement), 'Should not call getJsxElementInstanceType on non-class Element');
73487347

73497348
let classSymbol = getJsxElementTagSymbol(node);
73507349
if (classSymbol === unknownSymbol) {
@@ -7367,16 +7366,11 @@ namespace ts {
73677366
if (signatures.length === 0) {
73687367
// We found no signatures at all, which is an error
73697368
error(node.tagName, Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, getTextOfNode(node.tagName));
7370-
return undefined;
7369+
return unknownType;
73717370
}
73727371
}
73737372

7374-
// Check that the constructor/factory returns an object type
7375-
let returnType = getUnionType(signatures.map(s => getReturnTypeOfSignature(s)));
7376-
if (!isTypeAny(returnType) && !(returnType.flags & TypeFlags.ObjectType)) {
7377-
error(node.tagName, Diagnostics.The_return_type_of_a_JSX_element_constructor_must_return_an_object_type);
7378-
return undefined;
7379-
}
7373+
let returnType = getUnionType(signatures.map(getReturnTypeOfSignature));
73807374

73817375
// Issue an error if this return type isn't assignable to JSX.ElementClass
73827376
let elemClassType = getJsxGlobalElementClassType();
@@ -7437,7 +7431,7 @@ namespace ts {
74377431
let elemInstanceType = getJsxElementInstanceType(node);
74387432

74397433
if (isTypeAny(elemInstanceType)) {
7440-
return links.resolvedJsxType = anyType;
7434+
return links.resolvedJsxType = elemInstanceType;
74417435
}
74427436

74437437
let propsName = getJsxElementPropertiesName();
@@ -10880,9 +10874,6 @@ namespace ts {
1088010874
return;
1088110875
}
1088210876

10883-
// Exports should be checked only if enclosing module contains both exported and non exported declarations.
10884-
// In case if all declarations are non-exported check is unnecessary.
10885-
1088610877
// if localSymbol is defined on node then node itself is exported - check is required
1088710878
let symbol = node.localSymbol;
1088810879
if (!symbol) {
@@ -10902,27 +10893,45 @@ namespace ts {
1090210893

1090310894
// we use SymbolFlags.ExportValue, SymbolFlags.ExportType and SymbolFlags.ExportNamespace
1090410895
// to denote disjoint declarationSpaces (without making new enum type).
10905-
let exportedDeclarationSpaces: SymbolFlags = 0;
10906-
let nonExportedDeclarationSpaces: SymbolFlags = 0;
10907-
forEach(symbol.declarations, d => {
10896+
let exportedDeclarationSpaces = SymbolFlags.None;
10897+
let nonExportedDeclarationSpaces = SymbolFlags.None;
10898+
let defaultExportedDeclarationSpaces = SymbolFlags.None;
10899+
for (let d of symbol.declarations) {
1090810900
let declarationSpaces = getDeclarationSpaces(d);
10909-
if (getEffectiveDeclarationFlags(d, NodeFlags.Export)) {
10910-
exportedDeclarationSpaces |= declarationSpaces;
10901+
let effectiveDeclarationFlags = getEffectiveDeclarationFlags(d, NodeFlags.Export | NodeFlags.Default);
10902+
10903+
if (effectiveDeclarationFlags & NodeFlags.Export) {
10904+
if (effectiveDeclarationFlags & NodeFlags.Default) {
10905+
defaultExportedDeclarationSpaces |= declarationSpaces;
10906+
}
10907+
else {
10908+
exportedDeclarationSpaces |= declarationSpaces;
10909+
}
1091110910
}
1091210911
else {
1091310912
nonExportedDeclarationSpaces |= declarationSpaces;
1091410913
}
10915-
});
10914+
}
1091610915

10917-
let commonDeclarationSpace = exportedDeclarationSpaces & nonExportedDeclarationSpaces;
10916+
// Spaces for anyting not declared a 'default export'.
10917+
let nonDefaultExportedDeclarationSpaces = exportedDeclarationSpaces | nonExportedDeclarationSpaces;
10918+
10919+
let commonDeclarationSpacesForExportsAndLocals = exportedDeclarationSpaces & nonExportedDeclarationSpaces;
10920+
let commonDeclarationSpacesForDefaultAndNonDefault = defaultExportedDeclarationSpaces & nonDefaultExportedDeclarationSpaces;
1091810921

10919-
if (commonDeclarationSpace) {
10922+
if (commonDeclarationSpacesForExportsAndLocals || commonDeclarationSpacesForDefaultAndNonDefault) {
1092010923
// declaration spaces for exported and non-exported declarations intersect
10921-
forEach(symbol.declarations, d => {
10922-
if (getDeclarationSpaces(d) & commonDeclarationSpace) {
10924+
for (let d of symbol.declarations) {
10925+
let declarationSpaces = getDeclarationSpaces(d);
10926+
10927+
// Only error on the declarations that conributed to the intersecting spaces.
10928+
if (declarationSpaces & commonDeclarationSpacesForDefaultAndNonDefault) {
10929+
error(d.name, Diagnostics.Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead, declarationNameToString(d.name));
10930+
}
10931+
else if (declarationSpaces & commonDeclarationSpacesForExportsAndLocals) {
1092310932
error(d.name, Diagnostics.Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local, declarationNameToString(d.name));
1092410933
}
10925-
});
10934+
}
1092610935
}
1092710936

1092810937
function getDeclarationSpaces(d: Declaration): SymbolFlags {

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ namespace ts {
294294
Multiple_constructor_implementations_are_not_allowed: { code: 2392, category: DiagnosticCategory.Error, key: "Multiple constructor implementations are not allowed." },
295295
Duplicate_function_implementation: { code: 2393, category: DiagnosticCategory.Error, key: "Duplicate function implementation." },
296296
Overload_signature_is_not_compatible_with_function_implementation: { code: 2394, category: DiagnosticCategory.Error, key: "Overload signature is not compatible with function implementation." },
297-
Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local: { code: 2395, category: DiagnosticCategory.Error, key: "Individual declarations in merged declaration {0} must be all exported or all local." },
297+
Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local: { code: 2395, category: DiagnosticCategory.Error, key: "Individual declarations in merged declaration '{0}' must be all exported or all local." },
298298
Duplicate_identifier_arguments_Compiler_uses_arguments_to_initialize_rest_parameters: { code: 2396, category: DiagnosticCategory.Error, key: "Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters." },
299299
Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference: { code: 2399, category: DiagnosticCategory.Error, key: "Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference." },
300300
Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference: { code: 2400, category: DiagnosticCategory.Error, key: "Expression resolves to variable declaration '_this' that compiler uses to capture 'this' reference." },
@@ -425,6 +425,7 @@ 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." },
428429
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
429430
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}'." },
430431
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}'." },

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@
11651165
"category": "Error",
11661166
"code": 2394
11671167
},
1168-
"Individual declarations in merged declaration {0} must be all exported or all local.": {
1168+
"Individual declarations in merged declaration '{0}' must be all exported or all local.": {
11691169
"category": "Error",
11701170
"code": 2395
11711171
},
@@ -1689,6 +1689,10 @@
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.": {
1693+
"category": "Error",
1694+
"code": 2651
1695+
},
16921696
"Import declaration '{0}' is using private name '{1}'.": {
16931697
"category": "Error",
16941698
"code": 4000

src/compiler/emitter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6841,6 +6841,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
68416841
return leadingComments;
68426842
}
68436843

6844+
/**
6845+
* Removes all but the pinned or triple slash comments.
6846+
* @param ranges The array to be filtered
6847+
* @param onlyPinnedOrTripleSlashComments whether the filtering should be performed.
6848+
*/
68446849
function filterComments(ranges: CommentRange[], onlyPinnedOrTripleSlashComments: boolean): CommentRange[] {
68456850
// If we're removing comments, then we want to strip out all but the pinned or
68466851
// triple slash comments.

src/compiler/tsc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace ts {
55
export interface SourceFile {
6-
fileWatcher: FileWatcher;
6+
fileWatcher?: FileWatcher;
77
}
88

99
/**

src/services/services.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,26 +261,25 @@ namespace ts {
261261
}
262262

263263
public getFirstToken(sourceFile?: SourceFile): Node {
264-
let children = this.getChildren();
265-
for (let child of children) {
266-
if (child.kind < SyntaxKind.FirstNode) {
267-
return child;
268-
}
269-
270-
return child.getFirstToken(sourceFile);
264+
let children = this.getChildren(sourceFile);
265+
if (!children.length) {
266+
return undefined;
271267
}
268+
269+
let child = children[0];
270+
271+
return child.kind < SyntaxKind.FirstNode ? child : child.getFirstToken(sourceFile);
272272
}
273273

274274
public getLastToken(sourceFile?: SourceFile): Node {
275275
let children = this.getChildren(sourceFile);
276-
for (let i = children.length - 1; i >= 0; i--) {
277-
let child = children[i];
278-
if (child.kind < SyntaxKind.FirstNode) {
279-
return child;
280-
}
281276

282-
return child.getLastToken(sourceFile);
277+
let child = lastOrUndefined(children);
278+
if (!child) {
279+
return undefined;
283280
}
281+
282+
return child.kind < SyntaxKind.FirstNode ? child : child.getLastToken(sourceFile);
284283
}
285284
}
286285

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.
3+
tests/cases/conformance/es6/modules/m2.ts(5,8): error TS2304: Cannot find name 'Entity'.
4+
tests/cases/conformance/es6/modules/m2.ts(6,8): error TS2503: Cannot find namespace 'Entity'.
5+
tests/cases/conformance/es6/modules/m2.ts(8,8): error TS2339: Property 'x' does not exist on type '() => number'.
6+
tests/cases/conformance/es6/modules/m2.ts(9,8): error TS2339: Property 'y' does not exist on type '() => number'.
7+
8+
9+
==== tests/cases/conformance/es6/modules/m1.ts (2 errors) ====
10+
11+
export default function Decl() {
12+
~~~~
13+
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
14+
return 0;
15+
}
16+
17+
export interface Decl {
18+
p1: number;
19+
p2: number;
20+
}
21+
22+
export namespace Decl {
23+
~~~~
24+
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
25+
export var x = 10;
26+
export var y = 20;
27+
28+
interface I {
29+
}
30+
}
31+
32+
==== tests/cases/conformance/es6/modules/m2.ts (4 errors) ====
33+
import Entity from "m1"
34+
35+
Entity();
36+
37+
var x: Entity;
38+
~~~~~~
39+
!!! error TS2304: Cannot find name 'Entity'.
40+
var y: Entity.I;
41+
~~~~~~
42+
!!! error TS2503: Cannot find namespace 'Entity'.
43+
44+
Entity.x;
45+
~
46+
!!! error TS2339: Property 'x' does not exist on type '() => number'.
47+
Entity.y;
48+
~
49+
!!! error TS2339: Property 'y' does not exist on type '() => number'.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [tests/cases/conformance/es6/modules/defaultExportsCannotMerge01.ts] ////
2+
3+
//// [m1.ts]
4+
5+
export default function Decl() {
6+
return 0;
7+
}
8+
9+
export interface Decl {
10+
p1: number;
11+
p2: number;
12+
}
13+
14+
export namespace Decl {
15+
export var x = 10;
16+
export var y = 20;
17+
18+
interface I {
19+
}
20+
}
21+
22+
//// [m2.ts]
23+
import Entity from "m1"
24+
25+
Entity();
26+
27+
var x: Entity;
28+
var y: Entity.I;
29+
30+
Entity.x;
31+
Entity.y;
32+
33+
//// [m1.js]
34+
function Decl() {
35+
return 0;
36+
}
37+
Object.defineProperty(exports, "__esModule", { value: true });
38+
exports.default = Decl;
39+
var Decl;
40+
(function (Decl) {
41+
Decl.x = 10;
42+
Decl.y = 20;
43+
})(Decl = exports.Decl || (exports.Decl = {}));
44+
//// [m2.js]
45+
var m1_1 = require("m1");
46+
m1_1.default();
47+
var x;
48+
var y;
49+
m1_1.default.x;
50+
m1_1.default.y;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.
3+
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'?
4+
tests/cases/conformance/es6/modules/m2.ts(6,8): error TS2503: Cannot find namespace 'Entity'.
5+
tests/cases/conformance/es6/modules/m2.ts(8,13): error TS2339: Property 'p1' does not exist on type 'Decl'.
6+
tests/cases/conformance/es6/modules/m2.ts(8,20): error TS2339: Property 'p2' does not exist on type 'Decl'.
7+
8+
9+
==== tests/cases/conformance/es6/modules/m1.ts (2 errors) ====
10+
11+
export default class Decl {
12+
~~~~
13+
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
14+
}
15+
16+
export interface Decl {
17+
~~~~
18+
!!! error TS2651: Merged declaration 'Decl' cannot include a default export declaration. Consider adding a separate 'export default Decl' declaration instead.
19+
p1: number;
20+
p2: number;
21+
}
22+
23+
export namespace Decl {
24+
interface I {
25+
}
26+
}
27+
28+
==== tests/cases/conformance/es6/modules/m2.ts (4 errors) ====
29+
import Entity from "m1"
30+
31+
Entity();
32+
~~~~~~~~
33+
!!! error TS2348: Value of type 'typeof Decl' is not callable. Did you mean to include 'new'?
34+
35+
var x: Entity;
36+
var y: Entity.I;
37+
~~~~~~
38+
!!! error TS2503: Cannot find namespace 'Entity'.
39+
var z = new Entity();
40+
var sum = z.p1 + z.p2
41+
~~
42+
!!! error TS2339: Property 'p1' does not exist on type 'Decl'.
43+
~~
44+
!!! error TS2339: Property 'p2' does not exist on type 'Decl'.

0 commit comments

Comments
 (0)
0