Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purple-planes-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-import-resolver-typescript': minor
---

feat: add bun built-in module support
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"prepare": "simple-git-hooks",
"release": "changeset publish",
"test": "run-p 'test:*'",
"test:bun": "eslint --ext ts,tsx tests/bun",
"test:multipleEslintrcs": "eslint --ext ts,tsx tests/multipleEslintrcs",
"test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs",
"test:withJsExtension": "node tests/withJsExtension/test.js && eslint --ext ts,tsx tests/withJsExtension",
Expand Down
33 changes: 33 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ let resolver: Resolver | undefined
const digestHashObject = (value: object | null | undefined) =>
hashObject(value ?? {}).digest('hex')

let bunCoreModules: Set<string> | undefined
const getBunCoreModules = (): Set<string> => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really seems hacking, can we ask @Jarred-Sumner for helping here.

Secondly, we can add a new module similar like https://www.npmjs.com/package/is-core-module.

if (!bunCoreModules) {
bunCoreModules = new Set<string>()

const { found, path } = resolve('bun-types', 'bun-types')
if (found && path) {
const regex = /declare module ["'](bun:\w+)["']/g
const content = fs.readFileSync(path, 'utf8')
let match: RegExpExecArray | null

while ((match = regex.exec(content)) !== null) {
// The first captured group is at index 1
if (match[1]) {
bunCoreModules.add(match[1])
}
}
}
}

return bunCoreModules
}

/**
* @param source the module to resolve; i.e './some-module'
* @param file the importing file's full path; i.e. '/usr/local/bin/file.js'
Expand Down Expand Up @@ -169,6 +192,16 @@ export function resolve(
}
}

// match against bun core modules
if (getBunCoreModules().has(source)) {
log('matched bun core:', source)

return {
found: true,
path: null,
}
}

initMappers(cachedOptions)

const mappedPath = getMappedPath(source, file, cachedOptions.extensions, true)
Expand Down
1 change: 1 addition & 0 deletions tests/bun/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../baseEslintConfig.cjs')(__dirname)
5 changes: 5 additions & 0 deletions tests/bun/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable */
// @ts-expect-error
import { Database } from 'bun:sqlite'

export default new Database()
4 changes: 4 additions & 0 deletions tests/bun/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"compilerOptions": {},
"files": ["index.ts"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"module": "Node16",
"outDir": "./lib"
},
"include": ["./src", "./shim.d.ts"]
"include": ["./src", "./shim.d.ts"],
"exclude": ["./node_modules/bun-types/**/*"]
}