How to use the isFunctionExpression function from @babel/types
Find comprehensive JavaScript @babel/types.isFunctionExpression code examples handpicked from public code repositorys.
@babel/types.isFunctionExpression is a function that checks whether a given Babel AST node represents a function expression, returning a boolean value indicating the result.
67 68 69 70 71 72 73 74 75 76
// 先遍历所有函数(作用域在Program),并根据引用次数来判断是否为解密函数 traverse(this.ast, { Program(p) { p.traverse({ 'FunctionDeclaration|VariableDeclarator'(path) { if (!(t.isFunctionDeclaration(path.node) || t.isFunctionExpression(path.node.init))) { return } let name = path.node.id.name
+ 5 other calls in file
GitHub: yandex/reselector
81 82 83 84 85 86 87 88 89 90
if (parentPath && isElement(parentPath.node)) break isComponent = isComponent || ( t.isArrowFunctionExpression(node) || t.isFunctionExpression(node) || t.isClassMethod(node) || t.isObjectMethod(node) || t.isFunctionDeclaration(node) )
+ 17 other calls in file
How does @babel/types.isFunctionExpression work?
@babel/types.isFunctionExpression
is a function provided by the Babel compiler API, which allows you to programmatically analyze and transform JavaScript code.
When @babel/types.isFunctionExpression
is called with a Babel AST node as its argument, it checks whether the node represents a function expression or not by examining its type
property.
If the node represents a function expression, @babel/types.isFunctionExpression
returns true
. Otherwise, it returns false
.
This function can be useful for analyzing or transforming JavaScript code that includes function expressions, for example, to determine whether a given node needs to be transformed or analyzed further.
Note that @babel/types.isFunctionExpression
only works with Babel AST nodes and not with plain JavaScript code.
GitHub: nwjs/v8
43 44 45 46 47 48 49 50 51 52
if (babelTypes.isClassDeclaration(binding.path.node) || babelTypes.isClassExpression(binding.path.node)) { path.scope.rename(name, '__c_' + context.classIndex++); } else if (babelTypes.isFunctionDeclaration(binding.path.node) || babelTypes.isFunctionExpression(binding.path.node)) { path.scope.rename(name, '__f_' + context.funcIndex++); } else { path.scope.rename(name, '__v_' + context.varIndex++); }
315 316 317 318 319 320 321 322 323 324
let newName; if (path.node.name.startsWith('VAR_')) { newName = path.node.name; } else if (babelTypes.isFunctionDeclaration(binding.path.node) || babelTypes.isFunctionExpression(binding.path.node) || babelTypes.isDeclaration(binding.path.node) || babelTypes.isFunctionExpression(binding.path.node)) { // Unknown dependency. Don't handle this. _markSkipped(path);
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
const { parse } = require("@babel/parser"); const t = require("@babel/types"); const code = ` const add = function(x, y) { return x + y; }; `; const ast = parse(code); const addFunction = ast.program.body[0].declarations[0].init; if (t.isFunctionExpression(addFunction)) { console.log("The node is a function expression."); } else { console.log("The node is not a function expression."); }
In this example, we're using the @babel/parser module to parse a string of JavaScript code into a Babel AST. We're then accessing the first declaration in the AST, which in this case is a function expression for an add function. We're then using @babel/types.isFunctionExpression to check whether the addFunction node is a function expression or not, and using console.log to output the result. When we run this code, we get the following output: vbnet Copy code
GitHub: HarryHoudini/reselector
67 68 69 70 71 72 73 74 75 76
const prevPaths = [] do { isComponent = isComponent || ( t.isArrowFunctionExpression(currentPath.node) || t.isFunctionExpression(currentPath.node) || t.isClassExpression(currentPath.node) ) prevPaths.push(currentPath)
+ 5 other calls in file
3 4 5 6 7 8 9 10 11 12
const unnestProxyFunctions = { BlockStatement(path) { const {node, scope} = path path.stop() node.body.forEach(node => { if (!(t.isFunctionDeclaration(node) || (t.isVariableDeclaration(node) && node.declarations && t.isFunctionExpression(node.declarations[0].init)))) return const body = node.body?.body || node.declarations[0].init.body.body if (!t.isReturnStatement(body[0])) return if (t.isArrayExpression(body[0].argument)) return if (t.isCallExpression(body[0].argument) && body[0].argument.callee?.property?.name === 'apply') return
GitHub: jiangjing1994/fishhook
40 41 42 43 44 45 46 47 48 49
types.isNumericLiteral(node) || types.isStringLiteral(node) ) { return node.value } else if ( types.isFunctionExpression(node) || types.isArrowFunctionExpression(node) || types.isObjectMethod(node) ) { try {
+ 11 other calls in file
86 87 88 89 90 91 92 93 94 95
} } else if ( t.isArrayExpression(path.node.init) ){ // No handling for arrays right now } else if ( t.isBinaryExpression(path.node.init) ){ // No handling for ^^ right now } else if ( t.isFunctionExpression(path.node.init) ){ // No handling for ^^ right now } else if ( t.isUnaryExpression(path.node.init) ){ // No handling for ^^ right now } else if ( t.isConditionalExpression(path.node.init) ){
GitHub: Alanhays/ast-decode
92 93 94 95 96 97 98 99 100 101
let {node, scope} = path; const {id, init} = node; if (!types.isObjectExpression(init)) return; let properties = init.properties; if (properties.length == 0 || !properties.every(property => isBaseLiteral(property.value) || types.isFunctionExpression(property.value))) return; let binding = scope.getBinding(id.name); let {constant, referencePaths} = binding; if (!constant) return;
+ 136 other calls in file
81 82 83 84 85 86 87 88 89 90
} const { operator, left, right } = expression; if (operator === '=' && isModuleExport(left)) { // insert `module.exports = ${identifier}(module)(xxx)` if (t.isClassExpression(right) || t.isFunctionExpression(right)) { if (t.isIdentifier(right.id)) { path.replaceWith(right); insertNodes.push( t.assignmentExpression(
270 271 272 273 274 275 276 277 278 279
AssignmentExpression: (path) => { const { node } = path; // Proxy function if ( t.isFunctionExpression(node.right) && t.isMemberExpression(node.left) && (node.left.property.value || node.left.property.name) && node.right.body.body.length === 1 && t.isReturnStatement(node.right.body.body[0]) &&
+ 36 other calls in file
GitHub: Blue-Skie/YuanRenXue-Web
427 428 429 430 431 432 433 434 435 436
path.remove(); } else if((sourceCode.includes("constructor") || sourceCode.includes("RegExp")) && types.isIdentifier(callee) && arguments.length == 2 && types.isThisExpression(arguments[0]) && types.isFunctionExpression(arguments[1])) { let funcName = id.name; let nextSibling = path.parentPath.getNextSibling();
+ 71 other calls in file
272 273 274 275 276 277 278 279 280 281
if (scope && bindings[varName]) { var parentNode = scope.path.parent; var isReactComponentBlock = false; if ((types.isArrowFunctionExpression(scope.block) || types.isFunctionExpression(scope.block)) && types.isVariableDeclarator(parentNode) && types.isIdentifier(parentNode.id)) { isReactComponentBlock = /^use/.test(parentNode.id.name) || /^_?[A-Z]/.test(parentNode.id.name); } else if (types.isFunctionDeclaration(scope.block) && types.isIdentifier(scope.block.id)) { isReactComponentBlock = /^use/.test(scope.block.id.name) || /^_?[A-Z]/.test(scope.block.id.name); } // console.log(
+ 3 other calls in file
96 97 98 99 100 101 102 103 104 105
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); }
21 22 23 24 25 26 27 28 29 30
async: true }) || t.isArrowFunctionExpression(node, { async: true }) || t.isFunctionExpression(node, { async: true }) || t.isObjectMethod(node, { async: true
@babel/types.identifier is the most popular function in @babel/types (20936 examples)