Skip to content

Fix decorators interaction with abstract/declare fields#17947

Merged
nicolo-ribaudo merged 2 commits into
babel:mainfrom
nicolo-ribaudo:ts-dec
Apr 27, 2026
Merged

Fix decorators interaction with abstract/declare fields#17947
nicolo-ribaudo merged 2 commits into
babel:mainfrom
nicolo-ribaudo:ts-dec

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Fixes #17942

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Apr 24, 2026
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Apr 24, 2026

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61364

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 24, 2026

Open in StackBlitz

commit: 986843e

if (kind === FIELD_IGNORED) {
if (hasDecorators) {
throw element.buildCodeFrameError(
"Class elements marked with 'declare' or 'abstract' cannot be decorated.",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this PR does not quite fix the original issue.

To recap, we injected the protoInitCall, one of the fieldInitializerExpressions to a non-static field:

if (
fieldInitializerExpressions.length > 0 &&
!isStatic &&
(kind === FIELD || kind === ACCESSOR)
) {
prependExpressionsToFieldInitializer(
fieldInitializerExpressions,
element as NodePath<t.ClassProperty | t.ClassPrivateProperty>,
);
fieldInitializerExpressions = [];
}

The original issue does not provide the config required to reproduce the error. I tried a bit on REPL and I can only reproduce the error when we keep the typescript syntax but transform the decorators (link) And the posted error stacktrace does look like a third-party TS parser, which means the user might have kept the TS syntax and fed the Babel output to another TS aware tool.

Previously we disallowed untransformed TS class features in packages/babel-helper-create-class-features-plugin/src/typescript.ts, maybe we can guard the abstract field as well. Alternatively, we can also skip TS abstract field / declared field / finite field in the decorators transform.

@nicolo-ribaudo
Copy link
Copy Markdown
Member Author

nicolo-ribaudo commented Apr 24, 2026

This PR fixes the case in which you only transpile decorators and not TS. I suspect that the original issue is using Babel to transpile decorators, and then feeding the output into Oxc to transpile the rest.

Alternatively, we can also skip TS abstract field / declared field / finite field in the decorators transform.

Uhm, isn't this what this PR does? We skip abstract/declare fields in the loop so that we don't attach the fieldInitializerExpressions to them.


I'll split the parser changes into a separate PR: #17949

@JLHwung
Copy link
Copy Markdown
Contributor

JLHwung commented Apr 24, 2026

This PR fixes the case in which you only transpile decorators and not TS. I suspect that the original issue is using Babel to transpile decorators, and then feeding the output into Oxc to transpile the rest.

Alternatively, we can also skip TS abstract field / declared field / finite field in the decorators transform.

Uhm, isn't this what this PR does? We skip abstract/declare fields in the loop so that we don't attach the fieldInitializerExpressions to them.

I'll split the parser changes into a separate PR: #17949

Thank you, yeah the split changes are much easier to review.

@nicolo-ribaudo nicolo-ribaudo merged commit 032ac12 into babel:main Apr 27, 2026
56 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the ts-dec branch April 27, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decorators conflicts with abstract fields - trying to initialize with _initProto and compiler throws

4 participants