Skip to content

Adds the @init: modifier to decorator plugins#13859

Merged
nicolo-ribaudo merged 5 commits intobabel:feat-7.16.0/decoratorsfrom
pzuraq:add-init
Nov 24, 2021
Merged

Adds the @init: modifier to decorator plugins#13859
nicolo-ribaudo merged 5 commits intobabel:feat-7.16.0/decoratorsfrom
pzuraq:add-init

Conversation

@pzuraq
Copy link
Copy Markdown
Contributor

@pzuraq pzuraq commented Oct 18, 2021

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

Adds the @init: modifier to the decorator plugins. If present, this
adds init: true to the decorator node. This currently works in both
plugins, the idea being that we can error for legacy versions of
decorators which do not support @init: in the Babel plugin.

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci bot commented Oct 18, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 3fec3c6:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Oct 18, 2021

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

@JLHwung JLHwung added PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Decorators labels Oct 18, 2021
@@ -446,14 +446,42 @@ export default class StatementParser extends ExpressionParser {

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.

Nit: Can you put a spec link as comment before hasPlugin("decorators")? For example https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-decorators.

@fedeci fedeci self-requested a review October 18, 2021 15:57
shouldParseExportDeclaration(): boolean {
const { type } = this.state;
if (type === tt.at) {
if (type === tt.at || type === tt.atInit) {
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung Nov 8, 2021

Choose a reason for hiding this comment

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

We don't need to expect decorators-legacy plugin when type is tt.atInit, since the token is invalid in the legacy decorators. Maybe we can move the expectPlugin check to the tokenizer so we don't have to take care of that in parsing level.

// Replace the previous state
this.state = old;

if (wordNext === charCodes.colon) {
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung Nov 8, 2021

Choose a reason for hiding this comment

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

We need to check the next character after : because it may form a doubleColon token ::, e.g.

class C {
  @init::b p;
}

is valid given that decorators-legacy and functionBind are enabled.


if (wordNext === charCodes.colon) {
if (word !== "init" || containsEsc) {
throw this.raise(this.state.pos, Errors.UnsupportedDecoratorModifier);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need to throw, we can recover by parsing it as if it was @init: (which is what would already happen by just removing throw).

Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

It looks like some fixtures need to be updated!

shouldParseExportDeclaration(): boolean {
const { type } = this.state;
if (type === tt.at) {
if (type === tt.at || type === tt.atInit) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (type === tt.at || type === tt.atInit) {
if (tokenIsDecorator(type)) {

@pzuraq pzuraq force-pushed the add-init branch 2 times, most recently from 5dd248e to 56a5229 Compare November 23, 2021 16:55
Adds the `@init:` modifier to the decorator plugins. If present, this
adds `init: true` to the decorator node. This currently works in both
plugins, the idea being that we can error for legacy versions of
decorators which do not support `@init:` in the Babel plugin.
@nicolo-ribaudo
Copy link
Copy Markdown
Member

I have rebased feat-7.16.0/decorators since it should solve most CI failures; I'm rebasing this one now.

@nicolo-ribaudo
Copy link
Copy Markdown
Member

The last commit fixes #13859 (comment).

@nicolo-ribaudo
Copy link
Copy Markdown
Member

The last commit does #13859 (comment)

@nicolo-ribaudo nicolo-ribaudo merged commit e292ecc into babel:feat-7.16.0/decorators Nov 24, 2021
nicolo-ribaudo added a commit that referenced this pull request Dec 5, 2021
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
@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 Feb 24, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2022
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 PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Decorators

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants