Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bun built-in modules support #266

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


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

feat: add bun built-in module support
1 change: 1 addition & 0 deletions package.json
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
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
@@ -0,0 +1 @@
module.exports = require('../baseEslintConfig.cjs')(__dirname)
5 changes: 5 additions & 0 deletions tests/bun/index.ts
@@ -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
@@ -0,0 +1,4 @@
{
"compilerOptions": {},
"files": ["index.ts"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
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/**/*"]
}