Skip to content

Commit 357f74a

Browse files
committed
fix: anonymous callback handlers
1 parent 77c5c0e commit 357f74a

File tree

8 files changed

+1007
-41
lines changed

8 files changed

+1007
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
node_modules
33
tmp
4+
dist

lib/index.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ const generateInside = ({ t, id, line, ch, timeout, extra } = {}) => {
2525
),
2626
extra
2727
? t.blockStatement([
28-
t.expressionStatement(
29-
t.callExpression(extra, [t.numericLiteral(line), t.numericLiteral(ch)])
30-
),
31-
t.breakStatement(),
32-
])
28+
t.expressionStatement(
29+
t.callExpression(extra, [
30+
t.numericLiteral(line),
31+
t.numericLiteral(ch),
32+
])
33+
),
34+
t.breakStatement(),
35+
])
3336
: t.breakStatement()
3437
);
3538
};
@@ -59,13 +62,17 @@ module.exports = (timeout = 100, extra = null) => {
5962
if (typeof extra === 'string') {
6063
const string = extra;
6164
extra = `() => console.error("${string.replace(/"/g, '\\"')}")`;
65+
} else if (extra !== null) {
66+
extra = extra.toString();
67+
if (extra.startsWith('function (')) {
68+
// fix anonymous functions as they'll cause
69+
// the callback transform to blow up
70+
extra = extra.replace(/^function \(/, 'function callback(');
71+
}
6272
}
63-
return ({ types: t, transform }) => {
64-
const node = extra
65-
? transform(extra).ast.program.body[0]
66-
: null;
6773

68-
// console.log(node && node.type)
74+
return ({ types: t, transform }) => {
75+
const node = extra ? transform(extra).ast.program.body[0] : null;
6976

7077
let callback = null;
7178
if (t.isExpressionStatement(node)) {

0 commit comments

Comments
 (0)