Skip to content

Commit 1b49ae3

Browse files
andrewigginsclaude
andcommitted
Extract template literal helper functions
Refactor constant template literal handling into reusable helper functions with JSDoc documentation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 93ecd8f commit 1b49ae3

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

index.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/** Check if node is a template literal with no expressions (e.g., `foo`) */
2+
function isConstantTemplateLiteral(t, node) {
3+
return (
4+
t.isTemplateLiteral(node) &&
5+
node.expressions.length === 0 &&
6+
node.quasis.length === 1
7+
);
8+
}
9+
10+
/** Create a template literal node with no expressions (e.g., `foo`) */
11+
function constantTemplateLiteral(t, value) {
12+
return t.templateLiteral(
13+
[t.templateElement({ raw: value, cooked: value }, true)],
14+
[]
15+
);
16+
}
17+
118
module.exports = function ({ types: t }, options = {}) {
219
const rename = options.rename || {};
320

@@ -90,16 +107,9 @@ module.exports = function ({ types: t }, options = {}) {
90107

91108
if (t.isStringLiteral(node.left)) {
92109
oldName = node.left.value;
93-
} else if (t.isTemplateLiteral(node.left)) {
94-
if (
95-
node.left.expressions.length === 0 &&
96-
node.left.quasis.length === 1
97-
) {
98-
oldName = node.left.quasis[0].value.cooked;
99-
isTemplateLiteral = true;
100-
} else {
101-
return;
102-
}
110+
} else if (isConstantTemplateLiteral(t, node.left)) {
111+
oldName = node.left.quasis[0].value.cooked;
112+
isTemplateLiteral = true;
103113
} else {
104114
return;
105115
}
@@ -110,10 +120,7 @@ module.exports = function ({ types: t }, options = {}) {
110120
}
111121

112122
const newNode = isTemplateLiteral
113-
? t.templateLiteral(
114-
[t.templateElement({ raw: newName, cooked: newName }, true)],
115-
[]
116-
)
123+
? constantTemplateLiteral(t, newName)
117124
: t.stringLiteral(newName);
118125

119126
const replacedNode = t.binaryExpression("in", newNode, node.right);

0 commit comments

Comments
 (0)