diff --git a/.changeset/vite-config-oxc-parser.md b/.changeset/vite-config-oxc-parser.md new file mode 100644 index 0000000..4edcfaa --- /dev/null +++ b/.changeset/vite-config-oxc-parser.md @@ -0,0 +1,5 @@ +--- +"@stll/oxlint-config": patch +--- + +The Vite publish target resolver reads the declaration helper's config with oxc-parser instead of stripping TypeScript with esbuild and parsing with acorn, and the toolchain check reads declared TypeScript API importers the same way, so `esbuild` is no longer a direct dependency. A config that exports `defineConfig(...)` through a local constant is now recognized, and a value import of `vite-plugin-dts` or `unplugin-dts` beside the helper fails even when unused. diff --git a/bun.lock b/bun.lock index e796233..96933e1 100644 --- a/bun.lock +++ b/bun.lock @@ -39,7 +39,7 @@ "@changesets/read": "1.0.1", "@manypkg/get-packages": "3.1.0", "acorn": "8.19.0", - "esbuild": "0.28.2", + "oxc-parser": "0.155.0", "picomatch": "4.0.7", "semver": "7.8.6", "smol-toml": "1.9.1", @@ -82,7 +82,7 @@ "name": "@stll/test-kit", "version": "0.4.3", "dependencies": { - "@stll/time": "0.4.2", + "@stll/time": "0.4.3", "better-result": "3.0.1", }, "devDependencies": { @@ -235,7 +235,45 @@ "@napi-rs/wasm-runtime": ["@napi-rs/wasm-runtime@1.2.6", "", { "dependencies": { "@tybys/wasm-util": "^0.10.3" }, "peerDependencies": { "@emnapi/core": "^1.7.1 || 2.0.0-beta.2", "@emnapi/runtime": "^1.7.1 || 2.0.0-beta.2" } }, "sha512-mRyDmEw9/fCDXCGfL1sbgIw/GUK0PVxRW4YwgPN61F3LQ+fSyoBFarJVzgGyhF6kRzEQZ0FejDqm4WLXSeqQmw=="], - "@oxc-project/types": ["@oxc-project/types@0.157.0", "", {}, "sha512-95iTox1qdt6WGWce3xwNOVayByOfLaYAq+qGPpub6NWbBh9sv0GG3+zyfh9ZBCO/jVu5A0EWI+qQI7g6fevjxg=="], + "@oxc-parser/binding-android-arm-eabi": ["@oxc-parser/binding-android-arm-eabi@0.155.0", "", { "os": "android", "cpu": "arm" }, "sha512-A6xv7hDtnTm+xlZ7utn6rRQpRMJHRhVr3gjRGqlx0RUX84mNk0pAsy6YN/wWhuOTkaZhRrrzaLxf0NfbH77j2w=="], + + "@oxc-parser/binding-android-arm64": ["@oxc-parser/binding-android-arm64@0.155.0", "", { "os": "android", "cpu": "arm64" }, "sha512-T1wWwH9/JI5g7Z4kEQwDOwhToxdDhRyOTklH+uuNpJ16m3MZFmGl+l2rKc0NHQdbVRG2LTOBsNjpW3h1BWytPA=="], + + "@oxc-parser/binding-darwin-arm64": ["@oxc-parser/binding-darwin-arm64@0.155.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-ZoRB3qJxJ8DWA41jHja+YeB0lLfjBy4T+Kr15vnN1k65X3zbJt3rPZftEwN4Ju4tmA4K/T9dWWD7Z8ewQLnrVA=="], + + "@oxc-parser/binding-darwin-x64": ["@oxc-parser/binding-darwin-x64@0.155.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-vxtL86BtM+IgrpejA86nbTk5j0HKTIMeuaJ1fQciw6p9RfavPtIPjI7gN34owap0yG9txXBaAWpGdxokNwdqIw=="], + + "@oxc-parser/binding-freebsd-x64": ["@oxc-parser/binding-freebsd-x64@0.155.0", "", { "os": "freebsd", "cpu": "x64" }, "sha512-tN6Ru5e0UVnWaIuiH8jEwnvxuxg7FvhWwSd3oe5oDhOD4ObsSr9/N6WfHLjilUWlPPj65ptwrSDH0Y7lzwTzmw=="], + + "@oxc-parser/binding-linux-arm-gnueabihf": ["@oxc-parser/binding-linux-arm-gnueabihf@0.155.0", "", { "os": "linux", "cpu": "arm" }, "sha512-h4ozA5n6L6lwolsz9iwunEhKWnIVbmFRots5QxEix0H6nJ5ICpuDa7PuShuhfpIDFe3+Qvyscioyiv+9g+4Syg=="], + + "@oxc-parser/binding-linux-arm-musleabihf": ["@oxc-parser/binding-linux-arm-musleabihf@0.155.0", "", { "os": "linux", "cpu": "arm" }, "sha512-058/+LardpfuUlsKIvBGk/CYoakEVcMgqYx8GgyUZeOxFWTojYLlCc1Gb60/y21v3esDhHidgiY/Zbq9LpBhaQ=="], + + "@oxc-parser/binding-linux-arm64-gnu": ["@oxc-parser/binding-linux-arm64-gnu@0.155.0", "", { "os": "linux", "cpu": "arm64", "libc": "glibc" }, "sha512-nrOt2Y3xIAzu+lSPzH0864ieO+iCCXgoo9qFlGANE8Xh0cQ+RP8Wstfp+/wXN2YCyb98cAGzX0VruKYw7it/jw=="], + + "@oxc-parser/binding-linux-arm64-musl": ["@oxc-parser/binding-linux-arm64-musl@0.155.0", "", { "os": "linux", "cpu": "arm64", "libc": "musl" }, "sha512-KYAayyN5hQbJPtGKtRxlyc8XYmyntOplyXSQzTDrmbz7l/GCrzQbSswvyJFBTH1LGtsZrbXOAfN52i/Kz6kLKg=="], + + "@oxc-parser/binding-linux-ppc64-gnu": ["@oxc-parser/binding-linux-ppc64-gnu@0.155.0", "", { "os": "linux", "cpu": "ppc64", "libc": "glibc" }, "sha512-CSuSY5hfjtsBnHXydOj8jiNfS1qHbdPZuPz1YL1cSgK7VmNYP+QlZIbG0BBYYBgX/vYEgZwbG9mBdW4YairZ5w=="], + + "@oxc-parser/binding-linux-riscv64-gnu": ["@oxc-parser/binding-linux-riscv64-gnu@0.155.0", "", { "os": "linux", "cpu": "none", "libc": "glibc" }, "sha512-7KCfOQm+Nz2N8trAXbQkal4TQ5756WVEmhm35y816YHvyFnEfVsICsiV5k0pEszNf/fRm4cm2i6i18SJ6/iCWg=="], + + "@oxc-parser/binding-linux-riscv64-musl": ["@oxc-parser/binding-linux-riscv64-musl@0.155.0", "", { "os": "linux", "cpu": "none", "libc": "musl" }, "sha512-JSlhgoK+/g1TGJI5NuADx2+6syhxnQNLCWLXyO5QLFyetomcr/vIxSdiNlmyAv963YXUzCuwAwRG6npZ5xyJ1A=="], + + "@oxc-parser/binding-linux-s390x-gnu": ["@oxc-parser/binding-linux-s390x-gnu@0.155.0", "", { "os": "linux", "cpu": "s390x", "libc": "glibc" }, "sha512-WFIX+dPokWp64mi434Md3U3lJaTwNxAphvGjpltoyQjGUsdXavOIRrnp9dOS34Hd75HOAwDxAI+YwWOwqmJbtg=="], + + "@oxc-parser/binding-linux-x64-gnu": ["@oxc-parser/binding-linux-x64-gnu@0.155.0", "", { "os": "linux", "cpu": "x64", "libc": "glibc" }, "sha512-g6xSDC/JpbnVOz5uafMGyK3txBVRFMQTx8opv/yHXGxqV3ZMVl3bf7W8XrSnF68dmy3gM4dgReNryuE53+WkeQ=="], + + "@oxc-parser/binding-linux-x64-musl": ["@oxc-parser/binding-linux-x64-musl@0.155.0", "", { "os": "linux", "cpu": "x64", "libc": "musl" }, "sha512-6/4xlxKlZJWrQ0uwDFPc0H9+xLu7068CKhX3vIDW3SbvP/lVgnVuT5v6dyugeWskWpuxqF/WJ4WKwhxjU3pG2w=="], + + "@oxc-parser/binding-openharmony-arm64": ["@oxc-parser/binding-openharmony-arm64@0.155.0", "", { "os": "none", "cpu": "arm64" }, "sha512-tltklL0J0XjDxIRTBXWOBhvc2mitcmES45pIxhwDWfxRyqz7Vkeu0NekTZEIxGE4f/lXgq3vZcjjffteQOVorg=="], + + "@oxc-parser/binding-win32-arm64-msvc": ["@oxc-parser/binding-win32-arm64-msvc@0.155.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-alAb9TzjatUEsOeoncAziUnfdTDxfyswhalFZ9sC6plUgATIUx2NIZ0+bMn6aJ28AduS1OpG/OAisr/YISn4Ig=="], + + "@oxc-parser/binding-win32-ia32-msvc": ["@oxc-parser/binding-win32-ia32-msvc@0.155.0", "", { "os": "win32", "cpu": "ia32" }, "sha512-yc5cbSfcGmRv/KpxDFuyk8adEBaWd/9HilWvBbQ4bFbus1V2PUh2mGB2Cfq9rqZTqLeiMT8x0skV+LKp9hOMgQ=="], + + "@oxc-parser/binding-win32-x64-msvc": ["@oxc-parser/binding-win32-x64-msvc@0.155.0", "", { "os": "win32", "cpu": "x64" }, "sha512-8heq6jGq+rfpTDRDjoQiBHw0maQZf3IXkS99pdHQZPoTL5kyF5qyPS0RidUlSdnWDIqxXb5nux7GBD9cT+imUw=="], + + "@oxc-project/types": ["@oxc-project/types@0.155.0", "", {}, "sha512-SeE8JPwW1xs1tHeNycjUtUBdhorRa8o4jlvjwk49R7/Z291i+4DQa7JJIgOm/TaYQZx+QrPQbVJ/9c8dMIZN8Q=="], "@oxlint-tsgolint/darwin-arm64": ["@oxlint-tsgolint/darwin-arm64@7.0.2006", "", { "os": "darwin", "cpu": "arm64" }, "sha512-uc0MEHXZUDW23Zmo5X28eSNeSnP17jKnh9GiehpVZqhfy99ks8spyXT5Llar3tjDN7ppnQN0Qd9FK6WNkdD+fQ=="], @@ -555,6 +593,8 @@ "obug": ["obug@3.0.0", "", {}, "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw=="], + "oxc-parser": ["oxc-parser@0.155.0", "", { "dependencies": { "@oxc-project/types": "^0.155.0" }, "optionalDependencies": { "@oxc-parser/binding-android-arm-eabi": "0.155.0", "@oxc-parser/binding-android-arm64": "0.155.0", "@oxc-parser/binding-darwin-arm64": "0.155.0", "@oxc-parser/binding-darwin-x64": "0.155.0", "@oxc-parser/binding-freebsd-x64": "0.155.0", "@oxc-parser/binding-linux-arm-gnueabihf": "0.155.0", "@oxc-parser/binding-linux-arm-musleabihf": "0.155.0", "@oxc-parser/binding-linux-arm64-gnu": "0.155.0", "@oxc-parser/binding-linux-arm64-musl": "0.155.0", "@oxc-parser/binding-linux-ppc64-gnu": "0.155.0", "@oxc-parser/binding-linux-riscv64-gnu": "0.155.0", "@oxc-parser/binding-linux-riscv64-musl": "0.155.0", "@oxc-parser/binding-linux-s390x-gnu": "0.155.0", "@oxc-parser/binding-linux-x64-gnu": "0.155.0", "@oxc-parser/binding-linux-x64-musl": "0.155.0", "@oxc-parser/binding-openharmony-arm64": "0.155.0", "@oxc-parser/binding-win32-arm64-msvc": "0.155.0", "@oxc-parser/binding-win32-ia32-msvc": "0.155.0", "@oxc-parser/binding-win32-x64-msvc": "0.155.0" } }, "sha512-C/ReSf1JUFukK/zvk6jhcNibSssvas5+ktIzK7fbKVd3CedWe59C3QwHEOrEZj/4Rt9+xg0Jeis/Jv1F/KM1mw=="], + "oxlint": ["oxlint@1.93.0", "", { "optionalDependencies": { "@oxlint/binding-android-arm-eabi": "1.93.0", "@oxlint/binding-android-arm64": "1.93.0", "@oxlint/binding-darwin-arm64": "1.93.0", "@oxlint/binding-darwin-x64": "1.93.0", "@oxlint/binding-freebsd-x64": "1.93.0", "@oxlint/binding-linux-arm-gnueabihf": "1.93.0", "@oxlint/binding-linux-arm-musleabihf": "1.93.0", "@oxlint/binding-linux-arm64-gnu": "1.93.0", "@oxlint/binding-linux-arm64-musl": "1.93.0", "@oxlint/binding-linux-ppc64-gnu": "1.93.0", "@oxlint/binding-linux-riscv64-gnu": "1.93.0", "@oxlint/binding-linux-riscv64-musl": "1.93.0", "@oxlint/binding-linux-s390x-gnu": "1.93.0", "@oxlint/binding-linux-x64-gnu": "1.93.0", "@oxlint/binding-linux-x64-musl": "1.93.0", "@oxlint/binding-openharmony-arm64": "1.93.0", "@oxlint/binding-win32-arm64-msvc": "1.93.0", "@oxlint/binding-win32-ia32-msvc": "1.93.0", "@oxlint/binding-win32-x64-msvc": "1.93.0" }, "peerDependencies": { "oxlint-tsgolint": ">=7.0.2005", "vite-plus": "*" }, "optionalPeers": ["oxlint-tsgolint", "vite-plus"], "bin": { "oxlint": "bin/oxlint" } }, "sha512-19JMsQFX+YvEbcUTFQxjTwhvs0Ibg0D6CFneSLgpwnACWcY/6LjhpGpkPodh0T5aIT1pjMTLlYaeBFzaVgugTA=="], "oxlint-tsgolint": ["oxlint-tsgolint@7.0.2006", "", { "optionalDependencies": { "@oxlint-tsgolint/darwin-arm64": "7.0.2006", "@oxlint-tsgolint/darwin-x64": "7.0.2006", "@oxlint-tsgolint/linux-arm64": "7.0.2006", "@oxlint-tsgolint/linux-x64": "7.0.2006", "@oxlint-tsgolint/win32-arm64": "7.0.2006", "@oxlint-tsgolint/win32-x64": "7.0.2006" }, "bin": { "tsgolint": "./bin/tsgolint.js" } }, "sha512-5B3UtH71kycPHUC1kFEIffivEAs49SUmkJuHZOZnTY0NPj4legp0bcsCQucuLVNCoJzDucS68P2C4WIhP+LypQ=="], @@ -657,6 +697,8 @@ "mlly/pkg-types": ["pkg-types@1.3.1", "", { "dependencies": { "confbox": "^0.1.8", "mlly": "^1.7.4", "pathe": "^2.0.1" } }, "sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ=="], + "rolldown/@oxc-project/types": ["@oxc-project/types@0.157.0", "", {}, "sha512-95iTox1qdt6WGWce3xwNOVayByOfLaYAq+qGPpub6NWbBh9sv0GG3+zyfh9ZBCO/jVu5A0EWI+qQI7g6fevjxg=="], + "vite/rolldown": ["rolldown@1.2.18", "", { "dependencies": { "@oxc-project/types": "=0.156.0", "@rolldown/pluginutils": "^1.0.0" }, "optionalDependencies": { "@rolldown/binding-android-arm-eabi": "1.2.18", "@rolldown/binding-android-arm64": "1.2.18", "@rolldown/binding-darwin-arm64": "1.2.18", "@rolldown/binding-darwin-x64": "1.2.18", "@rolldown/binding-freebsd-x64": "1.2.18", "@rolldown/binding-linux-arm-gnueabihf": "1.2.18", "@rolldown/binding-linux-arm64-gnu": "1.2.18", "@rolldown/binding-linux-arm64-musl": "1.2.18", "@rolldown/binding-linux-ppc64-gnu": "1.2.18", "@rolldown/binding-linux-s390x-gnu": "1.2.18", "@rolldown/binding-linux-x64-gnu": "1.2.18", "@rolldown/binding-linux-x64-musl": "1.2.18", "@rolldown/binding-openharmony-arm64": "1.2.18", "@rolldown/binding-win32-arm64-msvc": "1.2.18", "@rolldown/binding-win32-x64-msvc": "1.2.18" }, "bin": { "rolldown": "./bin/cli.mjs" } }, "sha512-C1uuT3UR60SShuCaPQFAAZyefJCWZlAKB02BV3BuPE2Rs4fevRQltEKoFO2tV5W9/eJhU5RieV6vJj3euulfNQ=="], "mlly/pkg-types/confbox": ["confbox@0.1.8", "", {}, "sha512-RMtmw0iFkeR4YV+fUOSucriAQNb9g8zFR52MWCtl+cCZOFRNL6zeB395vPzFhEjjn4fMxXudmELnl/KF/WrK6w=="], diff --git a/packages/oxlint-config/README.md b/packages/oxlint-config/README.md index 8689478..c0ea2c8 100644 --- a/packages/oxlint-config/README.md +++ b/packages/oxlint-config/README.md @@ -865,6 +865,8 @@ This adapter requires consumer-installed `vite-plugin-dts` 5.1.2 and its Unsupported versions fail explicitly. The resolver proves a direct helper import and call in a literal plugin array; raw declaration plugins, opaque plugin arrays, and additional emitters fail. +It reads the config with oxc-parser and ignores TypeScript-only syntax, so a +TypeScript config needs no compiler; any value import of a raw emitter fails. Options are closed and static: include/exclude, entryRoot, pathsToAliases, compilerOptions.declarationMap=false, strictOutput=true, and declarationOnly=false. Hooks may emit only `.d.ts`, `.d.mts`, `.d.cts`, and their maps. Bundle and on-disk non-declaration @@ -886,8 +888,8 @@ TypeScript import on the package's minimum supported Node release. Packed-artifact checks run nightly, while the static contract check runs per PR. Every packed `.js`, `.mjs`, and `.cjs` file is checked, including non-entry chunks and executables. The actual packed manifest must match the committed contract. -Acorn 8.15.0 applies the strictest declared ECMAScript target (`es5`, `es2015` -through `es2025`); the provisioned consumer Node also parses every file with +Acorn applies the strictest declared ECMAScript target (`es5`, `es2015` +through `es2025`; oxc-parser has no per-year grammar); the provisioned consumer Node also parses every file with `--check`, without executing artifact code. `esnext` and supported Node targets use the native consumer Node bound. Node target identifiers follow the same grammar and compatibility rule as the static contract; no older Node-to-ECMAScript mapping is inferred. Browser targets diff --git a/packages/oxlint-config/package.json b/packages/oxlint-config/package.json index 3c91c0e..b1b61ba 100644 --- a/packages/oxlint-config/package.json +++ b/packages/oxlint-config/package.json @@ -134,7 +134,7 @@ "@changesets/read": "1.0.1", "@manypkg/get-packages": "3.1.0", "acorn": "8.19.0", - "esbuild": "0.28.2", + "oxc-parser": "0.155.0", "picomatch": "4.0.7", "semver": "7.8.6", "smol-toml": "1.9.1", diff --git a/packages/oxlint-config/src/entry-graph.test.ts b/packages/oxlint-config/src/entry-graph.test.ts index 4743788..f20fdcc 100644 --- a/packages/oxlint-config/src/entry-graph.test.ts +++ b/packages/oxlint-config/src/entry-graph.test.ts @@ -1,20 +1,17 @@ import { expect, test } from "bun:test"; -import { build } from "esbuild"; import { fileURLToPath } from "node:url"; import manifest from "../package.json"; const graphImports = async (entry: URL) => { - const result = await build({ - entryPoints: [fileURLToPath(entry)], - bundle: true, + const result = await Bun.build({ + entrypoints: [fileURLToPath(entry)], packages: "external", - platform: "node", - write: false, + target: "node", metafile: true, - treeShaking: false, - logLevel: "silent", }); + if (result.metafile === undefined) + throw new Error("Bun.build returned no metafile for the entry graph"); return new Set( Object.values(result.metafile.inputs).flatMap(({ imports }) => imports.map(({ path }) => path), diff --git a/packages/oxlint-config/src/publish-build-target-vite.test.ts b/packages/oxlint-config/src/publish-build-target-vite.test.ts index 3f59167..43b0fd7 100644 --- a/packages/oxlint-config/src/publish-build-target-vite.test.ts +++ b/packages/oxlint-config/src/publish-build-target-vite.test.ts @@ -56,6 +56,65 @@ export default defineConfig({ plugins: [declarations({ directory: import.meta.di ).toThrow(); }); +test("declaration helper configuration reads TypeScript syntax without a type-stripping compiler", () => { + const source = `import type { UserConfig } from 'vite'; +import { type PluginOption, defineConfig } from 'vite'; +import { declarationOnlyDts } from '@stll/oxlint-config/declaration-only-dts'; +import type { PluginOptions } from 'vite-plugin-dts'; +interface Extra { value: string } +declare const injected: PluginOption; +const config: UserConfig = defineConfig({ + plugins: [declarationOnlyDts({ directory: import.meta.dirname!, include: ['src/**/*'] as string[] })], +} satisfies UserConfig); +type Helper = typeof declarationOnlyDts; +class Runner { run(declarationOnlyDts: string): void; run(value: string): void {} } +export default config;`; + for (const loader of ["ts", "js"] as const) { + const input = + loader === "ts" + ? source + : `import { defineConfig } from 'vite'; +import { declarationOnlyDts } from '@stll/oxlint-config/declaration-only-dts'; +const config = defineConfig({ plugins: [declarationOnlyDts({ directory: import.meta.dirname, include: ['src/**/*'] })] }); +export default config;`; + expect( + reviewedDtsConfiguration({ + source: input, + directory: "/package", + loader, + }), + ).toEqual({ include: ["src/**/*"] }); + } + expect( + reviewedDtsConfiguration({ + source: source + .replace("const config: UserConfig", "export const config: UserConfig") + .replace("export default config;", "export { config as default };"), + directory: "/package", + loader: "ts", + }), + ).toEqual({ include: ["src/**/*"] }); + for (const [mutation, message] of [ + [ + source.replace("import type { PluginOptions }", "import raw"), + "Raw declaration emitters cannot accompany the reviewed helper", + ], + [ + source + .replace("plugins: [", "plugins: [[") + .replace("as string[] })]", "as string[] })]]"), + "Declare exactly one direct declaration helper call", + ], + ] as const) + expect(() => + reviewedDtsConfiguration({ + source: mutation, + directory: "/package", + loader: "ts", + }), + ).toThrow(message); +}); + test("Vite native callbacks are version-profile-bound at their exact option paths", () => { const callback = () => undefined; for (const [name, field] of [ diff --git a/packages/oxlint-config/src/publish-build-target-vite.ts b/packages/oxlint-config/src/publish-build-target-vite.ts index d314764..b9d687e 100644 --- a/packages/oxlint-config/src/publish-build-target-vite.ts +++ b/packages/oxlint-config/src/publish-build-target-vite.ts @@ -1,9 +1,8 @@ -import { parse } from "acorn"; -import { transformSync } from "esbuild"; import { readFileSync, realpathSync } from "node:fs"; import { createRequire } from "node:module"; import path from "node:path"; import { pathToFileURL } from "node:url"; +import { parseSync } from "oxc-parser"; import { reviewedViteDtsOptions } from "./publish-build-target-vite-dts"; import { declarationOnlyDts } from "./publish-build-target-vite-dts-helper"; @@ -46,6 +45,74 @@ const walkAst = ({ value, visit, parent }: WalkAstOptions) => { if (typeof entry === "object" && entry !== null) walkAst({ value: entry, visit, parent: value }); }; +/** TypeScript wrappers whose runtime value is their operand. */ +const typeExpressionWrappers: ReadonlySet = new Set([ + "TSAsExpression", + "TSSatisfiesExpression", + "TSNonNullExpression", + "TSTypeAssertion", + "TSInstantiationExpression", +]); +/** Annotation fields that type stripping removes from otherwise JavaScript nodes. */ +const typeFields: ReadonlySet = new Set([ + "typeAnnotation", + "returnType", + "typeParameters", + "typeArguments", + "superTypeArguments", + "implements", +]); +/** Members and statements with no runtime counterpart. */ +const typeDeclarations: ReadonlySet = new Set([ + "TSTypeAliasDeclaration", + "TSInterfaceDeclaration", + "TSDeclareFunction", + "TSIndexSignature", + "TSAbstractMethodDefinition", + "TSAbstractPropertyDefinition", + "TSAbstractAccessorProperty", +]); +const typeOnly = (value: unknown): boolean => { + if (!record(value)) return false; + // An overload signature is a method whose value has no body. + if ( + value["type"] === "MethodDefinition" && + record(value["value"]) && + value["value"]["type"] === "TSEmptyBodyFunctionExpression" + ) + return true; + if ( + typeDeclarations.has(value["type"]) || + value["declare"] === true || + value["importKind"] === "type" || + value["exportKind"] === "type" + ) + return true; + if ( + value["type"] === "ExportNamedDeclaration" || + value["type"] === "ExportDefaultDeclaration" + ) + return typeOnly(value["declaration"]); + // Type stripping drops an import whose named bindings are all type-only. + return ( + value["type"] === "ImportDeclaration" && + array(value["specifiers"]) && + value["specifiers"].length > 0 && + value["specifiers"].every(typeOnly) + ); +}; +/** The JavaScript a type-stripping compiler would leave, as the plain ESTree the walk below reads. */ +const javascriptView = (value: unknown): unknown => { + if (array(value)) + return value.filter((entry) => !typeOnly(entry)).map(javascriptView); + if (!record(value)) return value; + if (typeExpressionWrappers.has(value["type"])) + return javascriptView(value["expression"]); + const result: Record = {}; + for (const [key, entry] of Object.entries(value)) + if (!typeFields.has(key)) result[key] = javascriptView(entry); + return result; +}; const astName = (value: unknown) => record(value) && value["type"] === "Identifier" ? value["name"] : undefined; const astLiteral = (value: unknown): unknown => { @@ -97,10 +164,16 @@ export const reviewedDtsConfiguration = ({ directory, loader, }: ReviewedDtsConfigurationOptions) => { - const ast: unknown = parse( - transformSync(source, { loader, format: "esm", target: "esnext" }).code, - { ecmaVersion: "latest", sourceType: "module" }, - ); + const { program, errors } = parseSync(`vite.config.${loader}`, source, { + lang: loader, + astType: loader, + sourceType: "module", + preserveParens: false, + }); + const error = errors.at(0); + if (error !== undefined) + throw new SyntaxError(`Invalid Vite configuration: ${error.message}`); + const ast = javascriptView(program); if (!record(ast) || !array(ast["body"])) throw new Error("Invalid Vite configuration AST"); const bindings = new Set(); @@ -143,11 +216,17 @@ export const reviewedDtsConfiguration = ({ defineConfigBindings.add(local); } } + // `export const config = ...` keeps its declaration inside the export. + const variables = + statement["type"] === "ExportNamedDeclaration" + ? statement["declaration"] + : statement; if ( - statement["type"] === "VariableDeclaration" && - array(statement["declarations"]) + record(variables) && + variables["type"] === "VariableDeclaration" && + array(variables["declarations"]) ) - for (const declaration of statement["declarations"]) + for (const declaration of variables["declarations"]) if (record(declaration)) initializers.set(astName(declaration["id"]), declaration["init"]); if (statement["type"] === "ExportDefaultDeclaration") diff --git a/packages/oxlint-config/src/toolchain-node-projects.test.ts b/packages/oxlint-config/src/toolchain-node-projects.test.ts index 1ff989e..3973985 100644 --- a/packages/oxlint-config/src/toolchain-node-projects.test.ts +++ b/packages/oxlint-config/src/toolchain-node-projects.test.ts @@ -1026,9 +1026,17 @@ test("declared inventory runtimes load real configs independently of the checker naming: "checker.mjs", target: "node", // tsrs resolves at run time, from the installation the checker runs in. - external: ["tsrs", "tsrs/*"], + // oxc-parser loads a native binding, so it stays a dependency of the + // checker, as in the published package. + external: ["tsrs", "tsrs/*", "oxc-parser"], }); expect(build.success).toBe(true); + mkdirSync(path.join(bundleRoot, "node_modules")); + symlinkSync( + path.join(import.meta.dir, "../node_modules/oxc-parser"), + path.join(bundleRoot, "node_modules/oxc-parser"), + "dir", + ); withInventoryFixture((root, trackedFiles) => { // The bundle sits outside the package, so tsrs resolves from the // repository: link the package's own installed tsrs. diff --git a/packages/oxlint-config/src/toolchain-typescript-allowlist.test.ts b/packages/oxlint-config/src/toolchain-typescript-allowlist.test.ts index 9d1dce1..c8f02f6 100644 --- a/packages/oxlint-config/src/toolchain-typescript-allowlist.test.ts +++ b/packages/oxlint-config/src/toolchain-typescript-allowlist.test.ts @@ -274,6 +274,7 @@ test("source API declarations bind exact installs to tracked value importers", ( 'import ts from "typescript";', 'import { type Program } from "typescript6";', "import {", + 'import ts from "typescript6"; console.log(ts.version); break;', ]) { expect( typescriptDiagnostics( diff --git a/packages/oxlint-config/src/toolchain-typescript-source-api.ts b/packages/oxlint-config/src/toolchain-typescript-source-api.ts index a72c3f3..df0328f 100644 --- a/packages/oxlint-config/src/toolchain-typescript-source-api.ts +++ b/packages/oxlint-config/src/toolchain-typescript-source-api.ts @@ -1,7 +1,6 @@ -import { parse } from "acorn"; -import { transformSync } from "esbuild"; import { lstatSync, readFileSync, realpathSync } from "node:fs"; import path from "node:path"; +import { parseSync } from "oxc-parser"; /** Read only explicitly declared, tracked regular source files in the checkout. */ type SourceApiInputOptions = { @@ -40,23 +39,27 @@ export const sourceApiImportProblem = ({ if (content === undefined) return "must be a tracked, readable regular source file without symlinks"; try { - const { code } = transformSync(content, { - sourcefile: source, - loader: source.endsWith("x") ? "tsx" : "ts", - format: "esm", - tsconfigRaw: { compilerOptions: { verbatimModuleSyntax: true } }, - logLevel: "silent", - }); - const program = parse(code, { - ecmaVersion: "latest", + const lang = source.endsWith("x") ? "tsx" : "ts"; + const { program, errors } = parseSync(source, content, { + lang, sourceType: "module", + // Early errors such as a top-level `break` come from the semantic pass. + showSemanticErrors: true, }); + if (errors.length > 0) + return "cannot parse the declared compiler API importer"; + // A value import keeps at least one binding that is not `type`-only. if ( program.body.some( (node) => node.type === "ImportDeclaration" && + node.importKind !== "type" && node.source.value === name && - node.specifiers.length > 0, + node.specifiers.some( + (specifier) => + specifier.type !== "ImportSpecifier" || + specifier.importKind !== "type", + ), ) ) return undefined;