1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.resolvePathAndUpdateNode = void 0;
|
4 | const general_utils_1 = require("./general-utils");
|
5 | const ts_helpers_1 = require("./ts-helpers");
|
6 | const resolve_module_name_1 = require("./resolve-module-name");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function resolvePathAndUpdateNode(context, node, moduleName, updaterFn) {
|
14 | const { sourceFile, tsInstance, factory } = context;
|
15 | const { normalizePath } = tsInstance;
|
16 |
|
17 | const tags = getStatementTags();
|
18 |
|
19 | if (tags.shouldSkip)
|
20 | return node;
|
21 |
|
22 | if (tags.overridePath) {
|
23 | const transformedPath = !(0, general_utils_1.isURL)(tags.overridePath)
|
24 | ? (0, general_utils_1.maybeAddRelativeLocalPrefix)(normalizePath(tags.overridePath))
|
25 | : tags.overridePath;
|
26 | return updaterFn(factory.createStringLiteral(transformedPath));
|
27 | }
|
28 |
|
29 |
|
30 | if (!(0, ts_helpers_1.isModulePathsMatch)(context, moduleName))
|
31 | return node;
|
32 | const res = (0, resolve_module_name_1.resolveModuleName)(context, moduleName);
|
33 | if (!res)
|
34 | return node;
|
35 | const { outputPath, resolvedPath } = res;
|
36 |
|
37 | if (context.excludeMatchers)
|
38 | for (const matcher of context.excludeMatchers)
|
39 | if (matcher.match(outputPath) || (resolvedPath && matcher.match(resolvedPath)))
|
40 | return node;
|
41 | return updaterFn(factory.createStringLiteral(outputPath));
|
42 | |
43 |
|
44 |
|
45 | function getStatementTags() {
|
46 | var _a;
|
47 | let targetNode = tsInstance.isStatement(node)
|
48 | ? node
|
49 | : (_a = tsInstance.findAncestor(node, tsInstance.isStatement)) !== null && _a !== void 0 ? _a : node;
|
50 | targetNode = tsInstance.getOriginalNode(targetNode);
|
51 | let jsDocTags;
|
52 | try {
|
53 | jsDocTags = tsInstance.getJSDocTags(targetNode);
|
54 | }
|
55 | catch (_b) { }
|
56 | const commentTags = new Map();
|
57 | try {
|
58 | const trivia = targetNode.getFullText(sourceFile).slice(0, targetNode.getLeadingTriviaWidth(sourceFile));
|
59 | const regex = /^\s*\/\/\/?\s*@(transform-path|no-transform-path)(?:[^\S\r\n](.+?))?$/gim;
|
60 | for (let match = regex.exec(trivia); match; match = regex.exec(trivia))
|
61 | commentTags.set(match[1], match[2]);
|
62 | }
|
63 | catch (_c) { }
|
64 | const overridePath = findTag("transform-path");
|
65 | const shouldSkip = findTag("no-transform-path");
|
66 | return {
|
67 | overridePath: typeof overridePath === "string" ? overridePath : void 0,
|
68 | shouldSkip: !!shouldSkip,
|
69 | };
|
70 | function findTag(expected) {
|
71 | if (commentTags.has(expected))
|
72 | return commentTags.get(expected) || true;
|
73 | if (!(jsDocTags === null || jsDocTags === void 0 ? void 0 : jsDocTags.length))
|
74 | return void 0;
|
75 | for (const tag of jsDocTags) {
|
76 | const tagName = tag.tagName.text.toLowerCase();
|
77 | if (tagName === expected)
|
78 | return typeof tag.comment === "string" ? tag.comment : true;
|
79 |
|
80 | if (typeof tag.comment !== "string" || tag.comment[0] !== "-")
|
81 | continue;
|
82 | const dashPos = expected.indexOf("-");
|
83 | if (dashPos < 0)
|
84 | return void 0;
|
85 | if (tagName === expected.slice(0, dashPos)) {
|
86 | const comment = tag.comment;
|
87 | const choppedCommentTagName = comment.slice(0, expected.length - dashPos);
|
88 | return choppedCommentTagName === expected.slice(dashPos)
|
89 | ? comment.slice(choppedCommentTagName.length + 1).trim() || true
|
90 | : void 0;
|
91 | }
|
92 | }
|
93 | }
|
94 | }
|
95 | }
|
96 | exports.resolvePathAndUpdateNode = resolvePathAndUpdateNode;
|
97 |
|
98 |
|
\ | No newline at end of file |