Scan files for import declarations.
Using the TypeScript Compiler API, it scans files for import declarations and retrieves their information.
npm install @yuheiy/import-scanner
// src/index.ts
import MyModule from 'my-module';
import { a as A, b as B } from 'another-module';
import { scanImportDeclarations } from '@yuheiy/import-scanner';
const importDeclarations = scanImportDeclarations('src/index.ts');
console.log(importDeclarations);
/*
[
{
"filePath": "src/index.ts",
"position": {
"start": 16,
"end": 49
},
"line": {
"start": 2,
"end": 2
},
"statement": "import MyModule from 'my-module';",
"moduleSpecifierValue": "my-module",
"details": {
"type": "default_import",
"isTypeOnly": false,
"importedBinding": "MyModule"
}
},
{
"filePath": "src/index.ts",
"position": {
"start": 50,
"end": 98
},
"line": {
"start": 3,
"end": 3
},
"statement": "import { a as A, b as B } from 'another-module';",
"moduleSpecifierValue": "another-module",
"details": {
"type": "named_imports",
"elements": [
{
"isTypeOnly": false,
"importedBinding": "A",
"moduleExportName": "a"
},
{
"isTypeOnly": false,
"importedBinding": "B",
"moduleExportName": "b"
}
]
}
}
]
*/
Return a ScannedImportDeclaration[]
.
interface ScannedImportDeclarationBase<T> {
filePath: string;
position: {
start: number;
end: number;
};
line: {
start: number;
end: number;
};
statement: string;
moduleSpecifierValue: string;
details: T;
}
type ScannedDefaultImportDeclaration = ScannedImportDeclarationBase<{
type: "default_import";
isTypeOnly: boolean;
importedBinding: string;
}>;
type ScannedNamespaceImportDeclaration = ScannedImportDeclarationBase<{
type: "namespace_import";
isTypeOnly: boolean;
importedBinding: string;
}>;
type ScannedNamedImportDeclaration = ScannedImportDeclarationBase<{
type: "named_imports";
elements: {
isTypeOnly: boolean;
importedBinding: string;
moduleExportName: string;
}[];
}>;
type ScannedSideEffectImportDeclaration = ScannedImportDeclarationBase<{
type: "side_effect_import";
}>;
type ScannedImportDeclaration =
| ScannedDefaultImportDeclaration
| ScannedNamespaceImportDeclaration
| ScannedNamedImportDeclaration
| ScannedSideEffectImportDeclaration;
Type: string
The file path to be scanned.
- @yuheiy/import-scanner-cli - CLI for this module