Home > vue-metamorph > astHelpers > findFirst
astHelpers.findFirst() function
Finds the first node in an AST that matches a partial node using deep partial matching. Works with both script ASTs (ESTree) and template ASTs (vue-eslint-parser).
Signature:
typescript
export declare function findFirst<M extends Matcher<namedTypes.ASTNode | AST.Node>>(ast: AST.Node | namedTypes.ASTNode, matcher: M): (AST.Node & {
type: M['type'];
}) | null;Parameters
Parameter | Type | Description |
|---|---|---|
ast | AST.Node | namedTypes.ASTNode | The node to traverse |
matcher | M | Partial object to match against (uses lodash isMatch) |
Returns:
(AST.Node & { type: M['type']; }) | null
The first matching node, or null if no matching node was found
Example
ts
// Find the first <div> in the template
const div = findFirst(sfcAST, { type: 'VElement', name: 'div' });
// Find the first console.log call in a script
const log = findFirst(scriptAST, {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: { type: 'Identifier', name: 'console' },
property: { type: 'Identifier', name: 'log' },
},
});