UNPKG

6.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.nodeVisitor = void 0;
4const utils_1 = require("./utils");
5/* ****************************************************************************************************************** *
6 * Helpers
7 * ****************************************************************************************************************** */
8const isAsyncImport = ({ tsInstance }, node) => tsInstance.isCallExpression(node) &&
9 node.expression.kind === tsInstance.SyntaxKind.ImportKeyword &&
10 tsInstance.isStringLiteral(node.arguments[0]) &&
11 node.arguments.length === 1;
12const isRequire = ({ tsInstance }, node) => tsInstance.isCallExpression(node) &&
13 tsInstance.isIdentifier(node.expression) &&
14 node.expression.text === "require" &&
15 tsInstance.isStringLiteral(node.arguments[0]) &&
16 node.arguments.length === 1;
17/* ****************************************************************************************************************** *
18 * Node Visitor
19 * ****************************************************************************************************************** */
20/**
21 * Visit and replace nodes with module specifiers
22 */
23function nodeVisitor(node) {
24 const { factory, tsInstance, transformationContext } = this;
25 /**
26 * Update require / import functions
27 *
28 * require('module')
29 * import('module')
30 */
31 if (isRequire(this, node) || isAsyncImport(this, node))
32 return (0, utils_1.resolvePathAndUpdateNode)(this, node, node.arguments[0].text, (p) => {
33 const res = factory.updateCallExpression(node, node.expression, node.typeArguments, [p]);
34 /* Handle comments */
35 const textNode = node.arguments[0];
36 const commentRanges = tsInstance.getLeadingCommentRanges(textNode.getFullText(), 0) || [];
37 for (const range of commentRanges) {
38 const { kind, pos, end, hasTrailingNewLine } = range;
39 const caption = textNode
40 .getFullText()
41 .substring(pos, end)
42 .replace(
43 /* searchValue */ kind === tsInstance.SyntaxKind.MultiLineCommentTrivia
44 ? // Comment range in a multi-line comment with more than one line erroneously
45 // includes the node's text in the range. For that reason, we use the greedy
46 // selector in capture group and dismiss anything after the final comment close tag
47 /^\/\*(.+)\*\/.*/s
48 : /^\/\/(.+)/s,
49 /* replaceValue */ "$1");
50 tsInstance.addSyntheticLeadingComment(p, kind, caption, hasTrailingNewLine);
51 }
52 return res;
53 });
54 /**
55 * Update ExternalModuleReference
56 *
57 * import foo = require("foo");
58 */
59 if (tsInstance.isExternalModuleReference(node) && tsInstance.isStringLiteral(node.expression))
60 return (0, utils_1.resolvePathAndUpdateNode)(this, node, node.expression.text, (p) => factory.updateExternalModuleReference(node, p));
61 /**
62 * Update ImportTypeNode
63 *
64 * typeof import("./bar");
65 * import ("package").MyType;
66 */
67 if (tsInstance.isImportTypeNode(node)) {
68 const argument = node.argument;
69 if (!tsInstance.isStringLiteral(argument.literal))
70 return node;
71 const { text } = argument.literal;
72 if (!text)
73 return node;
74 const res = (0, utils_1.resolvePathAndUpdateNode)(this, node, text, (p) => factory.updateImportTypeNode(node, factory.updateLiteralTypeNode(argument, p), node.assertions, node.qualifier, node.typeArguments, node.isTypeOf));
75 return tsInstance.visitEachChild(res, this.getVisitor(), transformationContext);
76 }
77 /**
78 * Update ImportDeclaration
79 *
80 * import ... 'module';
81 */
82 if (tsInstance.isImportDeclaration(node) && node.moduleSpecifier && tsInstance.isStringLiteral(node.moduleSpecifier))
83 return (0, utils_1.resolvePathAndUpdateNode)(this, node, node.moduleSpecifier.text, (p) => {
84 let importClause = node.importClause;
85 if (!this.isDeclarationFile && (importClause === null || importClause === void 0 ? void 0 : importClause.namedBindings)) {
86 const updatedImportClause = (0, utils_1.elideImportOrExportClause)(this, node);
87 if (!updatedImportClause)
88 return undefined; // No imports left, elide entire declaration
89 importClause = updatedImportClause;
90 }
91 return factory.updateImportDeclaration(node, node.modifiers, importClause, p, node.assertClause);
92 });
93 /**
94 * Update ExportDeclaration
95 *
96 * export ... 'module';
97 */
98 if (tsInstance.isExportDeclaration(node) && node.moduleSpecifier && tsInstance.isStringLiteral(node.moduleSpecifier))
99 return (0, utils_1.resolvePathAndUpdateNode)(this, node, node.moduleSpecifier.text, (p) => {
100 let exportClause = node.exportClause;
101 if (!this.isDeclarationFile && exportClause && tsInstance.isNamedExports(exportClause)) {
102 const updatedExportClause = (0, utils_1.elideImportOrExportClause)(this, node);
103 if (!updatedExportClause)
104 return undefined; // No export left, elide entire declaration
105 exportClause = updatedExportClause;
106 }
107 return factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, exportClause, p, node.assertClause);
108 });
109 /**
110 * Update module augmentation
111 */
112 if (tsInstance.isModuleDeclaration(node) && tsInstance.isStringLiteral(node.name))
113 return (0, utils_1.resolvePathAndUpdateNode)(this, node, node.name.text, (p) => factory.updateModuleDeclaration(node, node.modifiers, p, node.body));
114 return tsInstance.visitEachChild(node, this.getVisitor(), transformationContext);
115}
116exports.nodeVisitor = nodeVisitor;
117//# sourceMappingURL=visitor.js.map
\No newline at end of file