1 | "use strict";
|
2 |
|
3 |
|
4 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
5 | if (k2 === undefined) k2 = k;
|
6 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
7 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
8 | desc = { enumerable: true, get: function() { return m[k]; } };
|
9 | }
|
10 | Object.defineProperty(o, k2, desc);
|
11 | }) : (function(o, m, k, k2) {
|
12 | if (k2 === undefined) k2 = k;
|
13 | o[k2] = m[k];
|
14 | }));
|
15 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
16 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
17 | }) : function(o, v) {
|
18 | o["default"] = v;
|
19 | });
|
20 | var __importStar = (this && this.__importStar) || function (mod) {
|
21 | if (mod && mod.__esModule) return mod;
|
22 | var result = {};
|
23 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
24 | __setModuleDefault(result, mod);
|
25 | return result;
|
26 | };
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 | exports.TypeScriptHelpers = void 0;
|
29 |
|
30 | const ts = __importStar(require("typescript"));
|
31 | const SourceFileLocationFormatter_1 = require("./SourceFileLocationFormatter");
|
32 | const TypeScriptInternals_1 = require("./TypeScriptInternals");
|
33 | const node_core_library_1 = require("@rushstack/node-core-library");
|
34 | class TypeScriptHelpers {
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | static followAliases(symbol, typeChecker) {
|
47 | let current = symbol;
|
48 | for (;;) {
|
49 | if (!(current.flags & ts.SymbolFlags.Alias)) {
|
50 | break;
|
51 | }
|
52 | const currentAlias = typeChecker.getAliasedSymbol(current);
|
53 | if (!currentAlias || currentAlias === current) {
|
54 | break;
|
55 | }
|
56 | current = currentAlias;
|
57 | }
|
58 | return current;
|
59 | }
|
60 | |
61 |
|
62 |
|
63 |
|
64 | static isFollowableAlias(symbol, typeChecker) {
|
65 | if (!(symbol.flags & ts.SymbolFlags.Alias)) {
|
66 | return false;
|
67 | }
|
68 | const alias = typeChecker.getAliasedSymbol(symbol);
|
69 | if (!alias || alias === symbol) {
|
70 | return false;
|
71 | }
|
72 | return true;
|
73 | }
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | static tryGetADeclaration(symbol) {
|
80 | if (symbol.declarations && symbol.declarations.length > 0) {
|
81 | return symbol.declarations[0];
|
82 | }
|
83 | return undefined;
|
84 | }
|
85 | |
86 |
|
87 |
|
88 | static isAmbient(symbol, typeChecker) {
|
89 | const followedSymbol = TypeScriptHelpers.followAliases(symbol, typeChecker);
|
90 | if (followedSymbol.declarations && followedSymbol.declarations.length > 0) {
|
91 | const firstDeclaration = followedSymbol.declarations[0];
|
92 |
|
93 | const highestModuleDeclaration = TypeScriptHelpers.findHighestParent(firstDeclaration, ts.SyntaxKind.ModuleDeclaration);
|
94 | if (highestModuleDeclaration) {
|
95 | if (highestModuleDeclaration.name.getText().trim() === 'global') {
|
96 | return true;
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 | const sourceFile = firstDeclaration.getSourceFile();
|
103 | if (typeChecker.getSymbolAtLocation(sourceFile)) {
|
104 | return false;
|
105 | }
|
106 | }
|
107 | return true;
|
108 | }
|
109 | |
110 |
|
111 |
|
112 |
|
113 | static getSymbolForDeclaration(declaration, checker) {
|
114 | const symbol = TypeScriptInternals_1.TypeScriptInternals.tryGetSymbolForDeclaration(declaration, checker);
|
115 | if (!symbol) {
|
116 | throw new node_core_library_1.InternalError('Unable to determine semantic information for declaration:\n' +
|
117 | SourceFileLocationFormatter_1.SourceFileLocationFormatter.formatDeclaration(declaration));
|
118 | }
|
119 | return symbol;
|
120 | }
|
121 |
|
122 | static getModuleSpecifier(nodeWithModuleSpecifier) {
|
123 | if (nodeWithModuleSpecifier.kind === ts.SyntaxKind.ImportType) {
|
124 |
|
125 | if (nodeWithModuleSpecifier.argument.kind !== ts.SyntaxKind.LiteralType ||
|
126 | nodeWithModuleSpecifier.argument.literal.kind !== ts.SyntaxKind.StringLiteral) {
|
127 | throw new node_core_library_1.InternalError(`Invalid ImportTypeNode: ${nodeWithModuleSpecifier.getText()}\n` +
|
128 | SourceFileLocationFormatter_1.SourceFileLocationFormatter.formatDeclaration(nodeWithModuleSpecifier));
|
129 | }
|
130 | const literalTypeNode = nodeWithModuleSpecifier.argument;
|
131 | const stringLiteral = literalTypeNode.literal;
|
132 | return stringLiteral.text.trim();
|
133 | }
|
134 |
|
135 | if (nodeWithModuleSpecifier.moduleSpecifier &&
|
136 | ts.isStringLiteralLike(nodeWithModuleSpecifier.moduleSpecifier)) {
|
137 | return TypeScriptInternals_1.TypeScriptInternals.getTextOfIdentifierOrLiteral(nodeWithModuleSpecifier.moduleSpecifier);
|
138 | }
|
139 | return undefined;
|
140 | }
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | static matchAncestor(node, kindsToMatch) {
|
155 |
|
156 | const reversedParentKinds = kindsToMatch.slice(0).reverse();
|
157 | let current = undefined;
|
158 | for (const parentKind of reversedParentKinds) {
|
159 | if (!current) {
|
160 |
|
161 | current = node;
|
162 | }
|
163 | else {
|
164 |
|
165 | current = current.parent;
|
166 | }
|
167 |
|
168 | if (!current || current.kind !== parentKind) {
|
169 | return undefined;
|
170 | }
|
171 | }
|
172 |
|
173 | return current;
|
174 | }
|
175 | |
176 |
|
177 |
|
178 |
|
179 | static findFirstChildNode(node, kindToMatch) {
|
180 | for (const child of node.getChildren()) {
|
181 | if (child.kind === kindToMatch) {
|
182 | return child;
|
183 | }
|
184 | const recursiveMatch = TypeScriptHelpers.findFirstChildNode(child, kindToMatch);
|
185 | if (recursiveMatch) {
|
186 | return recursiveMatch;
|
187 | }
|
188 | }
|
189 | return undefined;
|
190 | }
|
191 | |
192 |
|
193 |
|
194 | static findFirstParent(node, kindToMatch) {
|
195 | let current = node.parent;
|
196 | while (current) {
|
197 | if (current.kind === kindToMatch) {
|
198 | return current;
|
199 | }
|
200 | current = current.parent;
|
201 | }
|
202 | return undefined;
|
203 | }
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 | static findHighestParent(node, kindToMatch) {
|
210 | let current = node;
|
211 | let highest = undefined;
|
212 | for (;;) {
|
213 | current = TypeScriptHelpers.findFirstParent(current, kindToMatch);
|
214 | if (!current) {
|
215 | break;
|
216 | }
|
217 | highest = current;
|
218 | }
|
219 | return highest;
|
220 | }
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | static tryDecodeWellKnownSymbolName(name) {
|
230 | const match = TypeScriptHelpers._wellKnownSymbolNameRegExp.exec(name);
|
231 | if (match) {
|
232 | const identifier = match[1];
|
233 | return `[Symbol.${identifier}]`;
|
234 | }
|
235 | return undefined;
|
236 | }
|
237 | |
238 |
|
239 |
|
240 | static isUniqueSymbolName(name) {
|
241 | return TypeScriptHelpers._uniqueSymbolNameRegExp.test(name);
|
242 | }
|
243 | |
244 |
|
245 |
|
246 | static tryGetLateBoundName(declarationName) {
|
247 |
|
248 |
|
249 | const printer = ts.createPrinter({ removeComments: true }, {
|
250 | onEmitNode(hint, node, emitCallback) {
|
251 | ts.setEmitFlags(declarationName, ts.EmitFlags.NoIndentation | ts.EmitFlags.SingleLine);
|
252 | emitCallback(hint, node);
|
253 | }
|
254 | });
|
255 | const sourceFile = declarationName.getSourceFile();
|
256 | const text = printer.printNode(ts.EmitHint.Unspecified, declarationName, sourceFile);
|
257 |
|
258 | ts.disposeEmitNodes(sourceFile);
|
259 | return text;
|
260 | }
|
261 | }
|
262 | exports.TypeScriptHelpers = TypeScriptHelpers;
|
263 |
|
264 |
|
265 | TypeScriptHelpers._wellKnownSymbolNameRegExp = /^__@(\w+)$/;
|
266 |
|
267 |
|
268 | TypeScriptHelpers._uniqueSymbolNameRegExp = /^__@.*@\d+$/;
|
269 |
|
\ | No newline at end of file |