Skip to content

Commit b602871

Browse files
committed
Modify transformer
1 parent fd9c035 commit b602871

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

scripts/exp/ts-transform-import-path.js

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,88 @@ export const importPathTransformer = () => ({
4949

5050
function transformImportPath({ pattern, template }) {
5151
return context => file => {
52-
return visitNodeAndChildren(file, context, { pattern, template });
52+
const firstPass = visitNodeAndChildren(
53+
file,
54+
context,
55+
{ pattern, template },
56+
transformDeclareNode
57+
);
58+
return visitNodeAndChildren(
59+
firstPass,
60+
context,
61+
{ pattern, template },
62+
transformImportExportNode
63+
);
5364
};
5465
}
5566

56-
function visitNodeAndChildren(node, context, { pattern, template }) {
67+
function visitNodeAndChildren(
68+
node,
69+
context,
70+
{ pattern, template },
71+
nodeTransformer
72+
) {
5773
return ts.visitEachChild(
58-
visitNode(node, { pattern, template }),
74+
nodeTransformer(node, { pattern, template }),
5975
childNode =>
60-
visitNodeAndChildren(childNode, context, { pattern, template }),
76+
visitNodeAndChildren(
77+
childNode,
78+
context,
79+
{ pattern, template },
80+
nodeTransformer
81+
),
6182
context
6283
);
6384
}
6485

65-
function visitNode(node, { pattern, template }) {
66-
let importPath;
86+
function replacePath(pathString, pattern, template) {
87+
const pathStringWithoutQuotes = pathString.substr(1, pathString.length - 2);
88+
89+
const captures = pattern.exec(pathStringWithoutQuotes);
90+
91+
if (captures) {
92+
const newNameFragments = [];
93+
for (const fragment of template) {
94+
if (typeof fragment === 'number') {
95+
newNameFragments.push(captures[fragment]);
96+
} else if (typeof fragment === 'string') {
97+
newNameFragments.push(fragment);
98+
} else {
99+
throw Error(`unrecognized fragment: ${fragment}`);
100+
}
101+
}
102+
return newNameFragments.join('');
103+
}
104+
105+
return null;
106+
}
107+
108+
function transformDeclareNode(node, { pattern, template }) {
109+
if (ts.isModuleDeclaration(node) && node.name) {
110+
const importPathWithQuotes = node.name.getText();
111+
112+
const newName = replacePath(importPathWithQuotes, pattern, template);
113+
if (newName) {
114+
const newNode = ts.getMutableClone(node);
115+
newNode.name = ts.createLiteral(newName);
116+
return newNode;
117+
}
118+
return node;
119+
}
120+
121+
return node;
122+
}
123+
124+
function transformImportExportNode(node, { pattern, template }) {
67125
if (
68126
(ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
69127
node.moduleSpecifier
70128
) {
71129
const importPathWithQuotes = node.moduleSpecifier.getText();
72-
importPath = importPathWithQuotes.substr(
73-
1,
74-
importPathWithQuotes.length - 2
75-
);
76130

77-
const captures = pattern.exec(importPath);
131+
const newName = replacePath(importPathWithQuotes, pattern, template);
78132

79-
if (captures) {
80-
const newNameFragments = [];
81-
for (const fragment of template) {
82-
if (typeof fragment === 'number') {
83-
newNameFragments.push(captures[fragment]);
84-
} else if (typeof fragment === 'string') {
85-
newNameFragments.push(fragment);
86-
} else {
87-
throw Error(`unrecognized fragment: ${fragment}`);
88-
}
89-
}
90-
const newName = newNameFragments.join('');
133+
if (newName) {
91134
const newNode = ts.getMutableClone(node);
92135
newNode.moduleSpecifier = ts.createLiteral(newName);
93136
return newNode;

0 commit comments

Comments
 (0)