Skip to content

Commit a5eb68b

Browse files
committed
Lazy resolution of global decorator types
1 parent 70241fd commit a5eb68b

File tree

7 files changed

+53
-94
lines changed

7 files changed

+53
-94
lines changed

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> = {};
@@ -8808,24 +8807,24 @@ module ts {
88088807
case SyntaxKind.ClassDeclaration:
88098808
let classSymbol = getSymbolOfNode(node.parent);
88108809
let classConstructorType = getTypeOfSymbol(classSymbol);
8811-
let classDecoratorType = instantiateSingleCallFunctionType(globalClassDecoratorType, [classConstructorType]);
8810+
let classDecoratorType = instantiateSingleCallFunctionType(getGlobalClassDecoratorType(), [classConstructorType]);
88128811
checkTypeAssignableTo(exprType, classDecoratorType, node);
88138812
break;
88148813

88158814
case SyntaxKind.PropertyDeclaration:
8816-
checkTypeAssignableTo(exprType, globalPropertyDecoratorType, node);
8815+
checkTypeAssignableTo(exprType, getGlobalPropertyDecoratorType(), node);
88178816
break;
88188817

88198818
case SyntaxKind.MethodDeclaration:
88208819
case SyntaxKind.GetAccessor:
88218820
case SyntaxKind.SetAccessor:
88228821
let methodType = getTypeOfNode(node.parent);
8823-
let methodDecoratorType = instantiateSingleCallFunctionType(globalMethodDecoratorType, [methodType]);
8822+
let methodDecoratorType = instantiateSingleCallFunctionType(getGlobalMethodDecoratorType(), [methodType]);
88248823
checkTypeAssignableTo(exprType, methodDecoratorType, node);
88258824
break;
88268825

88278826
case SyntaxKind.Parameter:
8828-
checkTypeAssignableTo(exprType, globalParameterDecoratorType, node);
8827+
checkTypeAssignableTo(exprType, getGlobalParameterDecoratorType(), node);
88298828
break;
88308829
}
88318830
}
@@ -11987,11 +11986,10 @@ module ts {
1198711986
globalNumberType = getGlobalType("Number");
1198811987
globalBooleanType = getGlobalType("Boolean");
1198911988
globalRegExpType = getGlobalType("RegExp");
11990-
globalTypedPropertyDescriptorType = getTypeOfGlobalSymbol(getGlobalTypeSymbol("TypedPropertyDescriptor"), 1);
11991-
globalClassDecoratorType = getGlobalType("ClassDecorator");
11992-
globalPropertyDecoratorType = getGlobalType("PropertyDecorator");
11993-
globalMethodDecoratorType = getGlobalType("MethodDecorator");
11994-
globalParameterDecoratorType = getGlobalType("ParameterDecorator");
11989+
getGlobalClassDecoratorType = memoize(() => getGlobalType("ClassDecorator"));
11990+
getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator"));
11991+
getGlobalMethodDecoratorType = memoize(() => getGlobalType("MethodDecorator"));
11992+
getGlobalParameterDecoratorType = memoize(() => getGlobalType("ParameterDecorator"));
1199511993

1199611994
// If we're in ES6 mode, load the TemplateStringsArray.
1199711995
// 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)