Skip to content

Commit

Permalink
fix: anonymous callback handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Jan 25, 2018
1 parent 77c5c0e commit 357f74a
Show file tree
Hide file tree
Showing 8 changed files with 1,007 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
tmp
dist
27 changes: 17 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ const generateInside = ({ t, id, line, ch, timeout, extra } = {}) => {
),
extra
? t.blockStatement([
t.expressionStatement(
t.callExpression(extra, [t.numericLiteral(line), t.numericLiteral(ch)])
),
t.breakStatement(),
])
t.expressionStatement(
t.callExpression(extra, [
t.numericLiteral(line),
t.numericLiteral(ch),
])
),
t.breakStatement(),
])
: t.breakStatement()
);
};
Expand Down Expand Up @@ -59,13 +62,17 @@ module.exports = (timeout = 100, extra = null) => {
if (typeof extra === 'string') {
const string = extra;
extra = `() => console.error("${string.replace(/"/g, '\\"')}")`;
} else if (extra !== null) {
extra = extra.toString();
if (extra.startsWith('function (')) {
// fix anonymous functions as they'll cause
// the callback transform to blow up
extra = extra.replace(/^function \(/, 'function callback(');
}
}
return ({ types: t, transform }) => {
const node = extra
? transform(extra).ast.program.body[0]
: null;

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

let callback = null;
if (t.isExpressionStatement(node)) {
Expand Down
Loading

0 comments on commit 357f74a

Please sign in to comment.