Commit Graph

69 Commits

Author SHA1 Message Date
Boshen ebb80b32ff feat(ast): add node_id field to all AST struct nodes (#18138)
## Summary

- Add `pub node_id: NodeId` as the first field in all AST structs (~190 structs across js.rs, ts.rs, jsx.rs, literal.rs)
- Update codegen to special-case `node_id`:
  - `Dummy`: Use `NodeId::DUMMY` instead of `Dummy::dummy(allocator)`
  - `CloneIn`: Use `NodeId::DUMMY` for regular clone, preserve ID for semantic clone
  - `ContentEq`: Skip `node_id` from equality comparisons
  - `ESTree`: Skip `node_id` from serialization
  - `AstBuilder`: Exclude `node_id` from builder parameters, use `NodeId::DUMMY`
- Update pattern matching in minifier and transformer to use `..` for new field
- Update traverse script to include `NodeId` import

This is the foundational step for assigning unique IDs to AST nodes. The `node_id` defaults to `NodeId::DUMMY` (value 0) for uninitialized nodes.

## Test plan

- [x] `cargo build` succeeds
- [x] `cargo test` passes
- [x] `just ast` regenerates code correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-02-12 08:06:28 +00:00
Boshen 57917eef0f fix(parser): parse decorators on rest parameters (#18938)
## Summary
- Add `decorators` field to `FormalParameterRest` to support decorators on rest parameters in TypeScript
- Parse decorators before checking for rest parameters in formal parameter lists
- Update semantic builder to visit decorators on rest parameters

Fixes parsing of `typescript/tests/cases/compiler/sourceMapValidationDecorators.ts`

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-02-05 09:54:47 +00:00
overlookmotel e39a983ce3 refactor(linter/plugins): store source text in end of buffer (#18714)
Previously we had to store source text at start of buffers sent to JS via raw transfer.

#18376 made changes to how raw transfer deserializer handles strings, in order to support files containing a BOM. Building on that, we're now able to remove the requirement that source text be at start of the buffer entirely.

This PR changes the deserializer used in Oxlint JS plugins to accept source text being anywhere in the buffer, *as long as no other strings are after it*. In practice this just means that the source text must be allocated before anything else, which is easy to satisfy.

Now the source text can be allocated with just the usual safe `allocator.alloc_str(source_text)` method.

This change removes a ton of dodgy workarounds and unsafe code we used previously to get source text at the start of buffer. It makes the code less labyrinthine and far less likely a slip up can inadvertently introduce UB.

Note: In `napi/parser`, source text still *is* at start of the buffer, as that's simpler and more efficient when the source text is written into the buffer on JS side. This change only affects Oxlint.
2026-01-30 00:52:09 +00:00
Boshen c205b0df51 fix(ast): remove ThisExpression from TSModuleReference (#18489)
## Summary

- Removes `ThisExpression` variant from `TSModuleReference` since `import x = this` is invalid TypeScript (error TS1359)
- Adds parser error when `this` is used in import equals declaration
- Updates codegen, transformer, and linter rules to handle the new enum structure

Closes #16841

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-01-24 15:25:10 +00:00
overlookmotel 09a4057f14 refactor(linter/plugins, napi/parser): remove pattern of destructuring globals at top level (#18455)
Pure refactor.

Remove a few remaining instances of the pattern where we destructure global objects at top level e.g. `const { isArray } = Array;`.

In Oxlint, TSDown plugin already performs this optimization. In `napi/parser`, we intend to build the package with TSDown, and we'll apply the same optimization in build process then too.
2026-01-23 14:28:18 +00:00
overlookmotel 8db0e78ae6 feat(linter/plugins): handle BOMs (#18376)
Closes #12526.

Handle BOM on start of files in the same way that ESLint does - do not include it in the source text on JS side, but `context.sourceCode.hasBOM` evaluates to `true`.

Method:

* Alter `program.source_text` to trim off the BOM before passing AST to JS side.
* Add a `has_bom` flag to `RawTransferMetadata`.
* Add ability to add an offset in the conversion from UTF-8 to UTF-16 spans.

The result is that the file as it's seen on JS side is as if the BOM didn't exist (except for the `hasBOM` flag). Spans are converted accordingly in JS-side AST, and converted back when passing diagnostics back to Rust.
2026-01-22 02:16:54 +00:00
overlookmotel 6ac09e2a9d feat(linter/plugins): support source text not being at start of buffer (#18375)
Previous raw transfer required that the source text start exactly at the start of the buffer. In linter, relax this restriction and handle when source text is stored elsewhere in the buffer.

This is necessary for stripping BOM from source (#18376), as then the source text used on JS side starts 3 bytes after the start of the buffer.
2026-01-22 02:16:53 +00:00
Boshen 7174336d10 refactor(parser): shorten raw transfer deserializer code for non-range variants (#18137)
## Summary

- Use inline assignment pattern (`let x; { x: x = value }`) for `start` and `end` fields instead of declaring them in the preamble
- This allows minifiers to eliminate unused variables when RANGE is false
- Restores original file size for deserializers that don't use range fields

**Before (js.js with RANGE=false):**
```javascript
function deserializeIdentifierName(pos) {
  let start = deserializeU32(pos),
    end = deserializeU32(pos + 4);
  return { type: "Identifier", name: deserializeStr(pos + 8), start, end };
}
```

**After (js.js with RANGE=false):**
```javascript
function deserializeIdentifierName(pos) {
  return { type: "Identifier", name: deserializeStr(pos + 8), start: deserializeU32(pos), end: deserializeU32(pos + 4) };
}
```

Closes #14332

## Test plan

- [x] `just ast` regenerates deserializers correctly
- [x] `pnpm test --filter='@oxc-project/napi-parser'` passes (109 tests)
- [x] `cargo clippy -p oxc_ast_tools` passes

🤖 Generated with [Claude Code](https://claude.ai/code)
2026-01-21 02:55:05 +00:00
Boshen 4f45a2845a refactor(ast)!: replace TSMappedType.type_parameter with dedicated key and constraint fields (#18185)
## Summary

- Replace `type_parameter: Box<'a, TSTypeParameter<'a>>` with two dedicated fields:
  - `key: BindingIdentifier<'a>` - the parameter name (e.g., `P` in `[P in keyof T]`)
  - `constraint: TSType<'a>` - the constraint type (e.g., `keyof T`)
- Improves type safety by preventing invalid AST states - `TSTypeParameter` contains `const`, `in`, `out`, and `default` fields that are syntactically invalid in mapped types
- Matches TSESTree structure more closely

Closes #15101

## Test plan

- [x] All parser, codegen, semantic, and linter tests pass
- [x] Conformance tests show no regressions
- [x] Semantic snapshots updated to reflect the new structure

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-01-18 14:49:37 +00:00
Boshen 08dad63678 feat(span): add sourceType: 'commonjs' support (#18089)
## Summary

- Add `ModuleKind::CommonJS` variant to support CommonJS module type in the parser
- Enable top-level `return` statements (CommonJS files are wrapped in a function)
- Enable top-level `new.target` (allowed in CommonJS function wrapper)
- Add proper errors for `import`/`export` statements in CommonJS
- Add proper errors for `import.meta` in CommonJS

The CommonJS source type can be set via:
- `SourceType::with_commonjs(true)` method
- Babel's `sourceType: "commonjs"` option

Closes #16200

## Test plan

- [x] Added test cases in `tasks/coverage/misc/pass/` for valid CommonJS features
- [x] Added test cases in `tasks/coverage/misc/fail/` for invalid CommonJS usage
- [x] Enabled Babel conformance tests for `sourceType: "commonjs"`
- [x] All existing tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-01-16 16:08:27 +00:00
Boshen 9135b0b2f3 feat(ast)!: move scope from TSEnumDeclaration to TSEnumBody (#18058)
## Summary

- Move `scope_id: Cell<Option<ScopeId>>` from `TSEnumDeclaration` to `TSEnumBody`
- Since `TSEnumBody` was introduced as a dedicated type in PR #10284, the scope logically belongs on the body rather than on the parent declaration node

Closes #10331

## Test plan

- [x] Unit tests pass (`just test`)
- [x] Conformance tests pass (`just conformance`)
- [x] Updated semantic test snapshots reflect scope now on `TSEnumBody`

🤖 Generated with [Claude Code](https://claude.ai/code)
2026-01-16 13:30:08 +00:00
Peter Wagenet 7a0ca9999b fix(linter/plugins): use correct ScriptKind for tokens (#17185)
When parsing tokens for JS plugins, the TypeScript parser was always using `ScriptKind.TSX`, regardless of file extension. This caused TypeScript to incorrectly parse generic arrow functions like `<T>() => {}` in `.ts` files, creating bogus `JsxText` tokens that overlap with comments.

This PR adds an `is_jsx` flag to `RawTransferMetadata`. Rust side sets the flag depending on source type, and JS side uses that information to pass the correct `ScriptKind` for the file to TypeScript parser.

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2026-01-15 19:06:08 +00:00
camc314 3984750c69 refactor(all)!: remove binding pattern wierdness (#15925)
fixes #11489

Note, in quite a few places i changed the span we report on to look something like this, the only reason for this is to reduce the diff in some of the snapshots
```
decl.type_annotation.as_ref().map_or_else(
                        || decl.id.span(),
                        |type_annotation| decl.id.span().merge(type_annotation.span()),
                    ),
```
2025-12-22 10:04:20 +00:00
overlookmotel 5aefb84daa perf(linter/plugins): reduce size of walker code (#17176)
By hoisting `let enter;` up out of the the `if` block, minifier is able to convert the `if` statement into a `&&` expression, resulting in tighter minified output.

This optimization is only visible in the bundled output, as that's where we do more aggressive minification.
2025-12-20 13:59:13 +00:00
overlookmotel 19566641dc refactor(linter/plugins, parser/napi): add JSDoc comments to generated code (#17175)
Just add JSDoc comments to some generated code.
2025-12-20 13:59:13 +00:00
overlookmotel 24a8d9e17e refactor(linter/plugins, parser/napi): generate type IDs maps as variants (#17174)
Pure refactor. Generate the JS files containing `NODE_TYPE_IDS_MAP` as variants of a single source code file, instead of constructing to two different versions for parser and linter manually.

This makes changes in further PRs to come easier to implement.
2025-12-20 13:59:13 +00:00
overlookmotel e59132b6c0 fix(parser/napi): fix lazy deser (#17069)
Lazy deserialization in `oxc-parser` was broken by #16957. Fix it.
2025-12-18 15:48:00 +00:00
overlookmotel a92faf08e8 fix(ast_tools): support u128 in assert_layouts generator (#17050)
Previously `u128` was not supported in `assert_layouts` generator, because Rust changed the alignment of `u128` from 8 to 16 - and therefore the alignment depended on Rust version.

That change happened some time ago now, and all Rust versions above our MSRV have `u128` with alignment of 16. So we can now support `u128` (and also `i128`, `NonZeroU128`, `NonZeroI128`).

Also support `u128` in `raw_transfer` and `raw_transfer_lazy` generators.

This will help with making lexer tokens serializable (#17025), since `Token` contains a `u128`.
2025-12-18 11:58:03 +00:00
camc314 5d211a8c69 refactor(napi/parser): move generated JS into src-js (#16957)
moves the generated code into src-js to keep the same pattern as oxlint
2025-12-17 11:43:42 +00:00