Skip to content

Commit 767e56a

Browse files
author
Fernando Basello
committed
fix: babel plugin
1 parent d1ed346 commit 767e56a

1 file changed

Lines changed: 39 additions & 47 deletions

File tree

src/plugins/index.js

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ module.exports = function (babel) {
9393
t.isImportDeclaration(binding.path.parent))
9494
) {
9595
// Check arguments for runtime values
96-
const args = arg.arguments.map((argNode) => {
97-
if (t.isIdentifier(argNode)) {
98-
const argBinding = path.scope.getBinding(argNode.name);
96+
const args = arg.arguments.map((arg) => {
97+
if (t.isIdentifier(arg)) {
98+
const argBinding = path.scope.getBinding(arg.name);
9999
if (argBinding) {
100100
return {
101-
value: argNode.name,
101+
value: arg.name,
102102
isRuntime: true,
103103
};
104104
}
105-
} else if (t.isBinaryExpression(argNode)) {
105+
} else if (t.isBinaryExpression(arg)) {
106106
// For binary expressions, we need to handle the entire expression as one unit
107107
const hasRuntimeValue = (node) => {
108108
if (t.isIdentifier(node)) {
@@ -117,17 +117,14 @@ module.exports = function (babel) {
117117
return false;
118118
};
119119

120-
const isRuntime = hasRuntimeValue(argNode);
120+
const isRuntime = hasRuntimeValue(arg);
121121
return {
122-
value: path.hub.file.code.slice(
123-
argNode.start,
124-
argNode.end
125-
),
122+
value: path.hub.file.code.slice(arg.start, arg.end),
126123
isRuntime,
127124
isBinaryExpression: true,
128125
};
129-
} else if (t.isObjectExpression(argNode)) {
130-
const properties = argNode.properties
126+
} else if (t.isObjectExpression(arg)) {
127+
const properties = arg.properties
131128
.map((prop) => {
132129
if (t.isObjectProperty(prop)) {
133130
if (t.isIdentifier(prop.value)) {
@@ -165,12 +162,12 @@ module.exports = function (babel) {
165162
}
166163
}
167164
return {
168-
value: path.hub.file.code.slice(argNode.start, argNode.end),
165+
value: path.hub.file.code.slice(arg.start, arg.end),
169166
isRuntime: false,
170167
};
171168
});
172169

173-
if (args.some((runtimeArg) => runtimeArg.isRuntime)) {
170+
if (args.some((arg) => arg.isRuntime)) {
174171
// Create template elements for each argument
175172
const quasis = [];
176173
const expressions = [];
@@ -184,8 +181,8 @@ module.exports = function (babel) {
184181
);
185182

186183
// Add each argument
187-
args.forEach((objectArg, index) => {
188-
if (objectArg.isObject) {
184+
args.forEach((arg, index) => {
185+
if (arg.isObject) {
189186
const prev = quasis.pop();
190187
let objStr = prev.value.raw + '{ ';
191188

@@ -250,11 +247,9 @@ module.exports = function (babel) {
250247
);
251248
} else {
252249
// If no runtime values, use string literal
253-
const argsString = args
254-
.map((stringArg) => stringArg.value)
255-
.join(', ');
250+
const arg = args.map((arg) => arg.value).join(', ');
256251
path.node.arguments[0] = t.stringLiteral(
257-
`${callee.name}(${argsString})`
252+
`${callee.name}(${arg})`
258253
);
259254
}
260255
return;
@@ -279,34 +274,34 @@ module.exports = function (babel) {
279274
extractFunctionParts(path, functionNode);
280275

281276
// Extract argument values and their declarations if they are variables
282-
const args = arg.arguments.map((argNode) => {
283-
if (t.isBinaryExpression(argNode)) {
277+
const args = arg.arguments.map((arg) => {
278+
if (t.isBinaryExpression(arg)) {
284279
// For binary expressions, check if either operand is a runtime value
285280
const left =
286-
t.isIdentifier(argNode.left) &&
287-
path.scope.getBinding(argNode.left.name)
288-
? { value: argNode.left.name, isRuntime: true }
281+
t.isIdentifier(arg.left) &&
282+
path.scope.getBinding(arg.left.name)
283+
? { value: arg.left.name, isRuntime: true }
289284
: {
290285
value: path.hub.file.code.slice(
291-
argNode.left.start,
292-
argNode.left.end
286+
arg.left.start,
287+
arg.left.end
293288
),
294289
isRuntime: false,
295290
};
296291

297292
const right =
298-
t.isIdentifier(argNode.right) &&
299-
path.scope.getBinding(argNode.right.name)
300-
? { value: argNode.right.name, isRuntime: true }
293+
t.isIdentifier(arg.right) &&
294+
path.scope.getBinding(arg.right.name)
295+
? { value: arg.right.name, isRuntime: true }
301296
: {
302297
value: path.hub.file.code.slice(
303-
argNode.right.start,
304-
argNode.right.end
298+
arg.right.start,
299+
arg.right.end
305300
),
306301
isRuntime: false,
307302
};
308303

309-
const operator = argNode.operator;
304+
const operator = arg.operator;
310305

311306
return {
312307
value: {
@@ -317,20 +312,17 @@ module.exports = function (babel) {
317312
isRuntime: left.isRuntime || right.isRuntime,
318313
isBinaryExpression: true,
319314
};
320-
} else if (t.isIdentifier(argNode)) {
321-
const argBinding = path.scope.getBinding(argNode.name);
315+
} else if (t.isIdentifier(arg)) {
316+
const argBinding = path.scope.getBinding(arg.name);
322317
if (argBinding) {
323318
return {
324-
value: argNode.name,
319+
value: arg.name,
325320
isRuntime: true,
326321
};
327322
}
328323
}
329324
return {
330-
value: path.hub.file.code.slice(
331-
argNode.start,
332-
argNode.end
333-
),
325+
value: path.hub.file.code.slice(arg.start, arg.end),
334326
isRuntime: false,
335327
};
336328
});
@@ -342,7 +334,7 @@ module.exports = function (babel) {
342334
.trim();
343335

344336
// If we have runtime values, create a template literal
345-
if (args.some((calleeArg) => calleeArg.isRuntime)) {
337+
if (args.some((arg) => arg.isRuntime)) {
346338
const asyncPrefix = isAsync ? 'async ' : '';
347339
const beforeExpr = `${asyncPrefix}function ${callee.name}(${params}) { ${cleanBody} } return ${callee.name}(`;
348340
const afterExpr = ');';
@@ -360,8 +352,8 @@ module.exports = function (babel) {
360352
);
361353

362354
// Add each argument
363-
args.forEach((expressionArg, index) => {
364-
if (expressionArg.isBinaryExpression) {
355+
args.forEach((arg, index) => {
356+
if (arg.isBinaryExpression) {
365357
const { left, operator, right } = arg.value;
366358
if (left.isRuntime) {
367359
expressions.push(t.identifier(left.value));
@@ -437,10 +429,10 @@ module.exports = function (babel) {
437429
} else {
438430
const asyncPrefix = isAsync ? 'async ' : '';
439431
const functionString = `${asyncPrefix}function ${callee.name}(${params}) { ${cleanBody} } return ${callee.name}(${args
440-
.map((calleeArg) =>
441-
calleeArg.isBinaryExpression
442-
? `${calleeArg.value.left.value} ${calleeArg.value.operator} ${calleeArg.value.right.value}`
443-
: calleeArg.value
432+
.map((arg) =>
433+
arg.isBinaryExpression
434+
? `${arg.value.left.value} ${arg.value.operator} ${arg.value.right.value}`
435+
: arg.value
444436
)
445437
.join(', ')});`;
446438
path.node.arguments[0] = t.stringLiteral(functionString);

0 commit comments

Comments
 (0)