function getEnclosingStatement(path: Path): Path { let resultPath = path; while (!t.isProgram(resultPath.parentPath.node)) { resultPath = resultPath.parentPath; } return resultPath; }
function hasBinding(node, name) { if (Array.isArray(node)) { return node.some(ancestor => hasBinding(ancestor, name)); } else if ( types.isProgram(node) || types.isBlockStatement(node) || types.isBlock(node) ) { return node.body.some(statement => hasBinding(statement, name)); } else if ( types.isFunctionDeclaration(node) || types.isFunctionExpression(node) || types.isArrowFunctionExpression(node) ) { return ( (node.id && node.id.name === name) || node.params.some( param => types.isIdentifier(param) && param.name === name ) ); } else if (types.isVariableDeclaration(node)) { return node.declarations.some(declaration => declaration.id.name === name); } return false; }
function isTopLevelThis(path: Path): boolean { if (!t.isThisExpression(path)) { return false; } let ancestor = path; while (!t.isProgram(ancestor.node)) { if (t.isFunction(ancestor.node) && !t.isArrowFunctionExpression(ancestor.node)) { return false; } ancestor = ancestor.parentPath; } return true; }
print(node, parent) { if (!node) return; const oldConcise = this.format.concise; if (node._compact) { this.format.concise = true; } const printMethod = this[node.type]; if (!printMethod) { throw new ReferenceError( `unknown node of type ${JSON.stringify( node.type, )} with constructor ${JSON.stringify(node && node.constructor.name)}`, ); } this._printStack.push(node); const oldInAux = this._insideAux; this._insideAux = !node.loc; this._maybeAddAuxComment(this._insideAux && !oldInAux); let needsParens = n.needsParens(node, parent, this._printStack); if ( this.format.retainFunctionParens && node.type === "FunctionExpression" && node.extra && node.extra.parenthesized ) { needsParens = true; } if (needsParens) this.token("("); this._printLeadingComments(node); const loc = t.isProgram(node) || t.isFile(node) ? null : node.loc; this.withSource("start", loc, () => { printMethod.call(this, node, parent); }); this._printTrailingComments(node); if (needsParens) this.token(")"); // end this._printStack.pop(); this.format.concise = oldConcise; this._insideAux = oldInAux; }
/** * @private */ function extractModuleIIFE(node: Node): ?Node { if (!t.isProgram(node)) { return null; } if (node.body.length !== 1) { return null; } let [ statement ] = node.body; if (!t.isExpressionStatement(statement)) { return null; } let { expression: call } = statement; if (t.isUnaryExpression(call) && call.operator === 'void') { // e.g. `void function(){}();` call = call.argument; } if (!t.isCallExpression(call)) { return null; } let { callee, arguments: args } = call; let iife; if (t.isFunctionExpression(callee)) { // e.g. `(function() {})();` if (args.length !== 0) { return null; } iife = callee; } else if (t.isMemberExpression(callee)) { // e.g. `(function() {}).call(this);` let { object, property, computed } = callee; if (computed || !t.isFunctionExpression(object)) { return null; } if (!t.isIdentifier(property, { name: 'call' })) { return null; } if (args.length !== 1 || !t.isThisExpression(args[0])) { return null; } iife = object; } else { return null; } if (iife.id || iife.params.length > 0 || iife.generator) { return null; } return iife; }
// eslint-disable-next-line complexity enter( node: Node, ancestors: TraversalAncestors, state: ScopeCollectionVisitorState ) { state.scopeStack.push(state.scope); const parentNode = ancestors.length === 0 ? null : ancestors[ancestors.length - 1].node; if (state.inType) { return; } if (t.isProgram(node)) { const scope = pushTempScope(state, "module", "Module", { start: fromBabelLocation(node.loc.start, state.sourceId), end: fromBabelLocation(node.loc.end, state.sourceId), }); scope.bindings.this = { type: "implicit", refs: [], }; } else if (t.isFunction(node)) { let scope = state.scope; if (t.isFunctionExpression(node) && isNode(node.id, "Identifier")) { scope = pushTempScope(state, "block", "Function Expression", { start: fromBabelLocation(node.loc.start, state.sourceId), end: fromBabelLocation(node.loc.end, state.sourceId), });
export function replaceWith(replacement) { this.resync(); if (this.removed) { throw new Error("You can't replace this node, we've already removed it"); } if (replacement instanceof NodePath) { replacement = replacement.node; } if (!replacement) { throw new Error( "You passed `path.replaceWith()` a falsy node, use `path.remove()` instead", ); } if (this.node === replacement) { return [this]; } if (this.isProgram() && !t.isProgram(replacement)) { throw new Error( "You can only replace a Program root node with another Program node", ); } if (Array.isArray(replacement)) { throw new Error( "Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`", ); } if (typeof replacement === "string") { throw new Error( "Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`", ); } let nodePath = ""; if (this.isNodeType("Statement") && t.isExpression(replacement)) { if ( !this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration() ) { // replacing a statement with an expression so wrap it in an expression statement replacement = t.expressionStatement(replacement); nodePath = "expression"; } } if (this.isNodeType("Expression") && t.isStatement(replacement)) { if ( !this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) ) { // replacing an expression with a statement so let's explode it return this.replaceExpressionWithStatements([replacement]); } } const oldNode = this.node; if (oldNode) { t.inheritsComments(replacement, oldNode); t.removeComments(oldNode); } // replace the node this._replaceWith(replacement); this.type = replacement.type; // potentially create new scope this.setScope(); // requeue for visiting this.requeue(); return [nodePath ? this.get(nodePath) : this]; }