UNPKG

3.86 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.elideImportOrExportClause = void 0;
4function elideImportOrExportClause(context, node) {
5 const { tsInstance, transformationContext, factory } = context;
6 const resolver = transformationContext.getEmitResolver();
7 // Resolver may not be present if run manually (without Program)
8 if (!resolver)
9 return tsInstance.isImportDeclaration(node) ? node.importClause : node.exportClause;
10 const { visitNode, isNamedImportBindings, isImportSpecifier, SyntaxKind, visitNodes, isNamedExportBindings, isExportSpecifier, } = tsInstance;
11 if (tsInstance.isImportDeclaration(node)) {
12 if (node.importClause.isTypeOnly)
13 return undefined;
14 return visitNode(node.importClause, visitImportClause);
15 }
16 else {
17 if (node.isTypeOnly)
18 return undefined;
19 return visitNode(node.exportClause, visitNamedExports, isNamedExportBindings);
20 }
21 /* ********************************************************* *
22 * Helpers
23 * ********************************************************* */
24 // The following visitors are adapted from the TS source-base src/compiler/transformers/ts
25 /**
26 * Visits an import clause, eliding it if it is not referenced.
27 *
28 * @param node The import clause node.
29 */
30 function visitImportClause(node) {
31 // Elide the import clause if we elide both its name and its named bindings.
32 const name = resolver.isReferencedAliasDeclaration(node) ? node.name : undefined;
33 const namedBindings = visitNode(node.namedBindings, visitNamedImportBindings, isNamedImportBindings);
34 return name || namedBindings
35 ? factory.updateImportClause(node, /*isTypeOnly*/ false, name, namedBindings)
36 : undefined;
37 }
38 /**
39 * Visits named import bindings, eliding it if it is not referenced.
40 *
41 * @param node The named import bindings node.
42 */
43 function visitNamedImportBindings(node) {
44 if (node.kind === SyntaxKind.NamespaceImport) {
45 // Elide a namespace import if it is not referenced.
46 return resolver.isReferencedAliasDeclaration(node) ? node : undefined;
47 }
48 else {
49 // Elide named imports if all of its import specifiers are elided.
50 const elements = visitNodes(node.elements, visitImportSpecifier, isImportSpecifier);
51 return tsInstance.some(elements) ? factory.updateNamedImports(node, elements) : undefined;
52 }
53 }
54 /**
55 * Visits an import specifier, eliding it if it is not referenced.
56 *
57 * @param node The import specifier node.
58 */
59 function visitImportSpecifier(node) {
60 // Elide an import specifier if it is not referenced.
61 return resolver.isReferencedAliasDeclaration(node) ? node : undefined;
62 }
63 /**
64 * Visits named exports, eliding it if it does not contain an export specifier that
65 * resolves to a value.
66 *
67 * @param node The named exports node.
68 */
69 function visitNamedExports(node) {
70 // Elide the named exports if all of its export specifiers were elided.
71 const elements = visitNodes(node.elements, visitExportSpecifier, isExportSpecifier);
72 return tsInstance.some(elements) ? factory.updateNamedExports(node, elements) : undefined;
73 }
74 /**
75 * Visits an export specifier, eliding it if it does not resolve to a value.
76 *
77 * @param node The export specifier node.
78 */
79 function visitExportSpecifier(node) {
80 // Elide an export specifier if it does not reference a value.
81 return resolver.isValueAliasDeclaration(node) ? node : undefined;
82 }
83}
84exports.elideImportOrExportClause = elideImportOrExportClause;
85// endregion
86//# sourceMappingURL=elide-import-export.js.map
\No newline at end of file