Skip to content

Commit

Permalink
Merge pull request #17 from jsbin/refactor/babel-transform
Browse files Browse the repository at this point in the history
Refactor/babel transform
  • Loading branch information
remy committed Jan 11, 2018
2 parents 7eb9db8 + e8fb1dd commit b955448
Show file tree
Hide file tree
Showing 12 changed files with 4,505 additions and 715 deletions.
15 changes: 0 additions & 15 deletions .editorconfig

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules
tmp
15 changes: 0 additions & 15 deletions .jshintrc

This file was deleted.

5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ sudo: false
language: node_js
notifications:
email: false
before_script:
- npm install
node_js:
- "0.10"
- 4
- 8
43 changes: 0 additions & 43 deletions Gruntfile.js

This file was deleted.

2 changes: 0 additions & 2 deletions dist/loop-protect.min.js

This file was deleted.

49 changes: 49 additions & 0 deletions lib/index.js
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),
},
});
Loading

0 comments on commit b955448

Please sign in to comment.