babel-plugin-debug-tools
Advanced tools
Comparing version 1.0.20 to 1.0.21
197
lib/index.js
@@ -119,52 +119,159 @@ "use strict"; | ||
function transpileASSERT() { | ||
const nexpr = t.callExpression(t.clone(callee.node), [calleeLoc()].concat(expr.node.arguments.reduce((prev, curr) => { | ||
if (t.isRegExpLiteral(curr)) { | ||
prev.push(t.clone(curr)); | ||
} else { | ||
const cclone = t.clone(curr); | ||
const cname = t.stringLiteral((0, _generator.default)(cclone).code); | ||
const assertions = []; | ||
if (expr.node.arguments.length === 0) throw path.buildCodeFrameError("Assert without arguments"); | ||
expr.node.arguments.forEach(curr => { | ||
handlArg(curr); | ||
}, []); | ||
const nexpr = t.callExpression(t.clone(callee.node), [calleeLoc()].concat(assertions.map(a => t.arrayExpression([a.caption, a.expr, t.objectExpression(Object.keys(a.fields).map(fn => t.objectProperty(t.stringLiteral(fn), a.fields[fn])))])))); | ||
path.get('expression').replaceWith(nexpr); | ||
if (t.isBinaryExpression(cclone)) { | ||
const leftId = path.scope.generateUidIdentifier('left'); | ||
const cnameleft = t.stringLiteral((0, _generator.default)(cclone.left).code); | ||
const cnameright = t.stringLiteral((0, _generator.default)(cclone.right).code); | ||
const isLiteralleft = t.isLiteral(cclone.left); | ||
const isLiteralright = t.isLiteral(cclone.right); | ||
path.scope.push({ | ||
id: leftId | ||
}); | ||
const rightId = path.scope.generateUidIdentifier('right'); | ||
path.scope.push({ | ||
id: rightId | ||
}); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', leftId, t.clone(cclone.left)))); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', rightId, t.clone(cclone.right)))); | ||
cclone.left = leftId; | ||
cclone.right = rightId; | ||
const aexpr = t.arrayExpression([cname, cclone]); | ||
prev.push(aexpr); | ||
const binExpr = t.objectExpression([]); | ||
const left = t.clone(cclone.left); | ||
if (!isLiteralleft) binExpr.properties.push(t.objectProperty(cnameleft, left)); | ||
const right = t.clone(cclone.right); | ||
if (!isLiteralright) binExpr.properties.push(t.objectProperty(cnameright, right)); | ||
aexpr.elements.push(binExpr); | ||
} else if (t.isUnaryExpression(cclone)) { | ||
const argId = path.scope.generateUidIdentifier('arg'); | ||
const cnameArg = t.stringLiteral((0, _generator.default)(cclone.argument).code); | ||
const isLiteralArg = t.isLiteral(cclone.argument); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', argId, t.clone(cclone.argument)))); | ||
cclone.argument = argId; | ||
const aexpr = t.arrayExpression([cname, cclone]); | ||
prev.push(aexpr); | ||
function handlArg(argExpr) { | ||
if (t.isRegExpLiteral(argExpr)) { | ||
add(t.stringLiteral(argExpr.pattern), t.clone(argExpr), {}); | ||
} else if (t.isObjectExpression(argExpr)) { | ||
argExpr.properties.forEach(p => { | ||
if (t.isSpreadElement(p)) throw path.buildCodeFrameError("Spread is not supported"); | ||
if (!isLiteralArg) { | ||
aexpr.elements.push(t.objectExpression([t.objectProperty(cnameArg, argId)])); | ||
if (t.isObjectMethod(p)) { | ||
if (p.params.length) throw path.buildCodeFrameError("Remove arguments of methods"); | ||
add(p.key, t.callExpression(t.functionExpression(null, [], t.clone(p.body)), []), {}); | ||
} else { | ||
const eaditionalFields = {}; | ||
const { | ||
caption, | ||
val | ||
} = explainExpr(p.value, eaditionalFields, false); | ||
add(t.stringLiteral(caption), val, eaditionalFields); | ||
} | ||
} else throw path.buildCodeFrameError("unsupported expression"); | ||
}); | ||
} else { | ||
const eaditionalFields = {}; | ||
const { | ||
caption, | ||
val | ||
} = explainExpr(argExpr, eaditionalFields, false); | ||
add(t.stringLiteral(caption), val, eaditionalFields); | ||
} | ||
} | ||
return prev; | ||
}, []))); | ||
path.get('expression').replaceWith(nexpr); | ||
function add(caption, expr, fields) { | ||
const a = { | ||
caption, | ||
expr, | ||
fields | ||
}; | ||
assertions.push(a); | ||
return a; | ||
} | ||
function explainExpr(bigExpr, fields, addf) { | ||
if (t.isStringLiteral(bigExpr)) { | ||
return { | ||
caption: bigExpr.value, | ||
val: bigExpr | ||
}; | ||
} else if (t.isNumericLiteral(bigExpr)) { | ||
return { | ||
caption: bigExpr.value.toString(), | ||
val: bigExpr | ||
}; | ||
} else if (t.isBooleanLiteral(bigExpr)) { | ||
return { | ||
caption: bigExpr.value ? 'true' : 'false', | ||
val: bigExpr | ||
}; | ||
} else if (t.isNullLiteral(bigExpr)) { | ||
return { | ||
caption: 'null', | ||
val: bigExpr | ||
}; | ||
} else if (t.isIdentifier(bigExpr)) { | ||
if (addf) fields[bigExpr.name] = t.clone(bigExpr); | ||
return { | ||
caption: bigExpr.name, | ||
val: bigExpr | ||
}; | ||
} else if (t.isCallExpression(bigExpr)) { | ||
bigExpr.arguments.forEach(a => { | ||
explainExpr(a, fields, true); | ||
}); | ||
const valId = path.scope.generateUidIdentifier('dbg'); | ||
path.scope.push({ | ||
id: valId | ||
}); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', valId, t.clone(bigExpr)))); | ||
return { | ||
caption: (0, _generator.default)(bigExpr).code, | ||
val: valId | ||
}; | ||
} else if (t.isBinaryExpression(bigExpr)) { | ||
const left = explainExpr(bigExpr.left, fields, true); | ||
const right = explainExpr(bigExpr.right, fields, true); | ||
const caption = (0, _generator.default)(bigExpr).code; | ||
const expr = t.binaryExpression(bigExpr.operator, t.clone(left.val), t.clone(right.val)); | ||
const valId = path.scope.generateUidIdentifier('dbg'); | ||
path.scope.push({ | ||
id: valId | ||
}); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', valId, expr))); | ||
fields[caption] = t.clone(valId); | ||
return { | ||
caption, | ||
val: t.clone(valId) | ||
}; | ||
} else if (t.isLogicalExpression(bigExpr)) { | ||
const left = explainExpr(bigExpr.left, fields, true); | ||
const right = explainExpr(bigExpr.right, fields, true); | ||
const caption = (0, _generator.default)(bigExpr).code; | ||
const expr = t.logicalExpression(bigExpr.operator, t.clone(left.val), t.clone(right.val)); | ||
const valId = path.scope.generateUidIdentifier('dbg'); | ||
path.scope.push({ | ||
id: valId | ||
}); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', valId, expr))); | ||
fields[caption] = t.clone(valId); | ||
return { | ||
caption, | ||
val: t.clone(valId) | ||
}; | ||
} else if (t.isConditionalExpression(bigExpr)) { | ||
const test = explainExpr(bigExpr.test, fields, true); | ||
const consequent = explainExpr(bigExpr.consequent, fields, true); | ||
const alternate = explainExpr(bigExpr.alternate, fields, true); | ||
const caption = (0, _generator.default)(bigExpr).code; | ||
const expr = t.conditionalExpression(t.clone(test.val), t.clone(consequent.val), t.clone(alternate.val)); | ||
const valId = path.scope.generateUidIdentifier('dbg'); | ||
path.scope.push({ | ||
id: valId | ||
}); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', valId, expr))); | ||
fields[caption] = t.clone(valId); | ||
return { | ||
caption, | ||
val: t.clone(valId) | ||
}; | ||
} else if (t.isUnaryExpression(bigExpr)) { | ||
const arg = explainExpr(bigExpr.argument, fields, true); | ||
const caption = (0, _generator.default)(bigExpr).code; | ||
const expr = t.unaryExpression(bigExpr.operator, t.clone(arg.val)); | ||
const valId = path.scope.generateUidIdentifier('dbg'); | ||
path.scope.push({ | ||
id: valId | ||
}); | ||
path.insertBefore(t.expressionStatement(t.assignmentExpression('=', valId, expr))); | ||
fields[caption] = t.clone(valId); | ||
return { | ||
caption, | ||
val: t.clone(valId) | ||
}; // const cnameArg = t.stringLiteral(generate(cclone.argument).code) | ||
// const isLiteralArg = t.isLiteral(cclone.argument) | ||
// cclone.argument = argId | ||
// if (!(isLiteralArg)) { | ||
// aditionalFields.push(t.objectProperty(cnameArg, argId)) | ||
// } | ||
// return { caption: cname, expr: cclone } | ||
} | ||
throw path.buildCodeFrameError("unsupported expression: " + bigExpr.type); | ||
} | ||
} | ||
@@ -171,0 +278,0 @@ |
{ | ||
"name": "babel-plugin-debug-tools", | ||
"version": "1.0.20", | ||
"version": "1.0.21", | ||
"description": "Helpers for debug Javascript Applications", | ||
@@ -5,0 +5,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53136
275