diff --git a/.changeset/purple-planes-grin.md b/.changeset/purple-planes-grin.md new file mode 100644 index 0000000..0630785 --- /dev/null +++ b/.changeset/purple-planes-grin.md @@ -0,0 +1,5 @@ +--- +'eslint-import-resolver-typescript': minor +--- + +feat: add bun built-in module support diff --git a/package.json b/package.json index 00e5f73..de9e92a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index e61246b..3ac064e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -117,6 +117,29 @@ let resolver: Resolver | undefined const digestHashObject = (value: object | null | undefined) => hashObject(value ?? {}).digest('hex') +let bunCoreModules: Set | undefined +const getBunCoreModules = (): Set => { + if (!bunCoreModules) { + bunCoreModules = new Set() + + 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' @@ -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) diff --git a/tests/bun/.eslintrc.cjs b/tests/bun/.eslintrc.cjs new file mode 100644 index 0000000..7497db6 --- /dev/null +++ b/tests/bun/.eslintrc.cjs @@ -0,0 +1 @@ +module.exports = require('../baseEslintConfig.cjs')(__dirname) diff --git a/tests/bun/index.ts b/tests/bun/index.ts new file mode 100644 index 0000000..331fb69 --- /dev/null +++ b/tests/bun/index.ts @@ -0,0 +1,5 @@ +/* eslint-disable */ +// @ts-expect-error +import { Database } from 'bun:sqlite' + +export default new Database() diff --git a/tests/bun/tsconfig.json b/tests/bun/tsconfig.json new file mode 100644 index 0000000..abdb5af --- /dev/null +++ b/tests/bun/tsconfig.json @@ -0,0 +1,4 @@ +{ + "compilerOptions": {}, + "files": ["index.ts"] +} diff --git a/tsconfig.json b/tsconfig.json index a303861..3d06dbd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,5 +4,6 @@ "module": "Node16", "outDir": "./lib" }, - "include": ["./src", "./shim.d.ts"] + "include": ["./src", "./shim.d.ts"], + "exclude": ["./node_modules/bun-types/**/*"] }