-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from jsbin/refactor/babel-transform
Refactor/babel transform
- Loading branch information
Showing
12 changed files
with
4,505 additions
and
715 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,5 @@ sudo: false | |
language: node_js | ||
notifications: | ||
email: false | ||
before_script: | ||
- npm install | ||
node_js: | ||
- "0.10" | ||
- 4 | ||
- 8 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const generateBefore = (t, id) => | ||
t.variableDeclaration('var', [ | ||
t.variableDeclarator( | ||
id, | ||
t.callExpression( | ||
t.memberExpression(t.identifier('Date'), t.identifier('now')), | ||
[] | ||
) | ||
), | ||
]); | ||
|
||
const generateInside = (t, id, line, timeout) => | ||
t.ifStatement( | ||
t.binaryExpression( | ||
'>', | ||
t.binaryExpression( | ||
'-', | ||
t.callExpression( | ||
t.memberExpression(t.identifier('Date'), t.identifier('now')), | ||
[] | ||
), | ||
id | ||
), | ||
t.numericLiteral(timeout) | ||
), | ||
t.breakStatement() | ||
); | ||
|
||
const protect = (t, timeout) => path => { | ||
const id = path.scope.generateUidIdentifier('LP'); | ||
const before = generateBefore(t, id); | ||
const inside = generateInside(t, id, path.node.loc.start.line, timeout); | ||
const body = path.get('body'); | ||
|
||
// if we have an expression statement, convert it to a block | ||
if (t.isExpressionStatement(body)) { | ||
body.replaceWith(t.blockStatement([body.node])); | ||
} | ||
path.insertBefore(before); | ||
body.unshiftContainer('body', inside); | ||
}; | ||
|
||
module.exports = (timeout = 100) => ({ types: t }) => ({ | ||
visitor: { | ||
WhileStatement: protect(t, timeout), | ||
ForStatement: protect(t, timeout), | ||
DoWhileStatement: protect(t, timeout), | ||
}, | ||
}); |
Oops, something went wrong.