UNPKG

3.19 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.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.AstImport = exports.AstImportKind = void 0;
6const node_core_library_1 = require("@rushstack/node-core-library");
7const AstEntity_1 = require("./AstEntity");
8/**
9 * Indicates the import kind for an `AstImport`.
10 */
11var AstImportKind;
12(function (AstImportKind) {
13 /**
14 * An import statement such as `import X from "y";`.
15 */
16 AstImportKind[AstImportKind["DefaultImport"] = 0] = "DefaultImport";
17 /**
18 * An import statement such as `import { X } from "y";`.
19 */
20 AstImportKind[AstImportKind["NamedImport"] = 1] = "NamedImport";
21 /**
22 * An import statement such as `import * as x from "y";`.
23 */
24 AstImportKind[AstImportKind["StarImport"] = 2] = "StarImport";
25 /**
26 * An import statement such as `import x = require("y");`.
27 */
28 AstImportKind[AstImportKind["EqualsImport"] = 3] = "EqualsImport";
29 /**
30 * An import statement such as `interface foo { foo: import("bar").a.b.c }`.
31 */
32 AstImportKind[AstImportKind["ImportType"] = 4] = "ImportType";
33})(AstImportKind = exports.AstImportKind || (exports.AstImportKind = {}));
34/**
35 * For a symbol that was imported from an external package, this tracks the import
36 * statement that was used to reach it.
37 */
38class AstImport extends AstEntity_1.AstSyntheticEntity {
39 constructor(options) {
40 super();
41 this.importKind = options.importKind;
42 this.modulePath = options.modulePath;
43 this.exportName = options.exportName;
44 // We start with this assumption, but it may get changed later if non-type-only import is encountered.
45 this.isTypeOnlyEverywhere = options.isTypeOnly;
46 this.key = AstImport.getKey(options);
47 }
48 /** {@inheritdoc} */
49 get localName() {
50 // abstract
51 return this.exportName;
52 }
53 /**
54 * Calculates the lookup key used with `AstImport.key`
55 */
56 static getKey(options) {
57 switch (options.importKind) {
58 case AstImportKind.DefaultImport:
59 return `${options.modulePath}:${options.exportName}`;
60 case AstImportKind.NamedImport:
61 return `${options.modulePath}:${options.exportName}`;
62 case AstImportKind.StarImport:
63 return `${options.modulePath}:*`;
64 case AstImportKind.EqualsImport:
65 return `${options.modulePath}:=`;
66 case AstImportKind.ImportType: {
67 const subKey = !options.exportName
68 ? '*' // Equivalent to StarImport
69 : options.exportName.includes('.') // Equivalent to a named export
70 ? options.exportName.split('.')[0]
71 : options.exportName;
72 return `${options.modulePath}:${subKey}`;
73 }
74 default:
75 throw new node_core_library_1.InternalError('Unknown AstImportKind');
76 }
77 }
78}
79exports.AstImport = AstImport;
80//# sourceMappingURL=AstImport.js.map
\No newline at end of file