1
|
"use strict";
|
2
|
/**
|
3
|
* @license
|
4
|
* Copyright Google Inc. All Rights Reserved.
|
5
|
*
|
6
|
* Use of this source code is governed by an MIT-style license that can be
|
7
|
* found in the LICENSE file at https://angular.io/license
|
8
|
*/
|
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
10
|
var compiler_1 = require("@angular/compiler");
|
11
|
var symbols_1 = require("./symbols");
|
12
|
var DiagnosticKind;
|
13
|
(function (DiagnosticKind) {
|
14
|
DiagnosticKind[DiagnosticKind["Error"] = 0] = "Error";
|
15
|
DiagnosticKind[DiagnosticKind["Warning"] = 1] = "Warning";
|
16
|
})(DiagnosticKind = exports.DiagnosticKind || (exports.DiagnosticKind = {}));
|
17
|
var TypeDiagnostic = (function () {
|
18
|
function TypeDiagnostic(kind, message, ast) {
|
19
|
this.kind = kind;
|
20
|
this.message = message;
|
21
|
this.ast = ast;
|
22
|
}
|
23
|
return TypeDiagnostic;
|
24
|
}());
|
25
|
exports.TypeDiagnostic = TypeDiagnostic;
|
26
|
// AstType calculatetype of the ast given AST element.
|
27
|
var AstType = (function () {
|
28
|
function AstType(scope, query, context) {
|
29
|
this.scope = scope;
|
30
|
this.query = query;
|
31
|
this.context = context;
|
32
|
}
|
33
|
AstType.prototype.getType = function (ast) { return ast.visit(this); };
|
34
|
AstType.prototype.getDiagnostics = function (ast) {
|
35
|
this.diagnostics = [];
|
36
|
var type = ast.visit(this);
|
37
|
if (this.context.event && type.callable) {
|
38
|
this.reportWarning('Unexpected callable expression. Expected a method call', ast);
|
39
|
}
|
40
|
return this.diagnostics;
|
41
|
};
|
42
|
AstType.prototype.visitBinary = function (ast) {
|
43
|
var _this = this;
|
44
|
// Treat undefined and null as other.
|
45
|
function normalize(kind, other) {
|
46
|
switch (kind) {
|
47
|
case symbols_1.BuiltinType.Undefined:
|
48
|
case symbols_1.BuiltinType.Null:
|
49
|
return normalize(other, symbols_1.BuiltinType.Other);
|
50
|
}
|
51
|
return kind;
|
52
|
}
|
53
|
var getType = function (ast, operation) {
|
54
|
var type = _this.getType(ast);
|
55
|
if (type.nullable) {
|
56
|
switch (operation) {
|
57
|
case '&&':
|
58
|
case '||':
|
59
|
case '==':
|
60
|
case '!=':
|
61
|
case '===':
|
62
|
case '!==':
|
63
|
// Nullable allowed.
|
64
|
break;
|
65
|
default:
|
66
|
_this.reportError("The expression might be null", ast);
|
67
|
break;
|
68
|
}
|
69
|
return _this.query.getNonNullableType(type);
|
70
|
}
|
71
|
return type;
|
72
|
};
|
73
|
var leftType = getType(ast.left, ast.operation);
|
74
|
var rightType = getType(ast.right, ast.operation);
|
75
|
var leftRawKind = this.query.getTypeKind(leftType);
|
76
|
var rightRawKind = this.query.getTypeKind(rightType);
|
77
|
var leftKind = normalize(leftRawKind, rightRawKind);
|
78
|
var rightKind = normalize(rightRawKind, leftRawKind);
|
79
|
// The following swtich implements operator typing similar to the
|
80
|
// type production tables in the TypeScript specification.
|
81
|
// https://github.com/Microsoft/TypeScript/blob/v1.8.10/doc/spec.md#4.19
|
82
|
var operKind = leftKind << 8 | rightKind;
|
83
|
switch (ast.operation) {
|
84
|
case '*':
|
85
|
case '/':
|
86
|
case '%':
|
87
|
case '-':
|
88
|
case '<<':
|
89
|
case '>>':
|
90
|
case '>>>':
|
91
|
case '&':
|
92
|
case '^':
|
93
|
case '|':
|
94
|
switch (operKind) {
|
95
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Any:
|
96
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Any:
|
97
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Number:
|
98
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Number:
|
99
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Number);
|
100
|
default:
|
101
|
var errorAst = ast.left;
|
102
|
switch (leftKind) {
|
103
|
case symbols_1.BuiltinType.Any:
|
104
|
case symbols_1.BuiltinType.Number:
|
105
|
errorAst = ast.right;
|
106
|
break;
|
107
|
}
|
108
|
return this.reportError('Expected a numeric type', errorAst);
|
109
|
}
|
110
|
case '+':
|
111
|
switch (operKind) {
|
112
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Any:
|
113
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Boolean:
|
114
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Number:
|
115
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Other:
|
116
|
case symbols_1.BuiltinType.Boolean << 8 | symbols_1.BuiltinType.Any:
|
117
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Any:
|
118
|
case symbols_1.BuiltinType.Other << 8 | symbols_1.BuiltinType.Any:
|
119
|
return this.anyType;
|
120
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.String:
|
121
|
case symbols_1.BuiltinType.Boolean << 8 | symbols_1.BuiltinType.String:
|
122
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.String:
|
123
|
case symbols_1.BuiltinType.String << 8 | symbols_1.BuiltinType.Any:
|
124
|
case symbols_1.BuiltinType.String << 8 | symbols_1.BuiltinType.Boolean:
|
125
|
case symbols_1.BuiltinType.String << 8 | symbols_1.BuiltinType.Number:
|
126
|
case symbols_1.BuiltinType.String << 8 | symbols_1.BuiltinType.String:
|
127
|
case symbols_1.BuiltinType.String << 8 | symbols_1.BuiltinType.Other:
|
128
|
case symbols_1.BuiltinType.Other << 8 | symbols_1.BuiltinType.String:
|
129
|
return this.query.getBuiltinType(symbols_1.BuiltinType.String);
|
130
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Number:
|
131
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Number);
|
132
|
case symbols_1.BuiltinType.Boolean << 8 | symbols_1.BuiltinType.Number:
|
133
|
case symbols_1.BuiltinType.Other << 8 | symbols_1.BuiltinType.Number:
|
134
|
return this.reportError('Expected a number type', ast.left);
|
135
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Boolean:
|
136
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Other:
|
137
|
return this.reportError('Expected a number type', ast.right);
|
138
|
default:
|
139
|
return this.reportError('Expected operands to be a string or number type', ast);
|
140
|
}
|
141
|
case '>':
|
142
|
case '<':
|
143
|
case '<=':
|
144
|
case '>=':
|
145
|
case '==':
|
146
|
case '!=':
|
147
|
case '===':
|
148
|
case '!==':
|
149
|
switch (operKind) {
|
150
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Any:
|
151
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Boolean:
|
152
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Number:
|
153
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.String:
|
154
|
case symbols_1.BuiltinType.Any << 8 | symbols_1.BuiltinType.Other:
|
155
|
case symbols_1.BuiltinType.Boolean << 8 | symbols_1.BuiltinType.Any:
|
156
|
case symbols_1.BuiltinType.Boolean << 8 | symbols_1.BuiltinType.Boolean:
|
157
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Any:
|
158
|
case symbols_1.BuiltinType.Number << 8 | symbols_1.BuiltinType.Number:
|
159
|
case symbols_1.BuiltinType.String << 8 | symbols_1.BuiltinType.Any:
|
160
|
case symbols_1.BuiltinType.String << 8 | symbols_1.BuiltinType.String:
|
161
|
case symbols_1.BuiltinType.Other << 8 | symbols_1.BuiltinType.Any:
|
162
|
case symbols_1.BuiltinType.Other << 8 | symbols_1.BuiltinType.Other:
|
163
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Boolean);
|
164
|
default:
|
165
|
return this.reportError('Expected the operants to be of similar type or any', ast);
|
166
|
}
|
167
|
case '&&':
|
168
|
return rightType;
|
169
|
case '||':
|
170
|
return this.query.getTypeUnion(leftType, rightType);
|
171
|
}
|
172
|
return this.reportError("Unrecognized operator " + ast.operation, ast);
|
173
|
};
|
174
|
AstType.prototype.visitChain = function (ast) {
|
175
|
if (this.diagnostics) {
|
176
|
// If we are producing diagnostics, visit the children
|
177
|
compiler_1.visitAstChildren(ast, this);
|
178
|
}
|
179
|
// The type of a chain is always undefined.
|
180
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Undefined);
|
181
|
};
|
182
|
AstType.prototype.visitConditional = function (ast) {
|
183
|
// The type of a conditional is the union of the true and false conditions.
|
184
|
if (this.diagnostics) {
|
185
|
compiler_1.visitAstChildren(ast, this);
|
186
|
}
|
187
|
return this.query.getTypeUnion(this.getType(ast.trueExp), this.getType(ast.falseExp));
|
188
|
};
|
189
|
AstType.prototype.visitFunctionCall = function (ast) {
|
190
|
var _this = this;
|
191
|
// The type of a function call is the return type of the selected signature.
|
192
|
// The signature is selected based on the types of the arguments. Angular doesn't
|
193
|
// support contextual typing of arguments so this is simpler than TypeScript's
|
194
|
// version.
|
195
|
var args = ast.args.map(function (arg) { return _this.getType(arg); });
|
196
|
var target = this.getType(ast.target);
|
197
|
if (!target || !target.callable)
|
198
|
return this.reportError('Call target is not callable', ast);
|
199
|
var signature = target.selectSignature(args);
|
200
|
if (signature)
|
201
|
return signature.result;
|
202
|
// TODO: Consider a better error message here.
|
203
|
return this.reportError('Unable no compatible signature found for call', ast);
|
204
|
};
|
205
|
AstType.prototype.visitImplicitReceiver = function (ast) {
|
206
|
var _this = this;
|
207
|
// Return a pseudo-symbol for the implicit receiver.
|
208
|
// The members of the implicit receiver are what is defined by the
|
209
|
// scope passed into this class.
|
210
|
return {
|
211
|
name: '$implict',
|
212
|
kind: 'component',
|
213
|
language: 'ng-template',
|
214
|
type: undefined,
|
215
|
container: undefined,
|
216
|
callable: false,
|
217
|
nullable: false,
|
218
|
public: true,
|
219
|
definition: undefined,
|
220
|
members: function () { return _this.scope; },
|
221
|
signatures: function () { return []; },
|
222
|
selectSignature: function (types) { return undefined; },
|
223
|
indexed: function (argument) { return undefined; }
|
224
|
};
|
225
|
};
|
226
|
AstType.prototype.visitInterpolation = function (ast) {
|
227
|
// If we are producing diagnostics, visit the children.
|
228
|
if (this.diagnostics) {
|
229
|
compiler_1.visitAstChildren(ast, this);
|
230
|
}
|
231
|
return this.undefinedType;
|
232
|
};
|
233
|
AstType.prototype.visitKeyedRead = function (ast) {
|
234
|
var targetType = this.getType(ast.obj);
|
235
|
var keyType = this.getType(ast.key);
|
236
|
var result = targetType.indexed(keyType);
|
237
|
return result || this.anyType;
|
238
|
};
|
239
|
AstType.prototype.visitKeyedWrite = function (ast) {
|
240
|
// The write of a type is the type of the value being written.
|
241
|
return this.getType(ast.value);
|
242
|
};
|
243
|
AstType.prototype.visitLiteralArray = function (ast) {
|
244
|
var _this = this;
|
245
|
// A type literal is an array type of the union of the elements
|
246
|
return this.query.getArrayType((_a = this.query).getTypeUnion.apply(_a, ast.expressions.map(function (element) { return _this.getType(element); })));
|
247
|
var _a;
|
248
|
};
|
249
|
AstType.prototype.visitLiteralMap = function (ast) {
|
250
|
// If we are producing diagnostics, visit the children
|
251
|
if (this.diagnostics) {
|
252
|
compiler_1.visitAstChildren(ast, this);
|
253
|
}
|
254
|
// TODO: Return a composite type.
|
255
|
return this.anyType;
|
256
|
};
|
257
|
AstType.prototype.visitLiteralPrimitive = function (ast) {
|
258
|
// The type of a literal primitive depends on the value of the literal.
|
259
|
switch (ast.value) {
|
260
|
case true:
|
261
|
case false:
|
262
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Boolean);
|
263
|
case null:
|
264
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Null);
|
265
|
case undefined:
|
266
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Undefined);
|
267
|
default:
|
268
|
switch (typeof ast.value) {
|
269
|
case 'string':
|
270
|
return this.query.getBuiltinType(symbols_1.BuiltinType.String);
|
271
|
case 'number':
|
272
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Number);
|
273
|
default:
|
274
|
return this.reportError('Unrecognized primitive', ast);
|
275
|
}
|
276
|
}
|
277
|
};
|
278
|
AstType.prototype.visitMethodCall = function (ast) {
|
279
|
return this.resolveMethodCall(this.getType(ast.receiver), ast);
|
280
|
};
|
281
|
AstType.prototype.visitPipe = function (ast) {
|
282
|
var _this = this;
|
283
|
// The type of a pipe node is the return type of the pipe's transform method. The table returned
|
284
|
// by getPipes() is expected to contain symbols with the corresponding transform method type.
|
285
|
var pipe = this.query.getPipes().get(ast.name);
|
286
|
if (!pipe)
|
287
|
return this.reportError("No pipe by the name " + ast.name + " found", ast);
|
288
|
var expType = this.getType(ast.exp);
|
289
|
var signature = pipe.selectSignature([expType].concat(ast.args.map(function (arg) { return _this.getType(arg); })));
|
290
|
if (!signature)
|
291
|
return this.reportError('Unable to resolve signature for pipe invocation', ast);
|
292
|
return signature.result;
|
293
|
};
|
294
|
AstType.prototype.visitPrefixNot = function (ast) {
|
295
|
// The type of a prefix ! is always boolean.
|
296
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Boolean);
|
297
|
};
|
298
|
AstType.prototype.visitNonNullAssert = function (ast) {
|
299
|
var expressionType = this.getType(ast.expression);
|
300
|
return this.query.getNonNullableType(expressionType);
|
301
|
};
|
302
|
AstType.prototype.visitPropertyRead = function (ast) {
|
303
|
return this.resolvePropertyRead(this.getType(ast.receiver), ast);
|
304
|
};
|
305
|
AstType.prototype.visitPropertyWrite = function (ast) {
|
306
|
// The type of a write is the type of the value being written.
|
307
|
return this.getType(ast.value);
|
308
|
};
|
309
|
AstType.prototype.visitQuote = function (ast) {
|
310
|
// The type of a quoted expression is any.
|
311
|
return this.query.getBuiltinType(symbols_1.BuiltinType.Any);
|
312
|
};
|
313
|
AstType.prototype.visitSafeMethodCall = function (ast) {
|
314
|
return this.resolveMethodCall(this.query.getNonNullableType(this.getType(ast.receiver)), ast);
|
315
|
};
|
316
|
AstType.prototype.visitSafePropertyRead = function (ast) {
|
317
|
return this.resolvePropertyRead(this.query.getNonNullableType(this.getType(ast.receiver)), ast);
|
318
|
};
|
319
|
Object.defineProperty(AstType.prototype, "anyType", {
|
320
|
get: function () {
|
321
|
var result = this._anyType;
|
322
|
if (!result) {
|
323
|
result = this._anyType = this.query.getBuiltinType(symbols_1.BuiltinType.Any);
|
324
|
}
|
325
|
return result;
|
326
|
},
|
327
|
enumerable: true,
|
328
|
configurable: true
|
329
|
});
|
330
|
Object.defineProperty(AstType.prototype, "undefinedType", {
|
331
|
get: function () {
|
332
|
var result = this._undefinedType;
|
333
|
if (!result) {
|
334
|
result = this._undefinedType = this.query.getBuiltinType(symbols_1.BuiltinType.Undefined);
|
335
|
}
|
336
|
return result;
|
337
|
},
|
338
|
enumerable: true,
|
339
|
configurable: true
|
340
|
});
|
341
|
AstType.prototype.resolveMethodCall = function (receiverType, ast) {
|
342
|
var _this = this;
|
343
|
if (this.isAny(receiverType)) {
|
344
|
return this.anyType;
|
345
|
}
|
346
|
// The type of a method is the selected methods result type.
|
347
|
var method = receiverType.members().get(ast.name);
|
348
|
if (!method)
|
349
|
return this.reportError("Unknown method '" + ast.name + "'", ast);
|
350
|
if (!method.type)
|
351
|
return this.reportError("Could not find a type for '" + ast.name + "'", ast);
|
352
|
if (!method.type.callable)
|
353
|
return this.reportError("Member '" + ast.name + "' is not callable", ast);
|
354
|
var signature = method.type.selectSignature(ast.args.map(function (arg) { return _this.getType(arg); }));
|
355
|
if (!signature)
|
356
|
return this.reportError("Unable to resolve signature for call of method " + ast.name, ast);
|
357
|
return signature.result;
|
358
|
};
|
359
|
AstType.prototype.resolvePropertyRead = function (receiverType, ast) {
|
360
|
if (this.isAny(receiverType)) {
|
361
|
return this.anyType;
|
362
|
}
|
363
|
// The type of a property read is the seelcted member's type.
|
364
|
var member = receiverType.members().get(ast.name);
|
365
|
if (!member) {
|
366
|
var receiverInfo = receiverType.name;
|
367
|
if (receiverInfo == '$implict') {
|
368
|
receiverInfo =
|
369
|
'The component declaration, template variable declarations, and element references do';
|
370
|
}
|
371
|
else if (receiverType.nullable) {
|
372
|
return this.reportError("The expression might be null", ast.receiver);
|
373
|
}
|
374
|
else {
|
375
|
receiverInfo = "'" + receiverInfo + "' does";
|
376
|
}
|
377
|
return this.reportError("Identifier '" + ast.name + "' is not defined. " + receiverInfo + " not contain such a member", ast);
|
378
|
}
|
379
|
if (!member.public) {
|
380
|
var receiverInfo = receiverType.name;
|
381
|
if (receiverInfo == '$implict') {
|
382
|
receiverInfo = 'the component';
|
383
|
}
|
384
|
else {
|
385
|
receiverInfo = "'" + receiverInfo + "'";
|
386
|
}
|
387
|
this.reportWarning("Identifier '" + ast.name + "' refers to a private member of " + receiverInfo, ast);
|
388
|
}
|
389
|
return member.type;
|
390
|
};
|
391
|
AstType.prototype.reportError = function (message, ast) {
|
392
|
if (this.diagnostics) {
|
393
|
this.diagnostics.push(new TypeDiagnostic(DiagnosticKind.Error, message, ast));
|
394
|
}
|
395
|
return this.anyType;
|
396
|
};
|
397
|
AstType.prototype.reportWarning = function (message, ast) {
|
398
|
if (this.diagnostics) {
|
399
|
this.diagnostics.push(new TypeDiagnostic(DiagnosticKind.Warning, message, ast));
|
400
|
}
|
401
|
return this.anyType;
|
402
|
};
|
403
|
AstType.prototype.isAny = function (symbol) {
|
404
|
return !symbol || this.query.getTypeKind(symbol) == symbols_1.BuiltinType.Any ||
|
405
|
(!!symbol.type && this.isAny(symbol.type));
|
406
|
};
|
407
|
return AstType;
|
408
|
}());
|
409
|
exports.AstType = AstType;
|
410
|
//# sourceMappingURL=expression_type.js.map
|