Skip to content

Home > vue-metamorph > astHelpers > findAll

astHelpers.findAll() function

Finds all nodes in an AST that match a partial node using deep partial matching. Works with both script ASTs (ESTree) and template ASTs (vue-eslint-parser).

Signature:

typescript
export declare function findAll<M extends Matcher<namedTypes.ASTNode | AST.Node>>(ast: AST.Node | namedTypes.ASTNode, matcher: M): (AST.Node & {
    type: M['type'];
})[];

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']; })[]

All matching nodes

Example

ts
// Find all <MyComponent> elements in the template
const els = findAll(sfcAST, { type: 'VElement', name: 'MyComponent' });

// Find all v-if directives
const vIfs = findAll(sfcAST, {
  type: 'VAttribute',
  directive: true,
  key: { type: 'VDirectiveKey', name: { name: 'if' } },
});

// Find all call expressions in a script
const calls = findAll(scriptAST, { type: 'CallExpression' });