Skip to content

Commit 6ae52ac

Browse files
committed
transform paths in one pass
1 parent be591c1 commit 6ae52ac

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

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

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,26 @@ export function getImportPathTransformer({ pattern, template }) {
3434
export const importPathTransformer = () => ({
3535
before: [
3636
transformImportPath({
37-
pattern: /^(@firebase.*)-exp(.*)$/g,
37+
pattern: /^(@firebase.*)-exp(.*)$/,
3838
template: [1, 2]
3939
})
4040
],
4141
after: [],
4242
afterDeclarations: [
4343
transformImportPath({
44-
pattern: /^(@firebase.*)-exp(.*)$/g,
44+
pattern: /^(@firebase.*)-exp(.*)$/,
4545
template: [1, 2]
4646
})
4747
]
4848
});
4949

5050
function transformImportPath({ pattern, template }) {
5151
return context => file => {
52-
const firstPass = visitNodeAndChildren(
53-
file,
54-
context,
55-
{ pattern, template },
56-
transformDeclareNode
57-
);
5852
return visitNodeAndChildren(
59-
firstPass,
53+
file,
6054
context,
6155
{ pattern, template },
62-
transformImportExportNode
56+
transformNode
6357
);
6458
};
6559
}
@@ -105,36 +99,27 @@ function replacePath(pathString, pattern, template) {
10599
return null;
106100
}
107101

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 }) {
102+
function transformNode(node, { pattern, template }) {
125103
if (
126104
(ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
127105
node.moduleSpecifier
128106
) {
129107
const importPathWithQuotes = node.moduleSpecifier.getText();
130-
131108
const newName = replacePath(importPathWithQuotes, pattern, template);
132109

133110
if (newName) {
134111
const newNode = ts.getMutableClone(node);
135112
newNode.moduleSpecifier = ts.createLiteral(newName);
136113
return newNode;
137114
}
115+
} else if (ts.isModuleDeclaration(node) && node.name) {
116+
const importPathWithQuotes = node.name.getText();
117+
const newName = replacePath(importPathWithQuotes, pattern, template);
118+
if (newName) {
119+
const newNode = ts.getMutableClone(node);
120+
newNode.name = ts.createLiteral(newName);
121+
return newNode;
122+
}
138123
}
139124

140125
return node;

0 commit comments

Comments
 (0)