1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.nodeVisitor = void 0;
|
4 | const utils_1 = require("./utils");
|
5 |
|
6 |
|
7 |
|
8 | const isAsyncImport = ({ tsInstance }, node) => tsInstance.isCallExpression(node) &&
|
9 | node.expression.kind === tsInstance.SyntaxKind.ImportKeyword &&
|
10 | tsInstance.isStringLiteral(node.arguments[0]) &&
|
11 | node.arguments.length === 1;
|
12 | const 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 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function nodeVisitor(node) {
|
24 | const { factory, tsInstance, transformationContext } = this;
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
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 |
|
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 | kind === tsInstance.SyntaxKind.MultiLineCommentTrivia
|
44 | ?
|
45 |
|
46 |
|
47 | /^\/\*(.+)\*\/.*/s
|
48 | : /^\/\/(.+)/s,
|
49 | "$1");
|
50 | tsInstance.addSyntheticLeadingComment(p, kind, caption, hasTrailingNewLine);
|
51 | }
|
52 | return res;
|
53 | });
|
54 | |
55 |
|
56 |
|
57 |
|
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 |
|
63 |
|
64 |
|
65 |
|
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 |
|
79 |
|
80 |
|
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;
|
89 | importClause = updatedImportClause;
|
90 | }
|
91 | return factory.updateImportDeclaration(node, node.modifiers, importClause, p, node.assertClause);
|
92 | });
|
93 | |
94 |
|
95 |
|
96 |
|
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;
|
105 | exportClause = updatedExportClause;
|
106 | }
|
107 | return factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, exportClause, p, node.assertClause);
|
108 | });
|
109 | |
110 |
|
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 | }
|
116 | exports.nodeVisitor = nodeVisitor;
|
117 |
|
\ | No newline at end of file |