Skip to content

Commit

Permalink
feat: add alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Dec 18, 2023
1 parent e22086d commit 3b74c30
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn compute_alias(root: &Option<PathBuf>, alias: Option<Buffer>) -> Option<Al
Some(buf) => {
let alias_str = String::from_utf8_lossy(&buf).to_string();
let alias: Alias = alias_str
.trim()
.split(" ")
.map(|s| {
let kv: Vec<&str> = s.split(":").collect();
Expand Down
11 changes: 11 additions & 0 deletions packages/graph/__test__/alias.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'ava'
import {createParser, readParsedFile} from './utils.mjs'


test('should parse alias path', async (t) => {
const parser = createParser({'@nested': './nested'})

await parser.visit('./alias.js')

t.deepEqual(parser.parse(), readParsedFile('./alias.json'))
})
3 changes: 3 additions & 0 deletions packages/graph/__test__/fixture/code/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {read} from 'fs'
import {foo} from '@nested/foo'
export {bar} from '@nested/bar'
83 changes: 83 additions & 0 deletions packages/graph/__test__/fixture/parsed/alias.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"nested/foo.js": {
"id": "nested/foo.js",
"type": "internal",
"importer": [
"alias.js",
"nested/bar.js"
],
"import": null
},
"alias.js": {
"id": "alias.js",
"type": "internal",
"importer": null,
"import": [
{
"id": "fs",
"type": "static",
"ident": [
{
"name": "read",
"as": "read",
"isType": false
}
],
"typeOnly": false
},
{
"id": "nested/foo.js",
"type": "static",
"ident": [
{
"name": "foo",
"as": "foo",
"isType": false
}
],
"typeOnly": false
},
{
"id": "nested/bar.js",
"type": "static",
"ident": [
{
"name": "bar",
"as": "bar",
"isType": false
}
],
"typeOnly": false
}
]
},
"fs": {
"id": "fs",
"type": "builtin",
"importer": [
"alias.js"
],
"import": null
},
"nested/bar.js": {
"id": "nested/bar.js",
"type": "internal",
"importer": [
"alias.js"
],
"import": [
{
"id": "nested/foo.js",
"type": "static",
"ident": [
{
"name": "foo",
"as": "foo",
"isType": false
}
],
"typeOnly": false
}
]
}
}
3 changes: 2 additions & 1 deletion packages/graph/__test__/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const readParsedFile = (file, codeFiles = {}, nodeModules = {}, resolve =

return JSON.parse(content)
}
export const parser = new Parser({ root: join(process.cwd(), '__test__/fixture/code') })
export const createParser = (alias) => new Parser({ root: join(process.cwd(), '__test__/fixture/code'), alias })
export const parser = createParser()
export const getGitRepo = repo => join(__dirname, '../../../repos', repo)
export const getGitRepoFiles = (repo, pattern, ignore) => {
const root = getGitRepo(repo)
Expand Down

0 comments on commit 3b74c30

Please sign in to comment.