Skip to content

Commit 92dfc66

Browse files
committed
feat: use bun-types
To check only valid Bun modules, this commit uses `bun-types`. `bun-types` should be installed by the consumer because it's peerDependency.
1 parent 712971d commit 92dfc66

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

.projen/deps.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const project = new OpsBRTypeScriptProject({
44
defaultReleaseBranch: "main",
55
deps: ["debug", "eslint-import-resolver-typescript"],
66
devDeps: ["@opsbr/projen-typescript", "@types/debug"],
7+
peerDeps: ["bun-types"],
78
workflowPackageCache: true,
89
releaseToNpm: true,
910
});

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# replace this
1+
# eslint-import-resolver-typescript-bun
2+
3+
This plugin simply adds workarounds to [eslint-import-resolver-typescript](https://www.npmjs.com/package/eslint-import-resolver-typescript) when resolving [Bun](https://bun.sh/)'s modules.

package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1+
import { readFileSync } from "node:fs";
12
import debug from "debug";
23
import { resolve as resolveTs } from "eslint-import-resolver-typescript";
34

45
const IMPORTER_NAME = "eslint-import-resolver-typescript-bun";
56

67
const log = debug(IMPORTER_NAME);
78

9+
const findBunModules = () => {
10+
const { found, path } = resolveTs("bun-types", "bun-types");
11+
if (!found || !path) {
12+
log("bun-types not found.");
13+
return undefined;
14+
}
15+
// TODO: Better way to inspect .d.ts file.
16+
const bunTypesDefinition = readFileSync(path, "utf-8");
17+
const bunModules = new Set(
18+
bunTypesDefinition
19+
.split("\n")
20+
// Find `declare module "bun..." {`
21+
.filter((line) => line.includes('declare module "bun'))
22+
// Extract `bun...` parts only
23+
.map((line) => line.split('"')[1])
24+
);
25+
log("found bun modules:", bunModules);
26+
return bunModules;
27+
};
28+
const bunModules = findBunModules();
29+
830
export const interfaceVersion = 2;
931

1032
export const resolve: typeof resolveTs = function (source, file, config) {
11-
if (source.startsWith("bun:")) {
33+
if (bunModules?.has(source)) {
1234
log("matched bun modules:", source);
1335
return { found: true, path: null };
1436
}

test/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ test.each([
88
{ source: "fs", found: true, path: null },
99
{ source: "node:fs", found: true, path: null },
1010
{ source: "not_found", found: false, path: undefined },
11+
{ source: "bun", found: true, path: null },
1112
{ source: "bun:test", found: true, path: null },
13+
{ source: "bun:not_found", found: false, path: undefined },
1214
])(
1315
"resolve $source => found: $found, path: $path",
1416
({ source, found, path }) => {

yarn.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)