Home > vue-metamorph > astHelpers > findFirst
astHelpers.findFirst() function
Finds the first node in an AST that matches a partial node, using deep partial matching. This function works with both script ASTs from ESTree and template ASTs from 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 | A partial object to match against. Matching uses the lodash |
Returns:
(AST.Node & { type: M['type']; }) | null
The first matching node, or null if there's no match.
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' },
},
});