Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
yuheiy committed Sep 23, 2024
1 parent f829712 commit f4db642
Show file tree
Hide file tree
Showing 15 changed files with 698 additions and 1 deletion.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
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
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 1,2 @@
engine-strict=true
package-lock=false
7 changes: 7 additions & 0 deletions .vscode/settings.json
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"
}
142 changes: 141 additions & 1 deletion README.md
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
30 changes: 30 additions & 0 deletions biome.json
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"
}
}
}
32 changes: 32 additions & 0 deletions package.json
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
}
18 changes: 18 additions & 0 deletions src/index.ts
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 };
Loading

0 comments on commit f4db642

Please sign in to comment.