Skip to content

Commit a9e79bf

Browse files
committed
Lazy resolution of global decorator types
1 parent 4e78464 commit a9e79bf

7 files changed

Lines changed: 53 additions & 94 deletions

File tree

src/compiler/checker.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,10 @@ module ts {
114114
let globalIterableType: ObjectType;
115115

116116
let anyArrayType: Type;
117-
let globalTypedPropertyDescriptorType: ObjectType;
118-
let globalClassDecoratorType: ObjectType;
119-
let globalParameterDecoratorType: ObjectType;
120-
let globalPropertyDecoratorType: ObjectType;
121-
let globalMethodDecoratorType: ObjectType;
117+
let getGlobalClassDecoratorType: () => ObjectType;
118+
let getGlobalParameterDecoratorType: () => ObjectType;
119+
let getGlobalPropertyDecoratorType: () => ObjectType;
120+
let getGlobalMethodDecoratorType: () => ObjectType;
122121

123122
let tupleTypes: Map<TupleType> = {};
124123
let unionTypes: Map<UnionType> = {};
@@ -8790,24 +8789,24 @@ module ts {
87908789
case SyntaxKind.ClassDeclaration:
87918790
let classSymbol = getSymbolOfNode(node.parent);
87928791
let classConstructorType = getTypeOfSymbol(classSymbol);
8793-
let classDecoratorType = instantiateSingleCallFunctionType(globalClassDecoratorType, [classConstructorType]);
8792+
let classDecoratorType = instantiateSingleCallFunctionType(getGlobalClassDecoratorType(), [classConstructorType]);
87948793
checkTypeAssignableTo(exprType, classDecoratorType, node);
87958794
break;
87968795

87978796
case SyntaxKind.PropertyDeclaration:
8798-
checkTypeAssignableTo(exprType, globalPropertyDecoratorType, node);
8797+
checkTypeAssignableTo(exprType, getGlobalPropertyDecoratorType(), node);
87998798
break;
88008799

88018800
case SyntaxKind.MethodDeclaration:
88028801
case SyntaxKind.GetAccessor:
88038802
case SyntaxKind.SetAccessor:
88048803
let methodType = getTypeOfNode(node.parent);
8805-
let methodDecoratorType = instantiateSingleCallFunctionType(globalMethodDecoratorType, [methodType]);
8804+
let methodDecoratorType = instantiateSingleCallFunctionType(getGlobalMethodDecoratorType(), [methodType]);
88068805
checkTypeAssignableTo(exprType, methodDecoratorType, node);
88078806
break;
88088807

88098808
case SyntaxKind.Parameter:
8810-
checkTypeAssignableTo(exprType, globalParameterDecoratorType, node);
8809+
checkTypeAssignableTo(exprType, getGlobalParameterDecoratorType(), node);
88118810
break;
88128811
}
88138812
}
@@ -11969,11 +11968,10 @@ module ts {
1196911968
globalNumberType = getGlobalType("Number");
1197011969
globalBooleanType = getGlobalType("Boolean");
1197111970
globalRegExpType = getGlobalType("RegExp");
11972-
globalTypedPropertyDescriptorType = getTypeOfGlobalSymbol(getGlobalTypeSymbol("TypedPropertyDescriptor"), 1);
11973-
globalClassDecoratorType = getGlobalType("ClassDecorator");
11974-
globalPropertyDecoratorType = getGlobalType("PropertyDecorator");
11975-
globalMethodDecoratorType = getGlobalType("MethodDecorator");
11976-
globalParameterDecoratorType = getGlobalType("ParameterDecorator");
11971+
getGlobalClassDecoratorType = memoize(() => getGlobalType("ClassDecorator"));
11972+
getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator"));
11973+
getGlobalMethodDecoratorType = memoize(() => getGlobalType("MethodDecorator"));
11974+
getGlobalParameterDecoratorType = memoize(() => getGlobalType("ParameterDecorator"));
1197711975

1197811976
// If we're in ES6 mode, load the TemplateStringsArray.
1197911977
// Otherwise, default to 'unknown' for the purposes of type checking in LS scenarios.

src/compiler/core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ module ts {
281281
return result;
282282
}
283283

284+
export function memoize<T>(callback: () => T): () => T {
285+
let value: T;
286+
return () => {
287+
if (callback) {
288+
value = callback();
289+
callback = undefined;
290+
}
291+
return value;
292+
};
293+
}
294+
284295
function formatStringFromArgs(text: string, args: { [index: number]: any; }, baseIndex?: number): string {
285296
baseIndex = baseIndex || 0;
286297

tests/baselines/reference/noDefaultLib.errors.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
2-
error TS2318: Cannot find global type 'PropertyDecorator'.
3-
error TS2318: Cannot find global type 'ParameterDecorator'.
4-
error TS2318: Cannot find global type 'MethodDecorator'.
51
error TS2318: Cannot find global type 'IArguments'.
6-
error TS2318: Cannot find global type 'ClassDecorator'.
72
error TS2318: Cannot find global type 'Boolean'.
83
tests/cases/compiler/noDefaultLib.ts(4,11): error TS2317: Global type 'Array' must have 1 type parameter(s).
94

105

11-
!!! error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
12-
!!! error TS2318: Cannot find global type 'PropertyDecorator'.
13-
!!! error TS2318: Cannot find global type 'ParameterDecorator'.
14-
!!! error TS2318: Cannot find global type 'MethodDecorator'.
156
!!! error TS2318: Cannot find global type 'IArguments'.
16-
!!! error TS2318: Cannot find global type 'ClassDecorator'.
177
!!! error TS2318: Cannot find global type 'Boolean'.
188
==== tests/cases/compiler/noDefaultLib.ts (1 errors) ====
199
/// <reference no-default-lib="true"/>

tests/baselines/reference/parser509698.errors.txt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
1-
error TS2318: Cannot find global type 'Number'.
2-
error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
3-
error TS2318: Cannot find global type 'Object'.
4-
error TS2318: Cannot find global type 'Array'.
5-
error TS2318: Cannot find global type 'ClassDecorator'.
61
error TS2318: Cannot find global type 'String'.
72
error TS2318: Cannot find global type 'RegExp'.
8-
error TS2318: Cannot find global type 'PropertyDecorator'.
9-
error TS2318: Cannot find global type 'ParameterDecorator'.
3+
error TS2318: Cannot find global type 'Object'.
4+
error TS2318: Cannot find global type 'Number'.
5+
error TS2318: Cannot find global type 'IArguments'.
106
error TS2318: Cannot find global type 'Function'.
117
error TS2318: Cannot find global type 'Boolean'.
12-
error TS2318: Cannot find global type 'MethodDecorator'.
13-
error TS2318: Cannot find global type 'IArguments'.
8+
error TS2318: Cannot find global type 'Array'.
149

1510

16-
!!! error TS2318: Cannot find global type 'Number'.
17-
!!! error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
18-
!!! error TS2318: Cannot find global type 'Object'.
19-
!!! error TS2318: Cannot find global type 'Array'.
20-
!!! error TS2318: Cannot find global type 'ClassDecorator'.
2111
!!! error TS2318: Cannot find global type 'String'.
2212
!!! error TS2318: Cannot find global type 'RegExp'.
23-
!!! error TS2318: Cannot find global type 'PropertyDecorator'.
24-
!!! error TS2318: Cannot find global type 'ParameterDecorator'.
13+
!!! error TS2318: Cannot find global type 'Object'.
14+
!!! error TS2318: Cannot find global type 'Number'.
15+
!!! error TS2318: Cannot find global type 'IArguments'.
2516
!!! error TS2318: Cannot find global type 'Function'.
2617
!!! error TS2318: Cannot find global type 'Boolean'.
27-
!!! error TS2318: Cannot find global type 'MethodDecorator'.
28-
!!! error TS2318: Cannot find global type 'IArguments'.
18+
!!! error TS2318: Cannot find global type 'Array'.
2919
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser509698.ts (0 errors) ====
3020
/// <style requireSemi="on" />
3121
/// <reference no-default-lib="true"/>

tests/baselines/reference/project/noDefaultLib/amd/noDefaultLib.errors.txt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
error TS2318: Cannot find global type 'Object'.
2-
error TS2318: Cannot find global type 'Array'.
3-
error TS2318: Cannot find global type 'ParameterDecorator'.
4-
error TS2318: Cannot find global type 'ClassDecorator'.
5-
error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
61
error TS2318: Cannot find global type 'String'.
72
error TS2318: Cannot find global type 'RegExp'.
8-
error TS2318: Cannot find global type 'PropertyDecorator'.
9-
error TS2318: Cannot find global type 'Function'.
10-
error TS2318: Cannot find global type 'Boolean'.
3+
error TS2318: Cannot find global type 'Object'.
114
error TS2318: Cannot find global type 'Number'.
12-
error TS2318: Cannot find global type 'MethodDecorator'.
135
error TS2318: Cannot find global type 'IArguments'.
6+
error TS2318: Cannot find global type 'Function'.
7+
error TS2318: Cannot find global type 'Boolean'.
8+
error TS2318: Cannot find global type 'Array'.
149
test.ts(3,8): error TS2304: Cannot find name 'Array'.
1510

1611

17-
!!! error TS2318: Cannot find global type 'Object'.
18-
!!! error TS2318: Cannot find global type 'Array'.
19-
!!! error TS2318: Cannot find global type 'ParameterDecorator'.
20-
!!! error TS2318: Cannot find global type 'ClassDecorator'.
21-
!!! error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
2212
!!! error TS2318: Cannot find global type 'String'.
2313
!!! error TS2318: Cannot find global type 'RegExp'.
24-
!!! error TS2318: Cannot find global type 'PropertyDecorator'.
25-
!!! error TS2318: Cannot find global type 'Function'.
26-
!!! error TS2318: Cannot find global type 'Boolean'.
14+
!!! error TS2318: Cannot find global type 'Object'.
2715
!!! error TS2318: Cannot find global type 'Number'.
28-
!!! error TS2318: Cannot find global type 'MethodDecorator'.
2916
!!! error TS2318: Cannot find global type 'IArguments'.
17+
!!! error TS2318: Cannot find global type 'Function'.
18+
!!! error TS2318: Cannot find global type 'Boolean'.
19+
!!! error TS2318: Cannot find global type 'Array'.
3020
==== test.ts (1 errors) ====
3121
/// <reference no-default-lib="true"/>
3222

tests/baselines/reference/project/noDefaultLib/node/noDefaultLib.errors.txt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
error TS2318: Cannot find global type 'Object'.
2-
error TS2318: Cannot find global type 'Array'.
3-
error TS2318: Cannot find global type 'ParameterDecorator'.
4-
error TS2318: Cannot find global type 'ClassDecorator'.
5-
error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
61
error TS2318: Cannot find global type 'String'.
72
error TS2318: Cannot find global type 'RegExp'.
8-
error TS2318: Cannot find global type 'PropertyDecorator'.
9-
error TS2318: Cannot find global type 'Function'.
10-
error TS2318: Cannot find global type 'Boolean'.
3+
error TS2318: Cannot find global type 'Object'.
114
error TS2318: Cannot find global type 'Number'.
12-
error TS2318: Cannot find global type 'MethodDecorator'.
135
error TS2318: Cannot find global type 'IArguments'.
6+
error TS2318: Cannot find global type 'Function'.
7+
error TS2318: Cannot find global type 'Boolean'.
8+
error TS2318: Cannot find global type 'Array'.
149
test.ts(3,8): error TS2304: Cannot find name 'Array'.
1510

1611

17-
!!! error TS2318: Cannot find global type 'Object'.
18-
!!! error TS2318: Cannot find global type 'Array'.
19-
!!! error TS2318: Cannot find global type 'ParameterDecorator'.
20-
!!! error TS2318: Cannot find global type 'ClassDecorator'.
21-
!!! error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
2212
!!! error TS2318: Cannot find global type 'String'.
2313
!!! error TS2318: Cannot find global type 'RegExp'.
24-
!!! error TS2318: Cannot find global type 'PropertyDecorator'.
25-
!!! error TS2318: Cannot find global type 'Function'.
26-
!!! error TS2318: Cannot find global type 'Boolean'.
14+
!!! error TS2318: Cannot find global type 'Object'.
2715
!!! error TS2318: Cannot find global type 'Number'.
28-
!!! error TS2318: Cannot find global type 'MethodDecorator'.
2916
!!! error TS2318: Cannot find global type 'IArguments'.
17+
!!! error TS2318: Cannot find global type 'Function'.
18+
!!! error TS2318: Cannot find global type 'Boolean'.
19+
!!! error TS2318: Cannot find global type 'Array'.
3020
==== test.ts (1 errors) ====
3121
/// <reference no-default-lib="true"/>
3222

tests/baselines/reference/typeCheckTypeArgument.errors.txt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
error TS2318: Cannot find global type 'PropertyDecorator'.
2-
error TS2318: Cannot find global type 'Array'.
3-
error TS2318: Cannot find global type 'RegExp'.
4-
error TS2318: Cannot find global type 'ClassDecorator'.
51
error TS2318: Cannot find global type 'String'.
2+
error TS2318: Cannot find global type 'Array'.
63
error TS2318: Cannot find global type 'IArguments'.
7-
error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
8-
error TS2318: Cannot find global type 'Number'.
94
error TS2318: Cannot find global type 'Boolean'.
5+
error TS2318: Cannot find global type 'RegExp'.
106
error TS2318: Cannot find global type 'Object'.
11-
error TS2318: Cannot find global type 'MethodDecorator'.
7+
error TS2318: Cannot find global type 'Number'.
128
error TS2318: Cannot find global type 'Function'.
13-
error TS2318: Cannot find global type 'ParameterDecorator'.
149
tests/cases/compiler/typeCheckTypeArgument.ts(3,19): error TS2304: Cannot find name 'UNKNOWN'.
1510
tests/cases/compiler/typeCheckTypeArgument.ts(5,26): error TS2304: Cannot find name 'UNKNOWN'.
1611
tests/cases/compiler/typeCheckTypeArgument.ts(7,21): error TS2304: Cannot find name 'UNKNOWN'.
@@ -19,19 +14,14 @@ tests/cases/compiler/typeCheckTypeArgument.ts(12,22): error TS2304: Cannot find
1914
tests/cases/compiler/typeCheckTypeArgument.ts(15,13): error TS2304: Cannot find name 'UNKNOWN'.
2015

2116

22-
!!! error TS2318: Cannot find global type 'PropertyDecorator'.
23-
!!! error TS2318: Cannot find global type 'Array'.
24-
!!! error TS2318: Cannot find global type 'RegExp'.
25-
!!! error TS2318: Cannot find global type 'ClassDecorator'.
2617
!!! error TS2318: Cannot find global type 'String'.
18+
!!! error TS2318: Cannot find global type 'Array'.
2719
!!! error TS2318: Cannot find global type 'IArguments'.
28-
!!! error TS2318: Cannot find global type 'TypedPropertyDescriptor'.
29-
!!! error TS2318: Cannot find global type 'Number'.
3020
!!! error TS2318: Cannot find global type 'Boolean'.
21+
!!! error TS2318: Cannot find global type 'RegExp'.
3122
!!! error TS2318: Cannot find global type 'Object'.
32-
!!! error TS2318: Cannot find global type 'MethodDecorator'.
23+
!!! error TS2318: Cannot find global type 'Number'.
3324
!!! error TS2318: Cannot find global type 'Function'.
34-
!!! error TS2318: Cannot find global type 'ParameterDecorator'.
3525
==== tests/cases/compiler/typeCheckTypeArgument.ts (6 errors) ====
3626
/// <reference no-default-lib="true"/>
3727

0 commit comments

Comments
 (0)