UNPKG

6.32 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3// See LICENSE in the project root for license information.
4var __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}));
15var __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});
20var __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};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.TypeScriptInternals = void 0;
29/* eslint-disable @typescript-eslint/no-explicit-any */
30const ts = __importStar(require("typescript"));
31const node_core_library_1 = require("@rushstack/node-core-library");
32class TypeScriptInternals {
33 static getImmediateAliasedSymbol(symbol, typeChecker) {
34 // Compiler internal:
35 // https://github.com/microsoft/TypeScript/blob/v3.2.2/src/compiler/checker.ts
36 return typeChecker.getImmediateAliasedSymbol(symbol); // eslint-disable-line @typescript-eslint/no-explicit-any
37 }
38 /**
39 * Returns the Symbol for the provided Declaration. This is a workaround for a missing
40 * feature of the TypeScript Compiler API. It is the only apparent way to reach
41 * certain data structures, and seems to always work, but is not officially documented.
42 *
43 * @returns The associated Symbol. If there is no semantic information (e.g. if the
44 * declaration is an extra semicolon somewhere), then "undefined" is returned.
45 */
46 static tryGetSymbolForDeclaration(declaration, checker) {
47 let symbol = declaration.symbol;
48 if (symbol && symbol.escapedName === ts.InternalSymbolName.Computed) {
49 const name = ts.getNameOfDeclaration(declaration);
50 symbol = (name && checker.getSymbolAtLocation(name)) || symbol;
51 }
52 return symbol;
53 }
54 /**
55 * Returns whether the provided Symbol is a TypeScript "late-bound" Symbol (i.e. was created by the Checker
56 * for a computed property based on its type, rather than by the Binder).
57 */
58 static isLateBoundSymbol(symbol) {
59 if (
60 // eslint-disable-next-line no-bitwise
61 symbol.flags & ts.SymbolFlags.Transient &&
62 symbol.checkFlags === ts.CheckFlags.Late) {
63 return true;
64 }
65 return false;
66 }
67 /**
68 * Retrieves the comment ranges associated with the specified node.
69 */
70 static getJSDocCommentRanges(node, text) {
71 // Compiler internal:
72 // https://github.com/microsoft/TypeScript/blob/v2.4.2/src/compiler/utilities.ts#L616
73 return ts.getJSDocCommentRanges.apply(this, arguments);
74 }
75 /**
76 * Retrieves the (unescaped) value of an string literal, numeric literal, or identifier.
77 */
78 static getTextOfIdentifierOrLiteral(node) {
79 // Compiler internal:
80 // https://github.com/microsoft/TypeScript/blob/v3.2.2/src/compiler/utilities.ts#L2721
81 return ts.getTextOfIdentifierOrLiteral(node);
82 }
83 /**
84 * Retrieves the (cached) module resolution information for a module name that was exported from a SourceFile.
85 * The compiler populates this cache as part of analyzing the source file.
86 */
87 static getResolvedModule(sourceFile, moduleNameText, mode) {
88 // Compiler internal:
89 // https://github.com/microsoft/TypeScript/blob/v4.7.2/src/compiler/utilities.ts#L161
90 return ts.getResolvedModule(sourceFile, moduleNameText, mode);
91 }
92 /**
93 * Gets the mode required for module resolution required with the addition of Node16/nodenext
94 */
95 static getModeForUsageLocation(file, usage) {
96 // Compiler internal:
97 // https://github.com/microsoft/TypeScript/blob/v4.7.2/src/compiler/program.ts#L568
98 var _a, _b;
99 return (_b = (_a = ts).getModeForUsageLocation) === null || _b === void 0 ? void 0 : _b.call(_a, file, usage);
100 }
101 /**
102 * Returns ts.Symbol.parent if it exists.
103 */
104 static getSymbolParent(symbol) {
105 return symbol.parent;
106 }
107 /**
108 * In an statement like `export default class X { }`, the `Symbol.name` will be `default`
109 * whereas the `localSymbol` is `X`.
110 */
111 static tryGetLocalSymbol(declaration) {
112 return declaration.localSymbol;
113 }
114 static getGlobalVariableAnalyzer(program) {
115 var _a;
116 const anyProgram = program;
117 const typeCheckerInstance = (_a = anyProgram.getDiagnosticsProducingTypeChecker) !== null && _a !== void 0 ? _a : anyProgram.getTypeChecker;
118 if (!typeCheckerInstance) {
119 throw new node_core_library_1.InternalError('Missing Program.getDiagnosticsProducingTypeChecker or Program.getTypeChecker');
120 }
121 const typeChecker = typeCheckerInstance();
122 if (!typeChecker.getEmitResolver) {
123 throw new node_core_library_1.InternalError('Missing TypeChecker.getEmitResolver');
124 }
125 const resolver = typeChecker.getEmitResolver();
126 if (!resolver.hasGlobalName) {
127 throw new node_core_library_1.InternalError('Missing EmitResolver.hasGlobalName');
128 }
129 return resolver;
130 }
131 /**
132 * Returns whether a variable is declared with the const keyword
133 */
134 static isVarConst(node) {
135 // Compiler internal: https://github.com/microsoft/TypeScript/blob/71286e3d49c10e0e99faac360a6bbd40f12db7b6/src/compiler/utilities.ts#L925
136 return ts.isVarConst(node);
137 }
138}
139exports.TypeScriptInternals = TypeScriptInternals;
140//# sourceMappingURL=TypeScriptInternals.js.map
\No newline at end of file