From 43141cd72aec07ec7765a1648e27779eb467732d Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Wed, 22 Jul 2026 11:18:02 -0700 Subject: [PATCH] fix(ast): swcify --- .changeset/swc.md | 5 + package-lock.json | 400 +----------------- package.json | 2 +- .../metadata/utils/resolveTypes.mjs | 61 +-- src/utils/swc.mjs | 34 ++ 5 files changed, 81 insertions(+), 421 deletions(-) create mode 100644 .changeset/swc.md create mode 100644 src/utils/swc.mjs diff --git a/.changeset/swc.md b/.changeset/swc.md new file mode 100644 index 00000000..9b9a2bf2 --- /dev/null +++ b/.changeset/swc.md @@ -0,0 +1,5 @@ +--- +'@node-core/doc-kit': patch +--- + +Switches `oxc-parser` for `@swc/wasm`, since `oxc-parser` does not provide the needed bindings. diff --git a/package-lock.json b/package-lock.json index e0ce748d..d1d59bb8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@orama/ui": "^1.5.4", "@rollup/plugin-virtual": "^3.0.2", "@swc/html-wasm": "^1.15.43", + "@swc/wasm": "^1.15.46", "acorn": "^8.17.0", "commander": "^15.0.0", "dedent": "^1.7.2", @@ -27,7 +28,6 @@ "hastscript": "^9.0.1", "lightningcss-wasm": "^1.32.0", "mdast-util-slice-markdown": "^2.0.1", - "oxc-parser": "^0.139.0", "piscina": "^5.2.0", "preact": "^10.29.2", "preact-render-to-string": "^6.7.0", @@ -1593,352 +1593,6 @@ "@orama/core": "^1.2.13" } }, - "node_modules/@oxc-parser/binding-android-arm-eabi": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-android-arm-eabi/-/binding-android-arm-eabi-0.139.0.tgz", - "integrity": "sha512-22EsXTA3Vc7OvrF4bfT48PFln2UbxkVgrp/Tm32qLw76Dv7SmcInfClJe6yPYamnli6HiqasnESZ5ezN+X4ybg==", - "cpu": [ - "arm" - ], - "license": "MIT", - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-android-arm64": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-android-arm64/-/binding-android-arm64-0.139.0.tgz", - "integrity": "sha512-uASQkZV+CUQ6xXvoMzDQyfhd8OMusdv2FyCPfAi5ZDYptesapJlw7erhpGi1Lc+U8QjQV2JUrIh7d/3su2LzmQ==", - "cpu": [ - "arm64" - ], - "license": "MIT", - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-darwin-arm64": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-darwin-arm64/-/binding-darwin-arm64-0.139.0.tgz", - "integrity": "sha512-vuQOv2WF5pZCkdSPEExul98o853M3MCR24DpWsGrUoPJu5KcnGE34kLXY2yFwBwZwT+eN1x40Tt4q6ZzlEdNUA==", - "cpu": [ - "arm64" - ], - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-darwin-x64": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-darwin-x64/-/binding-darwin-x64-0.139.0.tgz", - "integrity": "sha512-zxZB8ChS+7XactZhcyxQ292P/DNiiBaPPKYiVUlb3RC0V/hme5bokNubjcmEmbW9uTfmqLTpveAdkbe66H3pGg==", - "cpu": [ - "x64" - ], - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-freebsd-x64": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-freebsd-x64/-/binding-freebsd-x64-0.139.0.tgz", - "integrity": "sha512-iw2MsoCPBQwdJqywRAGsF1jEAcGoZ+DeG2LVzt5pdUvRHqajsMF46BH0rHiWoWtMwcMSia+oQYga7fHo7u7Bcw==", - "cpu": [ - "x64" - ], - "license": "MIT", - "optional": true, - "os": [ - "freebsd" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-arm-gnueabihf": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-0.139.0.tgz", - "integrity": "sha512-SwjD/k3Y5bwGJWOH313VyaDrAuaYg1Pe+4AAXCgUKvSO5IHkj+mZ0oFcOWmB5nTuOM3uwdT+leL8h4OnFlI5hw==", - "cpu": [ - "arm" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-arm-musleabihf": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm-musleabihf/-/binding-linux-arm-musleabihf-0.139.0.tgz", - "integrity": "sha512-qbeygDkcvailKFhphb2YGMMXsaZIynHlPtoOrcx4NZB/tRbUKraCCMbw8dPeFtEKasfS2qWh1VnReY+Lcys1zA==", - "cpu": [ - "arm" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-arm64-gnu": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-0.139.0.tgz", - "integrity": "sha512-SrlL02KeImKlvx/9rCeopOLH7qKI3rqKKEguK6KBwVwEGLhV/A47dW4DNigf6/LFhrwoovaiayEQryjYAI86dQ==", - "cpu": [ - "arm64" - ], - "libc": [ - "glibc" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-arm64-musl": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm64-musl/-/binding-linux-arm64-musl-0.139.0.tgz", - "integrity": "sha512-u9e884ChAVRmIZ1jr/m46S96FoDQnruFjISLi4Y0i6Wu/JUUmIiw7+umLyXILJsPfUuqnN5BJLe23t07+Y6+IA==", - "cpu": [ - "arm64" - ], - "libc": [ - "musl" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-ppc64-gnu": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-0.139.0.tgz", - "integrity": "sha512-Z9tU2b3GJfAXOdirQmz4gZQkQkVjy53i77gf91l0733MQKa/qtk73KQQE2GzDtMqim+HyjpzvemmqzBtH2IJUA==", - "cpu": [ - "ppc64" - ], - "libc": [ - "glibc" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-riscv64-gnu": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-riscv64-gnu/-/binding-linux-riscv64-gnu-0.139.0.tgz", - "integrity": "sha512-RyUbr7hzPK84YDWKs77PRYk9VBwWbsbuYsQzQWiSmLnARXTg2zntLPGfCH/1wpfUYdmGkp/6SsqTXSsYdw9Jgw==", - "cpu": [ - "riscv64" - ], - "libc": [ - "glibc" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-riscv64-musl": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-riscv64-musl/-/binding-linux-riscv64-musl-0.139.0.tgz", - "integrity": "sha512-dcQhjtcDvtR8BgkUpt03Yz5SzxdzYvTigenIJOEsiSX6G2t6yybEMxGWjp0dOuYGror0BaqcZfTyXR58amCEig==", - "cpu": [ - "riscv64" - ], - "libc": [ - "musl" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-s390x-gnu": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-0.139.0.tgz", - "integrity": "sha512-iuGrxysV4rGUymdKpn7bgZQ6Vix8Bi/6D/rp71HYIzphq6NKrsBhsGOYsSZte+uBFL43tXh7Xr7TM72sGliJNA==", - "cpu": [ - "s390x" - ], - "libc": [ - "glibc" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-x64-gnu": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-x64-gnu/-/binding-linux-x64-gnu-0.139.0.tgz", - "integrity": "sha512-NxJdZZyaa2JLLvNfH/iJQXfCNfKcPMylwY4ObMpBVmW4Nq+RyUpVVQXrMMelcQ6rwx3nuhF1Iga8n+eoAKGCIA==", - "cpu": [ - "x64" - ], - "libc": [ - "glibc" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-linux-x64-musl": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-x64-musl/-/binding-linux-x64-musl-0.139.0.tgz", - "integrity": "sha512-ePxBvvtzISmSsJ0RIj8FNikSCn58i1jtccj7XR4U9Li4iSzhkFyYlnJ51cQTUwkairz8WMTD4SpKoot8RyTnQA==", - "cpu": [ - "x64" - ], - "libc": [ - "musl" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-openharmony-arm64": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-openharmony-arm64/-/binding-openharmony-arm64-0.139.0.tgz", - "integrity": "sha512-b/c2+mPXMOxG5x16n8yf9cjor/ntQQScmYnSmLEWIWJ4rfXd5dokMxx0kliSLA+YAGq6DD3K9BWi+aFXHiiV1w==", - "cpu": [ - "arm64" - ], - "license": "MIT", - "optional": true, - "os": [ - "openharmony" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-wasm32-wasi": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-wasm32-wasi/-/binding-wasm32-wasi-0.139.0.tgz", - "integrity": "sha512-rNU0pO+/CVPC2qZ+xtX5R2cOje9Q75q3UkfgwUCPlplqxMv6Iffd5tMPGVMCLGnPINxPlmi0WPsW94HltbYvfQ==", - "cpu": [ - "wasm32" - ], - "license": "MIT", - "optional": true, - "dependencies": { - "@emnapi/core": "1.11.1", - "@emnapi/runtime": "1.11.1", - "@napi-rs/wasm-runtime": "^1.1.6" - }, - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-win32-arm64-msvc": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-0.139.0.tgz", - "integrity": "sha512-JPEmfncZfqAFiGsAN6UZSMIBqnG8wn8buywRacFOWjP+9dZwcs03SsBp4tmyjM/4l/VoH/IuThrW1Jof3OyQUw==", - "cpu": [ - "arm64" - ], - "license": "MIT", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-win32-ia32-msvc": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-win32-ia32-msvc/-/binding-win32-ia32-msvc-0.139.0.tgz", - "integrity": "sha512-c06dWBwEnFHof5JN7T9/ts23uATXS9czPEBO60d4xnjH2Am29Bo7OGKdVHRmcWQnw0OBxlTg9fIEXAvtcwOBfw==", - "cpu": [ - "ia32" - ], - "license": "MIT", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, - "node_modules/@oxc-parser/binding-win32-x64-msvc": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-parser/binding-win32-x64-msvc/-/binding-win32-x64-msvc-0.139.0.tgz", - "integrity": "sha512-bI3/44urQjW/D495JPXe+c9d+4Q+ONi3DvDNnzwRZYTWHZ3hAlFdmirYK4mzhTfCZiAly02EbMYir7QGU+9O2Q==", - "cpu": [ - "x64" - ], - "license": "MIT", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": "^20.19.0 || >=22.12.0" - } - }, "node_modules/@oxc-project/types": { "version": "0.137.0", "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.137.0.tgz", @@ -3166,6 +2820,12 @@ "integrity": "sha512-tw1DpLmh54BDNPDBY/tHLDFcLnxzji242Fu/8Y8nrbSBdDndO0Lv56g869mRgrsmZBvsoS31JqgDkf6YYtvi3g==", "license": "Apache-2.0" }, + "node_modules/@swc/wasm": { + "version": "1.15.46", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.15.46.tgz", + "integrity": "sha512-XaHMuBVdMDZdi4h+V3471lo/R1aWefieDibevgiz7CFJPfAbmfqqQUPqpMYHx287+lrVWGeIbxS9swVEu8zruQ==", + "license": "Apache-2.0" + }, "node_modules/@tybys/wasm-util": { "version": "0.10.3", "resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.3.tgz", @@ -8678,52 +8338,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/oxc-parser": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/oxc-parser/-/oxc-parser-0.139.0.tgz", - "integrity": "sha512-cf1TKZN+zc0lwqigeyXKzKVk5+vNRe99Or2+wVJsXLdlhJgC+gsIniYDfj/ZEBzCJ8Xm21ZG6YtMbR262CcS2w==", - "license": "MIT", - "dependencies": { - "@oxc-project/types": "^0.139.0" - }, - "engines": { - "node": "^20.19.0 || >=22.12.0" - }, - "funding": { - "url": "https://github.com/sponsors/Boshen" - }, - "optionalDependencies": { - "@oxc-parser/binding-android-arm-eabi": "0.139.0", - "@oxc-parser/binding-android-arm64": "0.139.0", - "@oxc-parser/binding-darwin-arm64": "0.139.0", - "@oxc-parser/binding-darwin-x64": "0.139.0", - "@oxc-parser/binding-freebsd-x64": "0.139.0", - "@oxc-parser/binding-linux-arm-gnueabihf": "0.139.0", - "@oxc-parser/binding-linux-arm-musleabihf": "0.139.0", - "@oxc-parser/binding-linux-arm64-gnu": "0.139.0", - "@oxc-parser/binding-linux-arm64-musl": "0.139.0", - "@oxc-parser/binding-linux-ppc64-gnu": "0.139.0", - "@oxc-parser/binding-linux-riscv64-gnu": "0.139.0", - "@oxc-parser/binding-linux-riscv64-musl": "0.139.0", - "@oxc-parser/binding-linux-s390x-gnu": "0.139.0", - "@oxc-parser/binding-linux-x64-gnu": "0.139.0", - "@oxc-parser/binding-linux-x64-musl": "0.139.0", - "@oxc-parser/binding-openharmony-arm64": "0.139.0", - "@oxc-parser/binding-wasm32-wasi": "0.139.0", - "@oxc-parser/binding-win32-arm64-msvc": "0.139.0", - "@oxc-parser/binding-win32-ia32-msvc": "0.139.0", - "@oxc-parser/binding-win32-x64-msvc": "0.139.0" - } - }, - "node_modules/oxc-parser/node_modules/@oxc-project/types": { - "version": "0.139.0", - "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.139.0.tgz", - "integrity": "sha512-r9gHphtCs+1M7J0pw6Sn/hh/Wpa/iQrOOkrNAlVLF/gHq+/CJmHIWKKUUhdWjcD6CIa8idarspCsASiXCXvFUw==", - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/Boshen" - } - }, "node_modules/p-filter": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/p-filter/-/p-filter-2.1.0.tgz", diff --git a/package.json b/package.json index b043ceb6..a29c39e2 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "@orama/ui": "^1.5.4", "@rollup/plugin-virtual": "^3.0.2", "@swc/html-wasm": "^1.15.43", + "@swc/wasm": "^1.15.46", "acorn": "^8.17.0", "commander": "^15.0.0", "dedent": "^1.7.2", @@ -80,7 +81,6 @@ "hastscript": "^9.0.1", "lightningcss-wasm": "^1.32.0", "mdast-util-slice-markdown": "^2.0.1", - "oxc-parser": "^0.139.0", "piscina": "^5.2.0", "preact": "^10.29.2", "preact-render-to-string": "^6.7.0", diff --git a/src/generators/metadata/utils/resolveTypes.mjs b/src/generators/metadata/utils/resolveTypes.mjs index 86cf0d3c..00aba7de 100644 --- a/src/generators/metadata/utils/resolveTypes.mjs +++ b/src/generators/metadata/utils/resolveTypes.mjs @@ -1,23 +1,25 @@ -import { parseSync, Visitor } from 'oxc-parser'; +import { parseSync } from '@swc/wasm'; import { visit } from 'unist-util-visit'; import { lookupTypeName, resolveTypeReference } from './transformers.mjs'; import logger from '../../../logger/index.mjs'; - -const KEYWORDS = [ - 'TSStringKeyword', - 'TSNumberKeyword', - 'TSBooleanKeyword', - 'TSAnyKeyword', - 'TSUnknownKeyword', - 'TSNeverKeyword', - 'TSVoidKeyword', - 'TSUndefinedKeyword', - 'TSObjectKeyword', - 'TSBigIntKeyword', - 'TSSymbolKeyword', - 'TSThisType', -]; +import { walk } from '../../../utils/swc.mjs'; + +// `null` is a keyword kind in SWC, unlike most parsers which treat it as a literal type +const KEYWORDS = new Set([ + 'string', + 'number', + 'boolean', + 'any', + 'unknown', + 'never', + 'void', + 'undefined', + 'object', + 'bigint', + 'symbol', + 'null', +]); /** * Parses a wrapped `type $Ti = ;` statement @@ -36,19 +38,26 @@ const parseAliases = values => { }) .join(''); - const result = parseSync('types.ts', source); + let program; - if (result.errors.length > 0) { + try { + program = parseSync(source, { syntax: 'typescript' }); + } catch { return undefined; } + // SWC spans are absolute and keep growing across parse calls; the module's + // span begins at the first token, which is the start of our source + const spanOffset = program.span.start; + const identifiersPerAlias = values.map(() => []); /** * */ const record = node => { - const [start, end] = node.range ?? [node.start, node.end]; + const start = node.span.start - spanOffset; + const end = node.span.end - spanOffset; const index = Math.max( 0, @@ -66,28 +75,26 @@ const parseAliases = values => { }); }; - const visitor = new Visitor({ + walk(program, { /** * */ - TSTypeReference: node => record(node.typeName), // Promise, vm.Module + TsTypeReference: node => record(node.typeName), // Promise, vm.Module /** * */ - TSTypeQuery: node => record(node.exprName), // typeof Foo + TsTypeQuery: node => record(node.exprName), // typeof Foo /** * */ - TSImportType: node => node.qualifier && record(node.qualifier), // import('fs').Stats + TsImportType: node => node.qualifier && record(node.qualifier), // import('fs').Stats /** * */ - TSLiteralType: node => node.literal.type === 'NullLiteral' && record(node), - ...Object.fromEntries(KEYWORDS.map(kind => [kind, record])), + TsKeywordType: node => KEYWORDS.has(node.kind) && record(node), // string, null, ... + TsThisType: record, }); - visitor.visit(result.program); - return identifiersPerAlias.map(identifiers => ({ identifiers })); }; diff --git a/src/utils/swc.mjs b/src/utils/swc.mjs new file mode 100644 index 00000000..0a93d8bf --- /dev/null +++ b/src/utils/swc.mjs @@ -0,0 +1,34 @@ +'use strict'; + +// TODO(@avivkeller): Once oxc-parser supports WASM, or the +// bindings we need, remove this in favor of it. It's much +// faster. +// +// For now, however, this is our walker :-) + +/** + * @param {unknown} node + * @param {((node: { type: string }) => void) | Record void>} visitors + */ +export const walk = (node, visitors) => { + if (node === null || typeof node !== 'object') { + return; + } + + if (Array.isArray(node)) { + node.forEach(child => walk(child, visitors)); + return; + } + + if (typeof visitors === 'function') { + visitors(node); + } else { + visitors[node.type]?.(node); + } + + for (const [key, value] of Object.entries(node)) { + if (key !== 'span' && key !== 'loc') { + walk(value, visitors); + } + } +};