Skip to content

refactor: replace TypeScript parser with oxc-parser for AST creation and analysis (v2 breaking change) - #334

Closed
thepassle with Copilot wants to merge 6 commits into
masterfrom
copilot/refactor-project-oxc-parser
Closed

thepassle with Copilot wants to merge 6 commits into
masterfrom
copilot/refactor-project-oxc-parser

Conversation

Copilot AI commented Mar 21, 2026 •

Copy link
Copy Markdown

Replaces the TypeScript compiler API (typescript package) with oxc-parser for all AST parsing and traversal. oxc-parser is significantly faster and produces a standard ESTree-compatible AST, enabling a browser build without shipping the full TypeScript compiler.

This is a breaking change intended for a v2 major release. Internal plugin code now uses the native ESTree AST structure directly. External plugins written against the old TypeScript-compiler-API interface can use the new legacy() adapter to remain compatible.

New: src/utils/oxc-adapter.js

A compatibility shim that bridges oxc-parser's ESTree output to the TypeScript AST interface used by create.js and external plugins via legacy().

Key responsibilities:

  • SyntaxKind constants — string-based, matching ESTree node type names ('ClassDeclaration', 'MethodDeclaration', etc.)
  • Type predicates — isClassDeclaration(node), isMethodDeclaration(node), etc. delegating to node.kind
  • forEachChild() — depth-first traversal via an explicit CHILD_PROPS list; aliased properties (callee→expression, object→expression, property→name) are intentionally excluded to avoid visiting the same node twice; exported as a named export for use by internal traversal code
  • createSourceFile(filename, source) — parses with oxc-parser (.ts extension forced so TypeScript syntax is accepted in .js files), then augments every node
  • Node augmentation — attaches getText(), kind, parent, getSourceFile(), jsDoc, and structural aliases to bridge ESTree ↔ TypeScript API differences:
    • VariableDeclaration → adds declarationList.declarations, initializer, name aliases
    • MethodDefinition → remapped to Constructor / GetAccessor / SetAccessor / MethodDeclaration based on its original kind (saved before augmentKind overwrites it)
    • ImportDeclaration → synthesizes importClause with .name, .namedBindings, .isTypeOnly
    • ExportNamedDeclaration / ExportDefaultDeclaration → synthesizes exportClause, moduleSpecifier, and injects export/default modifiers onto wrapped declarations
    • CallExpression.expression = .callee, MemberExpression.expression = .object, MemberExpression.name = .property
  • JSDoc — extracted from result.comments via comment-parser with spacing: 'preserve'; inline {@link symbol text} tags resolved to symboltext matching TypeScript's output

New: src/utils/legacy.js — adapter for old plugins

Provides a legacy() helper so plugins written against the old TypeScript-compiler-API interface continue to work after upgrading to v2. The wrapper re-injects the oxc-adapter ts shim into every phase callback (initialize, collectPhase, analyzePhase, moduleLinkPhase, packageLinkPhase).

// cem.config.js
import { legacy } from '@custom-elements-manifest/analyzer';
import myOldPlugin from 'my-old-cem-plugin';

export default {
  plugins: [
    legacy(myOldPlugin()),          // already-instantiated plugin
    legacy(myOldPlugin),            // factory function (called with no args)
    legacy([plugin1(), plugin2()]), // array of plugins
  ],
};

Internal plugins updated to native ESTree

All internal plugin files and utilities (features/, utils/ast-helpers.js, utils/exports.js, utils/mixins.js, utils/index.js, and all framework plugins) have been updated to use native ESTree/augmented node properties directly:

  • No ts.SyntaxKind.X — replaced with string literals (e.g., 'ClassDeclaration', 'StaticKeyword')
  • No ts.isX(node) — replaced with node.kind === 'X' or node.type === 'X' comparisons
  • No ts.forEachChild() — internal traversal code uses the named forEachChild export from the adapter

Only three files still import ts from the adapter (all appropriate):

  • create.js — core traversal engine; passes ts to plugins for legacy compatibility
  • legacy.js — the adapter wrapper for old external plugins
  • browser-entrypoint.js

Node.js version requirement

oxc-parser requires Node.js ^20.19.0 || >=22.12.0. The minimum Node.js version has been updated accordingly:

  • .nvmrc updated from v16.3.0 to v20.19.0
  • engines.node set to >=20.19.0 in both the root and analyzer package.json

Notable bugs fixed during adaptation

Bug Fix
MethodDefinition.kind ('constructor'/'get'/'set') overwritten to 'MethodDefinition' before it could be used Save kind before augmentKind, use saved value in augmentMethodDefinition
customElements.define() export emitted twice callee and expression both in CHILD_PROPS caused the callee MemberExpression to be visited twice; removed callee/object/property from CHILD_PROPS
!expr inferred as number instead of boolean SyntaxKind.ExclamationToken set to '!' (operator string) not 'ExclamationToken'
Class decorators lost when class is inside an export statement augmentClass now merges decorators into existing modifiers rather than overwriting the ExportKeyword injected by augmentExportNamedDeclaration
Arrow function return types missing augmentArrowFunction now sets returnTypeNode from node.returnType.typeAnnotation
Double-augmentation of function parameters Added _paramAugmented idempotency guard; Identifier param names stay as strings (handled directly by handleParametersAndReturnType)
Anonymous class counter off by one isUnnamed in custom-elements-define-calls.js now matches original TypeScript behavior: !isAnonymousClass || classArg?.name === undefined, so the counter increments for all define calls as before
@ignore/@internal not filtering var declarations Added 'VariableStatement' to attachableTypes in attachJsDoc
Stencil attribute types missing Stencil plugin now uses property?.typeNode (the actual type annotation) instead of property?.type (the ESTree node discriminant string)

Scope of changes

All files that previously imported from typescript now import from ./oxc-adapter or ../utils/oxc-adapter. Internal plugin code uses native ESTree properties directly. The legacy helper is exported from the main package entry point alongside create and ts. The only breaking change for plugin authors is the new Node.js minimum version requirement and the updated AST node structure passed to plugin callbacks (mitigated by legacy()).

All 26 integration tests pass.


⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

@netlify

netlify Bot commented Mar 21, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for custom-elements-manifest-analyzer failed.

Name Link
🔨 Latest commit dbc9f65
🔍 Latest deploy log https://app.netlify.com/projects/custom-elements-manifest-analyzer/deploys/69bf39e10dd6d200098c89cf

Copilot AI changed the title [WIP] Refactor project to use oxc-parser for AST creation and analysis refactor: replace TypeScript parser with oxc-parser for AST creation and analysis Mar 21, 2026
Copilot AI requested a review from thepassle March 21, 2026 09:46
@thepassle

Copy link
Copy Markdown
Member

@copilot make sure all tests pass

Copilot AI commented Mar 21, 2026

Copy link
Copy Markdown
Author

@copilot make sure all tests pass

All 26 integration tests now pass (1 skipped with - prefix as before). Fixed in commit 5d7a729.

Key fixes:

  • Class modifiers now include decorators (without overwriting existing export keywords) — fixes Lit, FAST, Stencil, Catalyst plugins
  • augmentArrowFunction now sets returnTypeNode — fixes arrow function return types
  • augmentParam idempotency guard prevents double-augmentation — fixes class method parameter names and JSDoc @param override matching
  • attachableTypes now includes 'VariableStatement' — fixes @ignore/@internal on var declarations
  • Anonymous class counter in custom-elements-define-calls.js now matches original TypeScript behavior
  • Stencil plugin uses typeNode for attribute type text

@thepassle

Copy link
Copy Markdown
Member

@copilot some pipelines failing:

11:51:05 AM: build-image version: c2b8ae031d22ab4c62dd61deb4eac30945db824f (noble-new-builds)
11:51:05 AM: buildbot version: a8301bcc15cbe6e18ec5dff35f8ab4bdf567eb05
11:51:06 AM: Building with cache
11:51:06 AM: Starting to prepare the repo for build
11:51:06 AM: Preparing Git Reference pull/334/head
11:51:08 AM: Installing dependencies
11:51:08 AM: mise ~/.config/mise/config.toml tools: python@3.14.3
11:51:08 AM: mise ~/.config/mise/config.toml tools: ruby@3.4.8
11:51:08 AM: mise ~/.config/mise/config.toml tools: go@1.26.1
11:51:08 AM: Attempting Node.js version 'v16.3.0' from .nvmrc
11:51:09 AM: v16.3.0 is already installed.
11:51:09 AM: Now using node v16.3.0 (npm v7.15.1)
11:51:09 AM: yarn workspaces detected
11:51:09 AM: Installing npm packages using Yarn version 1.22.22
11:51:10 AM: yarn install v1.22.22
11:51:10 AM: [1/5] Validating package.json...
11:51:10 AM: [2/5] Resolving packages...
11:51:10 AM: [3/5] Fetching packages...
11:51:12 AM: Failed during stage 'Install dependencies': dependency_installation script returned non-zero exit code: 1
11:51:12 AM: error oxc-parser@0.121.0: The engine "node" is incompatible with this module. Expected version "^20.19.0 || >=22.12.0". Got "16.3.0"
11:51:12 AM: error Found incompatible module.
11:51:12 AM: info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
11:51:12 AM: Error during Yarn install
11:51:12 AM: Failing build: Failed to install dependencies

fix them

also, am fine with doing a breaking change (we can release this as a 2.0 version) and have breaking changes in the AST nodes passed to plugins, but update internal plugin code to the new ast structure. But maybe we can provide a kind of adaptor to existing plugins that want to be available for use in the new major version. Maybe something so people on a newer version of @CEM/A can use "legacy" plugins like: legacy(oldPlugin) or legacy(oldPlugin(opts)) or something? not sure if thats doable but figure it out

Copilot AI commented Mar 22, 2026

Copy link
Copy Markdown
Author

@copilot some pipelines failing:

11:51:05 AM: build-image version: c2b8ae031d22ab4c62dd61deb4eac30945db824f (noble-new-builds)
...

Fixed in commit 16b99fb.

CI fix: Updated .nvmrc from v16.3.0 → v20.19.0 and set engines.node to >=20.19.0 in both package.json files — oxc-parser requires ^20.19.0 || >=22.12.0.

legacy() adapter: Added src/utils/legacy.js and exported legacy from the main package index. It wraps old TypeScript-compiler-API plugins so they continue to work after upgrading to v2:

import { legacy } from '@custom-elements-manifest/analyzer';

export default {
  plugins: [
    legacy(myOldPlugin()),           // already-instantiated plugin
    legacy(myOldPlugin),             // factory function (called with no args)
    legacy([plugin1(), plugin2()]),  // array of plugins
  ]
};

The wrapper re-injects the oxc-adapter ts shim into every phase callback (initialize, collectPhase, analyzePhase, moduleLinkPhase, packageLinkPhase), so old plugins continue to see the TypeScript-compatible API they were built against. All 26 integration tests still pass.

@thepassle

Copy link
Copy Markdown
Member

@copilot ok but so internal plugins dont need the legacy adapter then? make sure internal code is only using new AST structure, make sure tests pass

Replace all ts.SyntaxKind.X and ts.isX() calls in internal plugins
with direct node.kind string comparisons using native ESTree/augmented
node properties from the oxc-adapter.

- utils/index.js: decorator uses decorator?.type === 'Decorator'
- utils/ast-helpers.js: all ts.isX/ts.SyntaxKind replaced with kind strings
- utils/exports.js: ExportKeyword/DefaultKeyword as strings
- utils/mixins.js: all type predicates replaced with kind comparisons
- creators/handlers.js: all SyntaxKind/type predicates replaced
- creators/createClass.js: import forEachChild named export, replace all ts usages
- creators/createFunctionLike.js: FunctionDeclaration/MethodDeclaration as strings
- creators/createArrowFunction.js: ArrowFunctionExpression as string
- analyse-phase plugins: remove ts from destructuring, use string literals
- collect-phase/collect-imports.js: Program instead of SourceFile
- framework-plugins/lit/*: remove ts import/destructuring, use string literals
- framework-plugins/catalyst*/controller.js: ClassDeclaration as string
- framework-plugins/stencil/stencil.js: ClassDeclaration as string
- framework-plugins/decorators/attr.js: ClassDeclaration as string

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI commented Mar 22, 2026

Copy link
Copy Markdown
Author

@copilot ok but so internal plugins dont need the legacy adapter then? make sure internal code is only using new AST structure, make sure tests pass

Done in commit dbc9f65. All internal plugins and utilities now use native ESTree/augmented node properties directly — no ts.SyntaxKind.X, ts.isX(), or ts.forEachChild() in any plugin or utility file.

Only three files still import ts from the adapter (all appropriate):

  • create.js — core traversal engine, passes ts to plugins for legacy compat
  • legacy.js — the adapter wrapper for external old plugins
  • browser-entrypoint.js

The forEachChild used in internal traversal code is now imported as a named export from the adapter. All 26 integration tests pass.

Copilot AI changed the title refactor: replace TypeScript parser with oxc-parser for AST creation and analysis refactor: replace TypeScript parser with oxc-parser for AST creation and analysis (v2 breaking change) Mar 22, 2026
@thepassle thepassle closed this Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants