UNPKG

6.38 kBJavaScriptView Raw
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10var ts = require("typescript");
11var Symbols = (function () {
12 function Symbols(sourceFile) {
13 this.sourceFile = sourceFile;
14 this.references = new Map();
15 }
16 Symbols.prototype.resolve = function (name, preferReference) {
17 return (preferReference && this.references.get(name)) || this.symbols.get(name);
18 };
19 Symbols.prototype.define = function (name, value) { this.symbols.set(name, value); };
20 Symbols.prototype.defineReference = function (name, value) {
21 this.references.set(name, value);
22 };
23 Symbols.prototype.has = function (name) { return this.symbols.has(name); };
24 Object.defineProperty(Symbols.prototype, "symbols", {
25 get: function () {
26 var result = this._symbols;
27 if (!result) {
28 result = this._symbols = new Map();
29 populateBuiltins(result);
30 this.buildImports();
31 }
32 return result;
33 },
34 enumerable: true,
35 configurable: true
36 });
37 Symbols.prototype.buildImports = function () {
38 var _this = this;
39 var symbols = this._symbols;
40 // Collect the imported symbols into this.symbols
41 var stripQuotes = function (s) { return s.replace(/^['"]|['"]$/g, ''); };
42 var visit = function (node) {
43 switch (node.kind) {
44 case ts.SyntaxKind.ImportEqualsDeclaration:
45 var importEqualsDeclaration = node;
46 if (importEqualsDeclaration.moduleReference.kind ===
47 ts.SyntaxKind.ExternalModuleReference) {
48 var externalReference = importEqualsDeclaration.moduleReference;
49 // An `import <identifier> = require(<module-specifier>);
50 if (!externalReference.expression.parent) {
51 // The `parent` field of a node is set by the TypeScript binder (run as
52 // part of the type checker). Setting it here allows us to call `getText()`
53 // even if the `SourceFile` was not type checked (which looks for `SourceFile`
54 // in the parent chain). This doesn't damage the node as the binder unconditionally
55 // sets the parent.
56 externalReference.expression.parent = externalReference;
57 externalReference.parent = _this.sourceFile;
58 }
59 var from_1 = stripQuotes(externalReference.expression.getText());
60 symbols.set(importEqualsDeclaration.name.text, { __symbolic: 'reference', module: from_1 });
61 }
62 else {
63 symbols.set(importEqualsDeclaration.name.text, { __symbolic: 'error', message: "Unsupported import syntax" });
64 }
65 break;
66 case ts.SyntaxKind.ImportDeclaration:
67 var importDecl = node;
68 if (!importDecl.importClause) {
69 // An `import <module-specifier>` clause which does not bring symbols into scope.
70 break;
71 }
72 if (!importDecl.moduleSpecifier.parent) {
73 // See note above in the `ImportEqualDeclaration` case.
74 importDecl.moduleSpecifier.parent = importDecl;
75 importDecl.parent = _this.sourceFile;
76 }
77 var from = stripQuotes(importDecl.moduleSpecifier.getText());
78 if (importDecl.importClause.name) {
79 // An `import <identifier> form <module-specifier>` clause. Record the defualt symbol.
80 symbols.set(importDecl.importClause.name.text, { __symbolic: 'reference', module: from, default: true });
81 }
82 var bindings = importDecl.importClause.namedBindings;
83 if (bindings) {
84 switch (bindings.kind) {
85 case ts.SyntaxKind.NamedImports:
86 // An `import { [<identifier> [, <identifier>] } from <module-specifier>` clause
87 for (var _i = 0, _a = bindings.elements; _i < _a.length; _i++) {
88 var binding = _a[_i];
89 symbols.set(binding.name.text, {
90 __symbolic: 'reference',
91 module: from,
92 name: binding.propertyName ? binding.propertyName.text : binding.name.text
93 });
94 }
95 break;
96 case ts.SyntaxKind.NamespaceImport:
97 // An `input * as <identifier> from <module-specifier>` clause.
98 symbols.set(bindings.name.text, { __symbolic: 'reference', module: from });
99 break;
100 }
101 }
102 break;
103 }
104 ts.forEachChild(node, visit);
105 };
106 if (this.sourceFile) {
107 ts.forEachChild(this.sourceFile, visit);
108 }
109 };
110 return Symbols;
111}());
112exports.Symbols = Symbols;
113function populateBuiltins(symbols) {
114 // From lib.core.d.ts (all "define const")
115 ['Object', 'Function', 'String', 'Number', 'Array', 'Boolean', 'Map', 'NaN', 'Infinity', 'Math',
116 'Date', 'RegExp', 'Error', 'Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError',
117 'TypeError', 'URIError', 'JSON', 'ArrayBuffer', 'DataView', 'Int8Array', 'Uint8Array',
118 'Uint8ClampedArray', 'Uint16Array', 'Int16Array', 'Int32Array', 'Uint32Array', 'Float32Array',
119 'Float64Array']
120 .forEach(function (name) { return symbols.set(name, { __symbolic: 'reference', name: name }); });
121}
122//# sourceMappingURL=symbols.js.map
\No newline at end of file