-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
698 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version-file: package.json | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
|
||
- name: Test | ||
run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
engine-strict=true | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.biome": "explicit" | ||
}, | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,141 @@ | ||
# import-analyzer | ||
# @yuheiy/import-scanner | ||
|
||
Scan files for import declarations based on specified module patterns. | ||
|
||
Using the TypeScript Compiler API, it scans files for import declarations and retrieves their information. | ||
|
||
## Install | ||
|
||
```sh | ||
npm install @yuheiy/import-scanner | ||
``` | ||
|
||
## Usage | ||
|
||
```ts | ||
// src/index.ts | ||
import MyModule from 'my-module'; | ||
import { a as A, b as B } from 'my-module/sub'; | ||
``` | ||
|
||
```ts | ||
import { scanImportDeclarations } from '@yuheiy/import-scanner'; | ||
import fg from 'fast-glob'; | ||
|
||
const importDeclarations = await scanImportDeclarations( | ||
['my-module', /^my-module\//], | ||
await fg('src/**/*.{js,ts,jsx,tsx}'), | ||
); | ||
|
||
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": 97 | ||
}, | ||
"line": { | ||
"start": 3, | ||
"end": 3 | ||
}, | ||
"statement": "import { a as A, b as B } from 'my-module/sub';", | ||
"moduleSpecifierValue": "my-module/sub", | ||
"details": { | ||
"type": "named_imports", | ||
"elements": [ | ||
{ | ||
"isTypeOnly": false, | ||
"importedBinding": "A", | ||
"moduleExportName": "a" | ||
}, | ||
{ | ||
"isTypeOnly": false, | ||
"importedBinding": "B", | ||
"moduleExportName": "b" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
*/ | ||
``` | ||
|
||
## API | ||
|
||
### scanImportDeclarations(modulePatterns, filePaths) | ||
|
||
Return a `Promise<ScannedImportDeclaration[]>`. | ||
|
||
```ts | ||
export type ScannedImportDeclaration = { | ||
filePath: string; | ||
position: { | ||
start: number; | ||
end: number; | ||
}; | ||
line: { | ||
start: number; | ||
end: number; | ||
}; | ||
statement: string; | ||
moduleSpecifierValue: string; | ||
details: | ||
| { | ||
type: 'default_import'; | ||
isTypeOnly: boolean; | ||
importedBinding: string; | ||
} | ||
| { | ||
type: 'namespace_import'; | ||
isTypeOnly: boolean; | ||
importedBinding: string; | ||
} | ||
| { | ||
type: 'named_imports'; | ||
elements: { | ||
isTypeOnly: boolean; | ||
importedBinding: string; | ||
moduleExportName: string; | ||
}[]; | ||
} | ||
| { | ||
type: 'side_effect_import'; | ||
}; | ||
}; | ||
``` | ||
|
||
#### modulePatterns | ||
|
||
Type: `string | RegExp | (string | RegExp)[]` | ||
|
||
The patterns of modules for which to retrieve import declarations. If it’s a string, an exact match is required; if it’s a regular expression, any matching items will be targeted. | ||
|
||
#### filePaths | ||
|
||
Type: `string[]` | ||
|
||
The file paths to be scanned. | ||
|
||
## Related | ||
|
||
- [@yuheiy/import-scanner-cli](https://github.com/yuheiy/import-scanner-cli) - CLI for this module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.2/schema.json", | ||
"vcs": { | ||
"enabled": false, | ||
"clientKind": "git", | ||
"useIgnoreFile": false | ||
}, | ||
"files": { | ||
"ignoreUnknown": false, | ||
"ignore": [] | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "tab" | ||
}, | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"quoteStyle": "single" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@yuheiy/import-scanner", | ||
"version": "0.0.0", | ||
"description": "Scan files for import declarations based on specified module patterns", | ||
"repository": "yuheiy/import-scanner", | ||
"license": "MIT", | ||
"author": "Yuhei Yasuda <[email protected]> (https://yuheiy.com/)", | ||
"type": "module", | ||
"exports": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"files": ["dist"], | ||
"scripts": { | ||
"build": "tsc --build", | ||
"check": "biome check --write", | ||
"test": "vitest" | ||
}, | ||
"dependencies": { | ||
"ts-morph": "^23.0.0" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.9.2", | ||
"@types/node": "^22.5.5", | ||
"typescript": "^5.6.2", | ||
"vitest": "^2.1.1" | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"sideEffect": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { type ScannedImportDeclaration, scanFile } from './scan-file.js'; | ||
|
||
export async function scanImportDeclarations( | ||
modulePatterns: string | RegExp | (string | RegExp)[], | ||
filePaths: string[], | ||
): Promise<ScannedImportDeclaration[]> { | ||
const modulePatterns_ = Array.isArray(modulePatterns) | ||
? modulePatterns | ||
: [modulePatterns]; | ||
|
||
const result = filePaths.map((filePath) => | ||
scanFile(modulePatterns_, filePath), | ||
); | ||
|
||
return result.flat(); | ||
} | ||
|
||
export type { ScannedImportDeclaration }; |
Oops, something went wrong.