babel-plugin-sitrep
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "babel-plugin-sitrep", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Log all assignments and the return value of a function with a simple comment", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -172,4 +172,29 @@ const pluginTester = require('babel-plugin-tester') | ||
} | ||
}, | ||
{ | ||
title: 'function has a for loop', | ||
code: ` | ||
// sitrep | ||
function test() { | ||
var sum = 0; | ||
for (var i = 0; i < 5; i++) { | ||
sum = sum + i; | ||
} | ||
return sum; | ||
} | ||
` | ||
}, | ||
{ | ||
title: 'function has a for-of loop', | ||
code: ` | ||
// sitrep | ||
function test(arr) { | ||
var sum = 0; | ||
for (const value of arr) { | ||
sum += value; | ||
} | ||
} | ||
` | ||
} | ||
] | ||
}) |
@@ -50,9 +50,9 @@ module.exports = function (babel) { | ||
function functionVisitor (path, state) { | ||
if (path.isArrowFunctionExpression()) { | ||
function functionVisitor (functionPath, state) { | ||
if (functionPath.isArrowFunctionExpression()) { | ||
return | ||
} | ||
let name = getName(path) | ||
path | ||
let name = getName(functionPath) | ||
functionPath | ||
.get('body') | ||
@@ -68,3 +68,3 @@ .unshiftContainer( | ||
let didWriteGroupEnd = false | ||
path.traverse({ | ||
functionPath.traverse({ | ||
AssignmentExpression (path) { | ||
@@ -75,4 +75,8 @@ path.insertAfter( | ||
}, | ||
VariableDeclaration (path) { | ||
const decls = path.node.declarations | ||
VariableDeclaration (variableDeclPath) { | ||
if (!variableDeclPath.parentPath.isBlockStatement()) { | ||
return | ||
} | ||
const decls = variableDeclPath.node.declarations | ||
decls.forEach(dec => { | ||
@@ -84,3 +88,3 @@ if (t.isPattern(dec.id)) { | ||
.forEach(prop => { | ||
path.insertAfter( | ||
variableDeclPath.insertAfter( | ||
t.expressionStatement( | ||
@@ -99,14 +103,14 @@ t.callExpression(logCallee, [ | ||
path.insertAfter(t.expressionStatement(createLogStatement(dec.id))) | ||
variableDeclPath.insertAfter(t.expressionStatement(createLogStatement(dec.id))) | ||
}) | ||
}, | ||
ReturnStatement (path) { | ||
const id = path.scope.generateUidIdentifier('returnValue') | ||
path.insertBefore( | ||
ReturnStatement (returnPath) { | ||
const id = returnPath.scope.generateUidIdentifier('returnValue') | ||
returnPath.insertBefore( | ||
t.variableDeclaration('var', [ | ||
t.variableDeclarator(id, path.node.argument) | ||
t.variableDeclarator(id, returnPath.node.argument) | ||
]) | ||
) | ||
path.insertBefore( | ||
returnPath.insertBefore( | ||
t.expressionStatement( | ||
@@ -119,7 +123,7 @@ t.callExpression(createGroupCallee(true, state.opts.collapsed), [ | ||
didWriteGroupEnd = true | ||
path.node.argument = id | ||
returnPath.node.argument = id | ||
} | ||
}) | ||
if (!didWriteGroupEnd) { | ||
path | ||
functionPath | ||
.get('body') | ||
@@ -126,0 +130,0 @@ .pushContainer( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
244634
803