Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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