1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | var ts = require("typescript");
|
11 | var 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 |
|
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 |
|
50 | if (!externalReference.expression.parent) {
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
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 |
|
70 | break;
|
71 | }
|
72 | if (!importDecl.moduleSpecifier.parent) {
|
73 |
|
74 | importDecl.moduleSpecifier.parent = importDecl;
|
75 | importDecl.parent = _this.sourceFile;
|
76 | }
|
77 | var from = stripQuotes(importDecl.moduleSpecifier.getText());
|
78 | if (importDecl.importClause.name) {
|
79 |
|
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 |
|
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 |
|
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 | }());
|
112 | exports.Symbols = Symbols;
|
113 | function populateBuiltins(symbols) {
|
114 |
|
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 |
|
\ | No newline at end of file |