fix(semantic): allow new.target in class field initializers#18349
Merged
graphite-app[bot] merged 1 commit intomainfrom Jan 21, 2026
Merged
fix(semantic): allow new.target in class field initializers#18349graphite-app[bot] merged 1 commit intomainfrom
new.target in class field initializers#18349graphite-app[bot] merged 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes semantic validation to correctly allow new.target in class field initializers per the ES2022 specification. Previously, Oxc incorrectly rejected new.target in this context, following TypeScript's outdated behavior from before the class fields standard was finalized. The fix aligns Oxc with Babel and the ES2022 spec, where new.target evaluates to undefined in field initializers.
Changes:
- Updated
new.targetvalidation logic to allow usage in class field initializers (both static and instance fields) - Improved error message to reflect the expanded set of valid contexts
- Fixed handling of arrow functions within field initializers (they inherit
new.targetfrom the field context)
Reviewed changes
Copilot reviewed 2 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
tasks/coverage/snapshots/semantic_babel.snap |
Shows Babel test es2022/class-properties/new-target now passes (1940 vs 1939 tests) |
tasks/coverage/snapshots/parser_typescript.snap |
Removes 4 incorrect errors for arrow functions in class field initializers |
tasks/coverage/snapshots/parser_babel.snap |
Shows Babel parsing test now passes (2206 vs 2205 tests) |
crates/oxc_semantic/src/diagnostics.rs |
Updates error message to include "class field initializers" as valid context |
crates/oxc_semantic/src/checker/javascript.rs |
Implements AST-based check for PropertyDefinition/AccessorProperty before falling back to scope-based validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
Author
Merge activity
|
a60c3f8 to
1c4f3ce
Compare
## Summary - Allow `new.target` in class field initializers per the ES2022 spec - `new.target` evaluates to `undefined` in this context, same as in static blocks - This aligns Oxc with Babel's behavior ## Background TypeScript incorrectly disallows `new.target` in class field initializers. Their validation was implemented in 2016 ([PR #12783](microsoft/TypeScript#12783)) before class fields were standardized, and was never updated when ES2022 landed. The ES spec explicitly allows this - see the [TC39 class-fields proposal](https://github.com/tc39/proposal-class-fields) and [Babel issue #6243](babel/babel#6243). ## Test case ```javascript class X { static a = new.target; // ✓ allowed (evaluates to undefined) static b = () => new.target; // ✓ allowed (arrow inherits from field context) e = new.target; // ✓ allowed f = () => new.target; // ✓ allowed } ``` ## Test plan - [x] Babel conformance test `es2022/class-properties/new-target` now passes - [x] `cargo test -p oxc_semantic --lib` passes - [x] `just check lint test` passes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1c4f3ce to
6037995
Compare
overlookmotel
pushed a commit
that referenced
this pull request
Jan 26, 2026
### 💥 BREAKING CHANGES - 22dec6a semantic: [**BREAKING**] Remove `Scoping::scope_build_child_ids` and all related APIs (#18362) (Dunqing) - 30a4899 oxc: [**BREAKING**] Remove `CompilerInterface::semantic_child_scope_ids` (#18361) (Dunqing) - 777fc40 ast: [**BREAKING**] Add `Ident` type (#18354) (Boshen) - af0ca46 span: [**BREAKING**] Use `ModuleKind::CommonJS` for `SourceType::cjs()` (#18276) (sapphi-red) ### 🚀 Features - 0a02026 semantic: Add TS1499 code to diagnostic (#18557) (camc314) - 8b4618f parser: Add TS1500 code to diagnostic (#18547) (camc314) - 866b6b3 parser: Add TS1048 code to diagnostic (#18546) (camc314) - 1117c44 parser: Add TS1054 code to diagnostic (#18541) (camc314) - e4fcdde semantic: Add TS1053 code to diagnostic (#18539) (camc314) - bcbf396 semantic: Add TS1052 code to diagnostic (#18538) (camc314) - 8155edf semantic: Add TS1049 code to diagnostic (#18535) (camc314) - 51d3b3f parser: Add TS1502 code to diagnostic (#18534) (camc314) - 00854e8 semantic: Add TS2337 error code to super call diagnostic (#18531) (camc314) - 993fd2b parser: Parse unambiguous await with better error messages (#18480) (Boshen) - 8db0e78 linter/plugins: Handle BOMs (#18376) (overlookmotel) - 6ac09e2 linter/plugins: Support source text not being at start of buffer (#18375) (overlookmotel) - 2ef5647 ast: Add escape_raw parameter to template_element builders (#18121) (Boshen) ### 🐛 Bug Fixes - 74d0998 semantic: Update error msg for multiple `default` cases in switch stmt (#18526) (camc314) - c205b0d ast: Remove `ThisExpression` from `TSModuleReference` (#18489) (Boshen) - aed3669 parser: Parse HTML-like comments in unambiguous mode (#18442) (Boshen) - c4132fb parser: Validate accessor parameters in interface method signatures (#18391) (Boshen) - b0cd74d semantic: Allow `var` and `function` with same name in static blocks (#18358) (Boshen) - 6037995 semantic: Allow `new.target` in class field initializers (#18349) (Boshen) - 9a15c6a semantic: Do not rely on spans for node comparison in `Function::bind` (#18296) (overlookmotel) ### ⚡ Performance - 6b600c4 semantic: Skip parent lookup for function declarations in `Function::bind` (#18293) (overlookmotel) - c27ad2d semantic: Move check for function declaration out of `is_function_part_of_if_statement` (#18292) (overlookmotel) - 63eb89e semantic: Skip checking redeclarations for function expressions (#18291) (overlookmotel) - 7c12743 semantic: Skip checking unresolved exports in CommonJS files (#18250) (overlookmotel) - 2349031 allocator: Increase initial chunk size from 512B to 16KB (#18234) (Boshen) ### 📚 Documentation - 8ccd853 npm: Update package homepage URLs and add keywords (#18509) (Boshen) - 9b3165f napi/parser: Clarify when to use `parseAsync` vs `parseSync` (#18486) (Boshen) - 1b59f63 napi/parser: Correct typo in README (#18251) (overlookmotel) - 00ff75f mangler: Fix `top_level` option in example (#18233) (overlookmotel) - 2ddc073 semantic: Fix typo in comment (#18238) (overlookmotel) Co-authored-by: Boshen <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
new.targetin class field initializers per the ES2022 specnew.targetevaluates toundefinedin this context, same as in static blocksBackground
TypeScript incorrectly disallows
new.targetin class field initializers. Their validation was implemented in 2016 (PR #12783) before class fields were standardized, and was never updated when ES2022 landed.The ES spec explicitly allows this - see the TC39 class-fields proposal and Babel issue #6243.
Test case
Test plan
es2022/class-properties/new-targetnow passescargo test -p oxc_semantic --libpassesjust check lint testpasses🤖 Generated with Claude Code