From 2fd904d623c873cc2b3311113ebcd23fdfe91990 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 28 Nov 2024 13:17:56 +0000 Subject: [PATCH] chore: migrate to `oxc-walker` --- packages/nuxt/package.json | 3 +- .../src/components/plugins/component-names.ts | 4 +-- .../nuxt/src/components/plugins/tree-shake.ts | 4 +-- .../nuxt/src/core/plugins/composable-keys.ts | 2 +- .../nuxt/src/core/plugins/plugin-metadata.ts | 3 +- packages/nuxt/src/core/plugins/prehydrate.ts | 2 +- packages/nuxt/src/core/utils/parse.ts | 35 ------------------- packages/nuxt/src/pages/plugins/page-meta.ts | 3 +- packages/nuxt/src/pages/route-rules.ts | 2 +- packages/nuxt/src/pages/utils.ts | 2 +- pnpm-lock.yaml | 18 ++++++---- 11 files changed, 23 insertions(+), 55 deletions(-) delete mode 100644 packages/nuxt/src/core/utils/parse.ts diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index 009f3fa32f..b933a62fa1 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -81,7 +81,6 @@ "errx": "^0.1.0", "esbuild": "^0.24.0", "escape-string-regexp": "^5.0.0", - "estree-walker": "^3.0.3", "globby": "^14.0.2", "h3": "npm:h3-nightly@2.0.0-1718872656.6765a6e", "hookable": "^5.5.3", @@ -99,6 +98,7 @@ "ofetch": "^1.4.1", "ohash": "^1.1.4", "oxc-parser": "^0.38.0", + "oxc-walker": "^0.0.1", "pathe": "^1.1.2", "perfect-debounce": "^1.0.0", "pkg-types": "^1.2.1", @@ -127,7 +127,6 @@ "devDependencies": { "@nuxt/scripts": "0.9.5", "@parcel/watcher": "2.5.0", - "@types/estree": "1.0.6", "@vitejs/plugin-vue": "5.2.1", "@vue/compiler-sfc": "3.5.13", "unbuild": "3.0.0-rc.11", diff --git a/packages/nuxt/src/components/plugins/component-names.ts b/packages/nuxt/src/components/plugins/component-names.ts index d7b6f10c7a..49ed4ebd05 100644 --- a/packages/nuxt/src/components/plugins/component-names.ts +++ b/packages/nuxt/src/components/plugins/component-names.ts @@ -1,7 +1,7 @@ import { createUnplugin } from 'unplugin' import MagicString from 'magic-string' import type { Component } from 'nuxt/schema' -import { parseAndWalk, withLocations } from '../../core/utils/parse' +import { parseAndWalk } from 'oxc-walker' import { SX_RE, isVue } from '../../core/utils' @@ -43,7 +43,7 @@ export const ComponentNamePlugin = (options: NameDevPluginOptions) => createUnpl return } - const { start, end } = withLocations(node.declaration) + const { start, end } = node.declaration s.overwrite(start, end, `Object.assign(${code.slice(start, end)}, { __name: ${JSON.stringify(component.pascalName)} })`) this.skip() }) diff --git a/packages/nuxt/src/components/plugins/tree-shake.ts b/packages/nuxt/src/components/plugins/tree-shake.ts index 1f6ce5fd48..de7568ed02 100644 --- a/packages/nuxt/src/components/plugins/tree-shake.ts +++ b/packages/nuxt/src/components/plugins/tree-shake.ts @@ -5,9 +5,9 @@ import type { BindingPattern, BindingProperty, BindingRestElement, CallExpressio import { createUnplugin } from 'unplugin' import type { Component } from '@nuxt/schema' import { resolve } from 'pathe' +import { parseAndWalk, walk } from 'oxc-walker' +import type { Node } from 'oxc-walker' -import { parseAndWalk, walk } from '../../core/utils/parse' -import type { Node } from '../../core/utils/parse' import { distDir } from '../../dirs' interface TreeShakeTemplatePluginOptions { diff --git a/packages/nuxt/src/core/plugins/composable-keys.ts b/packages/nuxt/src/core/plugins/composable-keys.ts index 2d3bedc0b9..fa67adfbd1 100644 --- a/packages/nuxt/src/core/plugins/composable-keys.ts +++ b/packages/nuxt/src/core/plugins/composable-keys.ts @@ -7,9 +7,9 @@ import { parseQuery, parseURL } from 'ufo' import escapeRE from 'escape-string-regexp' import { findStaticImports, parseStaticImport } from 'mlly' import type { BindingPattern } from 'oxc-parser' +import { parseAndWalk, walk } from 'oxc-walker' import { matchWithStringOrRegex } from '../utils/plugins' -import { parseAndWalk, walk } from '../utils/parse' interface ComposableKeysOptions { sourcemap: boolean diff --git a/packages/nuxt/src/core/plugins/plugin-metadata.ts b/packages/nuxt/src/core/plugins/plugin-metadata.ts index 437ea46d7c..ffe3626bdf 100644 --- a/packages/nuxt/src/core/plugins/plugin-metadata.ts +++ b/packages/nuxt/src/core/plugins/plugin-metadata.ts @@ -6,8 +6,7 @@ import { createUnplugin } from 'unplugin' import MagicString from 'magic-string' import { normalize } from 'pathe' import { logger } from '@nuxt/kit' - -import { parseAndWalk } from '../../core/utils/parse' +import { parseAndWalk } from 'oxc-walker' import type { ObjectPlugin, PluginMeta } from '#app' diff --git a/packages/nuxt/src/core/plugins/prehydrate.ts b/packages/nuxt/src/core/plugins/prehydrate.ts index d22e92747e..1dc8495846 100644 --- a/packages/nuxt/src/core/plugins/prehydrate.ts +++ b/packages/nuxt/src/core/plugins/prehydrate.ts @@ -2,8 +2,8 @@ import { transform } from 'esbuild' import { createUnplugin } from 'unplugin' import MagicString from 'magic-string' import { hash } from 'ohash' +import { parseAndWalk } from 'oxc-walker' -import { parseAndWalk } from '../../core/utils/parse' import { isJS, isVue } from '../utils' export function PrehydrateTransformPlugin (options: { sourcemap?: boolean } = {}) { diff --git a/packages/nuxt/src/core/utils/parse.ts b/packages/nuxt/src/core/utils/parse.ts deleted file mode 100644 index 9980c296c6..0000000000 --- a/packages/nuxt/src/core/utils/parse.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { parseSync } from 'oxc-parser' -import type { CatchClause, ClassBody, Declaration, Expression, MethodDefinition, ModuleDeclaration, ObjectProperty, Pattern, PrivateIdentifier, Program, PropertyDefinition, SpreadElement, Statement, Super, SwitchCase, TemplateElement } from 'oxc-parser' -import { walk as _walk } from 'estree-walker' -import type { SyncHandler } from 'estree-walker' -import type { Node as ESTreeNode, Program as ESTreeProgram, ModuleSpecifier } from 'estree' - -/** estree also has AssignmentProperty, Identifier and Literal as possible node types */ -export type Node = Declaration | Expression | ClassBody | CatchClause | MethodDefinition | ModuleDeclaration | ModuleSpecifier | Pattern | PrivateIdentifier | Program | SpreadElement | Statement | Super | SwitchCase | TemplateElement | ObjectProperty | PropertyDefinition - -type WalkerCallback = (this: ThisParameterType, node: Node, parent: Node | null, ctx: { key: string | number | symbol | null | undefined, index: number | null | undefined, ast: Program | Node }) => void - -export function walk (ast: Program | Node, callback: { enter?: WalkerCallback, leave?: WalkerCallback }) { - return _walk(ast as unknown as ESTreeProgram | ESTreeNode, { - enter (node, parent, key, index) { - callback.enter?.call(this, node as Node, parent as Node | null, { key, index, ast }) - }, - leave (node, parent, key, index) { - callback.leave?.call(this, node as Node, parent as Node | null, { key, index, ast }) - }, - }) -} - -export function parseAndWalk (code: string, sourceFilename: string, callback: WalkerCallback): Program -export function parseAndWalk (code: string, sourceFilename: string, object: { enter?: WalkerCallback, leave?: WalkerCallback }): Program -export function parseAndWalk (code: string, sourceFilename: string, callback: { enter?: WalkerCallback, leave?: WalkerCallback } | WalkerCallback) { - const ast = parseSync(code, { sourceType: 'module', sourceFilename }).program - walk(ast, typeof callback === 'function' ? { enter: callback } : callback) - return ast -} - -type WithLocations = T & { start: number, end: number } - -export function withLocations (node: T): WithLocations { - return node as WithLocations -} diff --git a/packages/nuxt/src/pages/plugins/page-meta.ts b/packages/nuxt/src/pages/plugins/page-meta.ts index d7bc22c20f..72a9397464 100644 --- a/packages/nuxt/src/pages/plugins/page-meta.ts +++ b/packages/nuxt/src/pages/plugins/page-meta.ts @@ -6,8 +6,7 @@ import { findExports, findStaticImports, parseStaticImport } from 'mlly' import MagicString from 'magic-string' import { isAbsolute } from 'pathe' import { logger } from '@nuxt/kit' - -import { parseAndWalk, walk } from '../../core/utils/parse' +import { parseAndWalk, walk } from 'oxc-walker' interface PageMetaPluginOptions { dev?: boolean diff --git a/packages/nuxt/src/pages/route-rules.ts b/packages/nuxt/src/pages/route-rules.ts index 1ae4b3620e..9ae579fc87 100644 --- a/packages/nuxt/src/pages/route-rules.ts +++ b/packages/nuxt/src/pages/route-rules.ts @@ -2,9 +2,9 @@ import { runInNewContext } from 'node:vm' import type { NuxtPage } from '@nuxt/schema' import type { NitroRouteConfig } from 'nitro/types' import { normalize } from 'pathe' +import { parseAndWalk } from 'oxc-walker' import { getLoader } from '../core/utils' -import { parseAndWalk } from '../core/utils/parse' import { extractScriptContent, pathToNitroGlob } from './utils' const ROUTE_RULE_RE = /\bdefineRouteRules\(/ diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index a204fc68b7..c25c97eae4 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -9,8 +9,8 @@ import { filename } from 'pathe/utils' import { hash } from 'ohash' import type { ObjectProperty } from 'oxc-parser' import type { NuxtPage } from 'nuxt/schema' +import { parseAndWalk } from 'oxc-walker' -import { parseAndWalk } from '../core/utils/parse' import { getLoader, uniqueBy } from '../core/utils' import { toArray } from '../utils' diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 06ff41e52b..c909dd19ba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -338,9 +338,6 @@ importers: escape-string-regexp: specifier: ^5.0.0 version: 5.0.0 - estree-walker: - specifier: ^3.0.3 - version: 3.0.3 globby: specifier: ^14.0.2 version: 14.0.2 @@ -392,6 +389,9 @@ importers: oxc-parser: specifier: ^0.38.0 version: 0.38.0 + oxc-walker: + specifier: ^0.0.1 + version: 0.0.1 pathe: specifier: ^1.1.2 version: 1.1.2 @@ -471,9 +471,6 @@ importers: '@parcel/watcher': specifier: 2.5.0 version: 2.5.0 - '@types/estree': - specifier: 1.0.6 - version: 1.0.6 '@vitejs/plugin-vue': specifier: 5.2.1 version: 5.2.1(vite@6.0.1(@types/node@22.10.1)(jiti@2.4.0)(sass@1.78.0)(terser@5.32.0)(tsx@4.19.1)(yaml@2.5.1))(vue@3.5.13(typescript@5.6.3)) @@ -5961,6 +5958,9 @@ packages: oxc-parser@0.38.0: resolution: {integrity: sha512-w/cUL64wDb72gaBoOnvodKgHhNF9pDjb2b8gPkDKJDTvIvlbcE9XGDT3cnXOP4N3XCMrRT4MC23bCHGb3gCFSQ==} + oxc-walker@0.0.1: + resolution: {integrity: sha512-lb1iCUTl2qy/ZYk1+lC8nxUBzCpcsupofSFqwHlP3GtbE/2uLq2A0Q448fDs9LDIuh+syC6sebonI0qpjh2Opw==} + p-limit@2.3.0: resolution: {integrity: sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==} engines: {node: '>=6'} @@ -13667,6 +13667,12 @@ snapshots: '@oxc-parser/binding-win32-arm64-msvc': 0.38.0 '@oxc-parser/binding-win32-x64-msvc': 0.38.0 + oxc-walker@0.0.1: + dependencies: + '@types/estree': 1.0.6 + estree-walker: 3.0.3 + oxc-parser: 0.38.0 + p-limit@2.3.0: dependencies: p-try: 2.2.0