Skip to content

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jan 24, 2024

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

This is a refactoring PR that paves the road to implement pzuraq/ecma262#12.

Previously we are searching the first field to inject initProto. In this PR we create a fieldInitializerAssignments queue, which will be prepended to the next available non-static field initializers, falling back to the class constructor. Though in this PR the fieldInitializerAssignments contains only the initProto call, it is designed to work with other assignments as well, such as the extra initializers introduced in pzuraq/ecma262#12.

For example, according to the updated spec, the input

class C {
  @dec #p = 1;
  #q = 2;
}

should be transformed to

class C {
  static {
    [_init_p, _extra_init_p] = _applyDecs(this, [[dec, 0, "p", o => o.#p, (o, v) => o.#p = v]], [], 0, _ => #q in _).e;
  }
  #p = _init_p(this, 1);
  #q = (_extra_init_p(this), 2);
}

as the extra initializers added by context.addInitializers() are now run after the field is created in the receiver class.

The test fixtures are updated because protoInit and the staticInit id are now declared before other generated identifiers. There are no behaviour changes introduced in this PR.

@babel-bot
Copy link
Collaborator

babel-bot commented Jan 24, 2024

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

if (initializer.node) {
expressions.push(initializer.node);
} else {
expressions.push(t.unaryExpression("void", t.numericLiteral(0)));
Copy link
Member

Choose a reason for hiding this comment

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

It's ok to do it in a follow-up if you want to keep the fixtures diff minimal, but we can make this shorter by doing

  x = expr1, void expr2

instead of

  x = expr1, expr2, void 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I can make the change like you said. This branch is currently unreachable because the protoInit call is currently injected in the first decorated field, which will include at least a init_field call in its initializer. It is probably an oversight and I will open a new PR for that.

@nicolo-ribaudo
Copy link
Member

(please don't merge right now, as the release action is running and I don't know if merging would cause problems)

@nicolo-ribaudo nicolo-ribaudo merged commit 2249bb4 into babel:main Jan 25, 2024
@nicolo-ribaudo nicolo-ribaudo deleted the refactor-proto-init-call-injection branch January 25, 2024 17:03
liuxingbaoyu pushed a commit to liuxingbaoyu/babel that referenced this pull request Mar 5, 2024
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 26, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants