Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import {
createTempVarIdentifier,
createAssignmentExprStmt,
createVarDeclaration,
createBlockExprStmt
} from '../node-builder';
import replaceNode from './replace-node';
// Transform:
// for(obj[prop] in src), for(obj.prop in src) -->
// for(const __set$temp in src) { obj[prop] = __set$temp; }
export default {
nodeReplacementRequireTransform: false,
nodeTypes: [Syntax.ForInStatement],
condition: node => node.left.type === Syntax.MemberExpression,
run: node => {
const tempVarAst = createTempVarIdentifier();
const varDeclaration = createVarDeclaration(tempVarAst);
const assignmentExprStmt = createAssignmentExprStmt(node.left, tempVarAst);
if (node.body.type !== Syntax.BlockStatement)
replaceNode(node.body, createBlockExprStmt([assignmentExprStmt, node.body]), node, 'body');
else
replaceNode(null, assignmentExprStmt, node.body, 'body');
replaceNode(node.left, varDeclaration, node, 'left');
return null;
return false;
// object[prop]++ || object[prop]-- || ++object[prop] || --object[prop]
if (parent.type === Syntax.UpdateExpression && parent.operator === '++' || parent.operator === '--')
return false;
// object[prop]()
if (parent.type === Syntax.CallExpression && parent.callee === node)
return false;
// new (object[prop])() || new (object[prop])
if (parent.type === Syntax.NewExpression && parent.callee === node)
return false;
// for(object[prop] in source)
if (parent.type === Syntax.ForInStatement && parent.left === node)
return false;
return true;
},
return false;
// Skip: object.prop()
if (parent.type === Syntax.CallExpression && parent.callee === node)
return false;
// Skip: object.prop++ || object.prop-- || ++object.prop || --object.prop
if (parent.type === Syntax.UpdateExpression && parent.operator === '++' || parent.operator === '--')
return false;
// Skip: new (object.prop)() || new (object.prop)
if (parent.type === Syntax.NewExpression && parent.callee === node)
return false;
// Skip: for(object.prop in source)
if (parent.type === Syntax.ForInStatement && parent.left === node)
return false;
return true;
},
condition: (node, parent) => {
// Skip: for(location.field in obj)
if (parent && parent.type === Syntax.ForInStatement && parent.left === node)
return false;
return node.object.type === Syntax.Identifier && node.object.name === 'location';
},