|
| 1 | +/** |
| 2 | + * Based on https://github.com/jsbin/loop-protect/blob/master/lib/index.js | License: http://jsbin.mit-license.org |
| 3 | + */ |
| 4 | +const generateBefore = (t, id) => |
| 5 | + t.variableDeclaration("let", [ |
| 6 | + t.variableDeclarator( |
| 7 | + id, |
| 8 | + t.callExpression( |
| 9 | + t.memberExpression(t.identifier("Date"), t.identifier("now")), |
| 10 | + [] |
| 11 | + ) |
| 12 | + ), |
| 13 | + ]); |
| 14 | + |
| 15 | +const generateInside = ({ t, id, line, ch, timeout, extra } = {}) => { |
| 16 | + return t.ifStatement( |
| 17 | + t.binaryExpression( |
| 18 | + ">", |
| 19 | + t.binaryExpression( |
| 20 | + "-", |
| 21 | + t.callExpression( |
| 22 | + t.memberExpression(t.identifier("Date"), t.identifier("now")), |
| 23 | + [] |
| 24 | + ), |
| 25 | + id |
| 26 | + ), |
| 27 | + t.numericLiteral(timeout) |
| 28 | + ), |
| 29 | + t.throwStatement(t.stringLiteral("Potential infinite loop")) |
| 30 | + ); |
| 31 | +}; |
| 32 | + |
| 33 | +const protect = (t, timeout, extra) => (path) => { |
| 34 | + if (!path.node.loc) { |
| 35 | + return; |
| 36 | + } |
| 37 | + const id = path.scope.generateUidIdentifier(randomString(16)); |
| 38 | + const before = generateBefore(t, id); |
| 39 | + const inside = generateInside({ |
| 40 | + t, |
| 41 | + id, |
| 42 | + line: path.node.loc.start.line, |
| 43 | + ch: path.node.loc.start.column, |
| 44 | + timeout, |
| 45 | + extra, |
| 46 | + }); |
| 47 | + const body = path.get("body"); |
| 48 | + |
| 49 | + // if we have an expression statement, convert it to a block |
| 50 | + if (!t.isBlockStatement(body)) { |
| 51 | + body.replaceWith(t.blockStatement([body.node])); |
| 52 | + } |
| 53 | + path.insertBefore(before); |
| 54 | + body.unshiftContainer("body", inside); |
| 55 | +}; |
| 56 | + |
| 57 | +export default (timeout = 100, extra = null) => { |
| 58 | + if (typeof extra === "string") { |
| 59 | + const string = extra; |
| 60 | + extra = `() => console.error("${string.replace(/"/g, '\\"')}")`; |
| 61 | + } else if (extra !== null) { |
| 62 | + extra = extra.toString(); |
| 63 | + if (extra.startsWith("function (")) { |
| 64 | + // fix anonymous functions as they'll cause |
| 65 | + // the callback transform to blow up |
| 66 | + extra = extra.replace(/^function \(/, "function callback("); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return ({ types: t, transform }) => { |
| 71 | + const node = extra |
| 72 | + ? transform(extra, { ast: true }).ast.program.body[0] |
| 73 | + : null; |
| 74 | + |
| 75 | + let callback = null; |
| 76 | + if (t.isExpressionStatement(node)) { |
| 77 | + callback = node.expression; |
| 78 | + } else if (t.isFunctionDeclaration(node)) { |
| 79 | + callback = t.functionExpression(null, node.params, node.body); |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + visitor: { |
| 84 | + WhileStatement: protect(t, timeout, callback), |
| 85 | + ForStatement: protect(t, timeout, callback), |
| 86 | + DoWhileStatement: protect(t, timeout, callback), |
| 87 | + }, |
| 88 | + }; |
| 89 | + }; |
| 90 | +}; |
| 91 | + |
| 92 | +function randomString(length) { |
| 93 | + const result = []; |
| 94 | + const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 95 | + const charactersLength = characters.length; |
| 96 | + let counter = 0; |
| 97 | + while (counter < length) { |
| 98 | + result.push( |
| 99 | + characters.charAt(Math.floor(Math.random() * charactersLength)) |
| 100 | + ); |
| 101 | + counter += 1; |
| 102 | + } |
| 103 | + return result.join(""); |
| 104 | +} |
0 commit comments