Skip to content

Home > vue-metamorph > astHelpers > findAll

astHelpers.findAll() function ​

Finds every 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 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

A partial object to match against. Matching uses the lodash isMatch function.

Returns:

(AST.Node & { type: M['type']; })[]

Every matching node.

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