Skip to content

Commit ead9d43

Browse files
author
Fernando Basello
committed
fix: Passing js functions to enqueueItem do not work #4
closes #4
1 parent cad2b78 commit ead9d43

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

src/plugins/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function (babel) {
2424
.trim();
2525

2626
const asyncPrefix = isAsync ? 'async ' : '';
27-
return `${asyncPrefix}function ${functionName}(${params}) { ${es5Body} } ${functionName}();`;
27+
return `(function() { ${asyncPrefix}function ${functionName}(${params}) { ${es5Body} } return ${functionName}(); })();`;
2828
}
2929

3030
function extractFunctionParts(path, node) {
@@ -336,8 +336,8 @@ module.exports = function (babel) {
336336
// If we have runtime values, create a template literal
337337
if (args.some((arg) => arg.isRuntime)) {
338338
const asyncPrefix = isAsync ? 'async ' : '';
339-
const beforeExpr = `${asyncPrefix}function ${callee.name}(${params}) { ${cleanBody} } return ${callee.name}(`;
340-
const afterExpr = ');';
339+
const beforeExpr = `(function() { ${asyncPrefix}function ${callee.name}(${params}) { ${cleanBody} } return ${callee.name}(`;
340+
const afterExpr = '); })();';
341341

342342
// Create template elements for each argument
343343
const quasis = [];
@@ -428,13 +428,13 @@ module.exports = function (babel) {
428428
);
429429
} else {
430430
const asyncPrefix = isAsync ? 'async ' : '';
431-
const functionString = `${asyncPrefix}function ${callee.name}(${params}) { ${cleanBody} } return ${callee.name}(${args
431+
const functionString = `(function() { ${asyncPrefix}function ${callee.name}(${params}) { ${cleanBody} } return ${callee.name}(${args
432432
.map((arg) =>
433433
arg.isBinaryExpression
434434
? `${arg.value.left.value} ${arg.value.operator} ${arg.value.right.value}`
435435
: arg.value
436436
)
437-
.join(', ')});`;
437+
.join(', ')}); })();`;
438438
path.node.arguments[0] = t.stringLiteral(functionString);
439439
}
440440
}

src/plugins/tests/plugin.test.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pluginTester({
2525
return 'done';
2626
};
2727
enqueueItem(
28-
"function loopForever(count) { for (let i = 0; i < count; i++) {} return 'done'; } return loopForever(100000000);"
28+
"(function() { function loopForever(count) { for (let i = 0; i < count; i++) {} return 'done'; } return loopForever(100000000); })();"
2929
);
3030
`,
3131
},
@@ -46,7 +46,7 @@ pluginTester({
4646
return 'done';
4747
};
4848
enqueueItem(
49-
\`function loopForever(count) { for (let i = 0; i < count; i++) {} return 'done'; } return loopForever(\${amount});\`
49+
\`(function() { function loopForever(count) { for (let i = 0; i < count; i++) {} return 'done'; } return loopForever(\${amount}); })();\`
5050
);
5151
`,
5252
},
@@ -67,7 +67,7 @@ pluginTester({
6767
return 'done';
6868
};
6969
enqueueItem(
70-
\`function loopForever(count) { for (let i = 0; i < count; i++) {} return 'done'; } return loopForever(\${base} * 100);\`
70+
\`(function() { function loopForever(count) { for (let i = 0; i < count; i++) {} return 'done'; } return loopForever(\${base} * 100); })();\`
7171
);
7272
`,
7373
},
@@ -86,7 +86,7 @@ pluginTester({
8686
return label;
8787
};
8888
enqueueItem(
89-
'function processData(count, label) { for (let i = 0; i < count; i++) {} return label; } return processData(1000, "processing");'
89+
'(function() { function processData(count, label) { for (let i = 0; i < count; i++) {} return label; } return processData(1000, "processing"); })();'
9090
);
9191
`,
9292
},
@@ -102,7 +102,9 @@ pluginTester({
102102
function funcToRun() {
103103
return 12234;
104104
}
105-
enqueueItem('function funcToRun() { return 12234; } funcToRun();');
105+
enqueueItem(
106+
'(function() { function funcToRun() { return 12234; } return funcToRun(); })();'
107+
);
106108
`,
107109
},
108110
{
@@ -124,7 +126,7 @@ pluginTester({
124126
};
125127
}
126128
enqueueItem(
127-
"function funcToRun() { return { test: 'test', otroTest: 'otroTest', }; } funcToRun();"
129+
"(function() { function funcToRun() { return { test: 'test', otroTest: 'otroTest', }; } return funcToRun(); })();"
128130
);
129131
`,
130132
},
@@ -140,7 +142,9 @@ pluginTester({
140142
const funcToRun = () => {
141143
return 12234;
142144
};
143-
enqueueItem('function funcToRun() { return 12234; } funcToRun();');
145+
enqueueItem(
146+
'(function() { function funcToRun() { return 12234; } return funcToRun(); })();'
147+
);
144148
`,
145149
},
146150
{
@@ -165,7 +169,9 @@ pluginTester({
165169
function funcToRun() {
166170
return 12234;
167171
}
168-
worker.enqueueItem('function funcToRun() { return 12234; } funcToRun();');
172+
worker.enqueueItem(
173+
'(function() { function funcToRun() { return 12234; } return funcToRun(); })();'
174+
);
169175
`,
170176
},
171177
{
@@ -181,7 +187,7 @@ pluginTester({
181187
return 12234;
182188
}
183189
some.deeply.nested.path.enqueueItem(
184-
'function funcToRun() { return 12234; } funcToRun();'
190+
'(function() { function funcToRun() { return 12234; } return funcToRun(); })();'
185191
);
186192
`,
187193
},
@@ -199,7 +205,7 @@ pluginTester({
199205
return 12234;
200206
}
201207
_reactNative.NativeModules.HermesWorker.enqueueItem(
202-
'function funcToRun() { return 12234; } funcToRun();'
208+
'(function() { function funcToRun() { return 12234; } return funcToRun(); })();'
203209
);
204210
`,
205211
},
@@ -216,7 +222,7 @@ pluginTester({
216222
return await Promise.resolve(123);
217223
}
218224
NativeModules.HermesWorker.enqueueItem(
219-
'async function asyncFunc() { return await Promise.resolve(123); } asyncFunc();'
225+
'(function() { async function asyncFunc() { return await Promise.resolve(123); } return asyncFunc(); })();'
220226
);
221227
`,
222228
},
@@ -233,7 +239,7 @@ pluginTester({
233239
return a + b;
234240
}
235241
NativeModules.HermesWorker.enqueueItem(
236-
'function paramFunc(a, b) { return a + b; } paramFunc();'
242+
'(function() { function paramFunc(a, b) { return a + b; } return paramFunc(); })();'
237243
);
238244
`,
239245
},
@@ -252,7 +258,7 @@ pluginTester({
252258
return 'pepito';
253259
};
254260
enqueueItem(
255-
"function loopForeverSync() { for (var i = 0; i < 100000000; i++) {} return 'pepito'; } loopForeverSync();"
261+
"(function() { function loopForeverSync() { for (var i = 0; i < 100000000; i++) {} return 'pepito'; } return loopForeverSync(); })();"
256262
);
257263
`,
258264
},
@@ -282,7 +288,7 @@ pluginTester({
282288
};
283289
};
284290
enqueueItem(
285-
'function complexFunc() { var result = []; for (var i = 0; i < 10; i++) { result.push(i * 2); } return { data: result, sum: result.reduce(function(a, b) { return a + b; }, 0) }; } complexFunc();'
291+
'(function() { function complexFunc() { var result = []; for (var i = 0; i < 10; i++) { result.push(i * 2); } return { data: result, sum: result.reduce(function(a, b) { return a + b; }, 0) }; } return complexFunc(); })();'
286292
);
287293
`,
288294
},
@@ -307,7 +313,7 @@ pluginTester({
307313
return \`Result: \${count}\`;
308314
};
309315
_reactNative.NativeModules.HermesWorker.enqueueItem(
310-
"function loopForeverSync() { var count = 0; for (var i = 0; i < 100000000; i++) { count += i; } return 'Result: \${count}'; } loopForeverSync();"
316+
"(function() { function loopForeverSync() { var count = 0; for (var i = 0; i < 100000000; i++) { count += i; } return 'Result: \${count}'; } return loopForeverSync(); })();"
311317
);
312318
`,
313319
},
@@ -334,7 +340,7 @@ pluginTester({
334340
return items;
335341
};
336342
enqueueItem(
337-
"function complexFunc() { var items = []; var max = 10; for (var i = 0; i < max; i++) { items.push('Item \${i}'); } return items; } complexFunc();"
343+
"(function() { function complexFunc() { var items = []; var max = 10; for (var i = 0; i < max; i++) { items.push('Item \${i}'); } return items; } return complexFunc(); })();"
338344
);
339345
`,
340346
},
@@ -353,7 +359,7 @@ pluginTester({
353359
return 'pepito';
354360
};
355361
enqueueItem(
356-
"function loopForeverSync(amount) { for (let i = 0; i < amount; i++) {} return 'pepito'; } return loopForeverSync(100000000);"
362+
"(function() { function loopForeverSync(amount) { for (let i = 0; i < amount; i++) {} return 'pepito'; } return loopForeverSync(100000000); })();"
357363
);
358364
`,
359365
},
@@ -372,7 +378,7 @@ pluginTester({
372378
return label;
373379
};
374380
enqueueItem(
375-
'function processData(count, label) { for (let i = 0; i < count; i++) {} return label; } return processData(1000, "processing");'
381+
'(function() { function processData(count, label) { for (let i = 0; i < count; i++) {} return label; } return processData(1000, "processing"); })();'
376382
);
377383
`,
378384
},
@@ -396,7 +402,7 @@ pluginTester({
396402
`,
397403
output: `
398404
import { externalFunc } from 'some-module';
399-
enqueueItem('externalFunc(123, "test")');
405+
enqueueItem('externalFunc(123, \"test\")');
400406
`,
401407
},
402408
{
@@ -428,7 +434,9 @@ pluginTester({
428434
return value;
429435
};
430436
const newValue = 123 + 123;
431-
enqueueItem(\`function loop(value) { return value; } return loop(\${newValue});\`);
437+
enqueueItem(
438+
\`(function() { function loop(value) { return value; } return loop(\${newValue}); })();\`
439+
);
432440
`,
433441
},
434442
{
@@ -445,7 +453,9 @@ pluginTester({
445453
return value;
446454
};
447455
const a = anotherValue();
448-
enqueueItem(\`function loop(value) { return value; } return loop(\${a});\`);
456+
enqueueItem(
457+
\`(function() { function loop(value) { return value; } return loop(\${a}); })();\`
458+
);
449459
`,
450460
},
451461
{
@@ -465,7 +475,7 @@ pluginTester({
465475
return 'done waiting';
466476
};
467477
enqueueItem(
468-
\`async function asyncLoop(waitTime) { await new Promise(resolve => setTimeout(resolve, waitTime)); return 'done waiting'; } return asyncLoop(\${delay});\`
478+
\`(function() { async function asyncLoop(waitTime) { await new Promise(resolve => setTimeout(resolve, waitTime)); return 'done waiting'; } return asyncLoop(\${delay}); })();\`
469479
);
470480
`,
471481
},
@@ -486,7 +496,7 @@ pluginTester({
486496
return 'done waiting';
487497
};
488498
enqueueItem(
489-
\`async function asyncLoop(waitTime) { await new Promise(resolve => setTimeout(resolve, waitTime)); return 'done waiting'; } return asyncLoop(\${baseDelay} * 2);\`
499+
\`(function() { async function asyncLoop(waitTime) { await new Promise(resolve => setTimeout(resolve, waitTime)); return 'done waiting'; } return asyncLoop(\${baseDelay} * 2); })();\`
490500
);
491501
`,
492502
},

0 commit comments

Comments
 (0)