Skip to content

Home > vue-metamorph > astHelpers > findImportDeclaration

astHelpers.findImportDeclaration() function ​

Finds an existing import declaration for a module in a script AST.

Signature:

typescript
export declare function findImportDeclaration(ast: namedTypes.Program, moduleSpecifier: string): namedTypes.ImportDeclaration | null;

Parameters ​

Parameter

Type

Description

ast

namedTypes.Program

The script AST.

moduleSpecifier

string

The module name, such as 'vue' or 'lodash-es'.

Returns:

namedTypes.ImportDeclaration | null

The ImportDeclaration node, or null if there's no match.

Example ​

ts
const vueImport = findImportDeclaration(scriptAST, 'vue');
if (vueImport) {
  // An `import ... from 'vue'` declaration exists
}