Skip to content

Commit

Permalink
Add accessor contextual modifier to class properties
Browse files Browse the repository at this point in the history
Adds the `accessor` modifier to public and private class properties.
This is the first part of the implementation of the latest version of
the decorators proposal: https://github.com/tc39/proposal-decorators

The keyword is outlined here in the README: https://github.com/tc39/proposal-decorators#class-auto-accessors

It was unclear to me if there is any feature flagging that should be
added in here at the parser level, or if that happens at a later stage.
I know that decorators would refuse to parse, for instance, unless you
have the decorators proposal enabled, but it seems like that might
happen in a different layer?
  • Loading branch information
pzuraq committed Aug 16, 2021
1 parent d3a7cd5 commit d071aa8
Show file tree
Hide file tree
Showing 185 changed files with 675 additions and 159 deletions.
1 change: 1 addition & 0 deletions packages/babel-parser/ast/spec.md
Expand Up @@ -1235,6 +1235,7 @@ interface ClassProperty <: Node {
value: Expression;
static: boolean;
computed: boolean;
accessor: boolean;
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/package.json
Expand Up @@ -4,7 +4,7 @@
"description": "A JavaScript parser",
"author": "The Babel Team (https://babel.dev/team)",
"homepage": "https://babel.dev/docs/en/next/babel-parser",
"bugs": "https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22pkg%3A+parser+%28babylon%29%22+is%3Aopen",
"bugs": "https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22pkg%3A+parser+%28bab;lon%29%22+is%3Aopen",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down
26 changes: 24 additions & 2 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1373,6 +1373,7 @@ export default class StatementParser extends ExpressionParser {
prop.computed = false;
prop.key = key;
prop.static = false;
prop.accessor = false;
classBody.body.push(this.parseClassProperty(prop));
return true;
}
Expand Down Expand Up @@ -1409,10 +1410,11 @@ export default class StatementParser extends ExpressionParser {
) {
const publicMethod: $FlowSubtype<N.ClassMethod> = member;
const privateMethod: $FlowSubtype<N.ClassPrivateMethod> = member;
const publicProp: $FlowSubtype<N.ClassMethod> = member;
const privateProp: $FlowSubtype<N.ClassPrivateMethod> = member;
const publicProp: $FlowSubtype<N.ClassProperty> = member;
const privateProp: $FlowSubtype<N.ClassPrivateProperty> = member;
const method: typeof publicMethod | typeof privateMethod = publicMethod;
const prop: typeof publicProp | typeof privateProp = publicProp;
const publicMember: typeof publicMethod | typeof publicProp = publicMethod;
member.static = isStatic;
Expand Down Expand Up @@ -1486,6 +1488,8 @@ export default class StatementParser extends ExpressionParser {
allowsDirectSuper,
);
} else if (this.isClassProperty()) {
prop.accessor = false;
if (isPrivate) {
this.pushClassPrivateProperty(classBody, privateProp);
} else {
Expand Down Expand Up @@ -1563,6 +1567,24 @@ export default class StatementParser extends ExpressionParser {
}

this.checkGetterSetterParams(publicMethod);
} else if (
isContextual &&
key.name === "accessor" &&
!this.isLineTerminator()
) {
this.resetPreviousNodeTrailingComments(key);

// The so-called parsed name would have been "get/set": get the real name.
const isPrivate = this.match(tt.privateName);
this.parseClassElementName(publicProp);

prop.accessor = true;

if (isPrivate) {
this.pushClassPrivateProperty(classBody, privateProp);
} else {
this.pushClassProperty(classBody, publicProp);
}
} else if (this.isLineTerminator()) {
// an uninitialized class property (due to ASI, since we don't otherwise recognize the next token)
if (isPrivate) {
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -813,6 +813,7 @@ export type ClassProperty = ClassMemberBase &
type: "ClassProperty",
key: Expression,
value: ?Expression, // TODO: Not in spec that this is nullable.
accessor: boolean,

typeAnnotation?: ?TypeAnnotationBase, // TODO: Not in spec
variance?: ?FlowVariance, // TODO: Not in spec
Expand All @@ -828,6 +829,7 @@ export type ClassPrivateProperty = NodeBase & {
value: ?Expression, // TODO: Not in spec that this is nullable.
static: boolean,
computed: false,
accessor: boolean,

// Flow and Typescript
typeAnnotation?: ?TypeAnnotationBase,
Expand Down
Expand Up @@ -33,6 +33,7 @@
"name": "p"
}
},
"accessor": false,
"value": null
}
]
Expand Down
Expand Up @@ -30,6 +30,7 @@
"name": "a"
},
"computed": false,
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":18,"end":19,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":7}},
Expand Down Expand Up @@ -98,6 +99,7 @@
"name": "b"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":61,"end":62,"loc":{"start":{"line":8,"column":7},"end":{"line":8,"column":8}},
Expand Down Expand Up @@ -222,12 +224,12 @@
"start":139,"end":143,"loc":{"start":{"line":14,"column":11},"end":{"line":14,"column":15},"identifierName":"Math"},
"name": "Math"
},
"computed": false,
"property": {
"type": "Identifier",
"start":144,"end":150,"loc":{"start":{"line":14,"column":16},"end":{"line":14,"column":22},"identifierName":"random"},
"name": "random"
},
"computed": false
}
},
"arguments": []
}
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand All @@ -59,6 +60,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":27,"end":28,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand All @@ -59,6 +60,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":34,"end":35,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
Expand Down
Expand Up @@ -62,6 +62,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":31,"end":32,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -62,6 +62,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":38,"end":39,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -61,6 +61,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":27,"end":28,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -61,6 +61,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":34,"end":35,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -68,6 +68,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":32,"end":33,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -68,6 +68,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":39,"end":40,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand All @@ -59,6 +60,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":34,"end":35,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand All @@ -59,6 +60,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":41,"end":42,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand Down
Expand Up @@ -36,6 +36,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
Expand Down
Expand Up @@ -62,6 +62,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":38,"end":39,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -62,6 +62,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":45,"end":46,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -61,6 +61,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":34,"end":35,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -61,6 +61,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":41,"end":42,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -68,6 +68,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":39,"end":40,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8}},
Expand Down
Expand Up @@ -68,6 +68,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "NumericLiteral",
"start":46,"end":47,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
Expand Down
Expand Up @@ -48,6 +48,7 @@
"name": "y"
}
},
"accessor": false,
"value": null
}
]
Expand Down
Expand Up @@ -50,6 +50,7 @@
"name": "x"
}
},
"accessor": false,
"value": {
"type": "Identifier",
"start":24,"end":29,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":29},"identifierName":"await"},
Expand Down

0 comments on commit d071aa8

Please sign in to comment.