Skip to content

Commit 10c4bd4

Browse files
authored
Support ESTree AccessorProperty (#17082)
* feat: create AccessorProperty for ESTree * add new test cases * add private key test case * update Babel 8 test fixtures * support AccessorProperty in eslint-* * guard accessor property rename by classFeatures * update parser options schema * address review comments
1 parent c014c63 commit 10c4bd4

15 files changed

Lines changed: 288 additions & 3 deletions

File tree

eslint/babel-eslint-parser/src/analyze-scope.cts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ class Referencer extends OriginalReferencer {
194194
this._visitClassProperty(node);
195195
}
196196

197+
AccessorProperty(node: any) {
198+
this._visitClassProperty(node);
199+
}
200+
197201
ClassAccessorProperty(node: any) {
198202
this._visitClassProperty(node);
199203
}

eslint/babel-eslint-plugin/src/rules/no-undef.cjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ const rule = (
1414
* @returns {Boolean} Returns true if the node is under a decorator.
1515
*/
1616
function isAccessorFieldName(node) {
17+
const parent = node.parent;
1718
return (
18-
node.parent.type === "ClassAccessorProperty" &&
19-
node.parent.key === node &&
20-
!node.parent.computed
19+
(parent.type === "AccessorProperty" ||
20+
(!process.env.BABEL_8_BREAKING &&
21+
parent.type === "ClassAccessorProperty")) &&
22+
parent.key === node &&
23+
!parent.computed
2124
);
2225
}
2326

packages/babel-parser/data/schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
},
1010
"type": "object"
1111
},
12+
"EstreePluginOptions": {
13+
"properties": {
14+
"classFeatures": {
15+
"type": "boolean"
16+
}
17+
}
18+
},
1219
"FlowPluginOptions": {
1320
"properties": {
1421
"all": {
@@ -98,6 +105,19 @@
98105
"additionalItems": false,
99106
"type": "array"
100107
},
108+
{
109+
"items": [
110+
{
111+
"enum": ["estree"],
112+
"type": "string"
113+
},
114+
{
115+
"$ref": "#/definitions/EstreePluginOptions"
116+
}
117+
],
118+
"additionalItems": false,
119+
"type": "array"
120+
},
101121
{
102122
"items": [
103123
{

packages/babel-parser/src/plugins/estree.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ export default (superClass: typeof Parser) =>
344344
return propertyNode;
345345
}
346346

347+
parseClassAccessorProperty(
348+
this: Parser,
349+
node: N.ClassAccessorProperty,
350+
): any {
351+
const accessorPropertyNode = super.parseClassAccessorProperty(node);
352+
if (!process.env.BABEL_8_BREAKING) {
353+
if (!this.getPluginOption("estree", "classFeatures")) {
354+
return accessorPropertyNode;
355+
}
356+
}
357+
(accessorPropertyNode as unknown as N.EstreeAccessorProperty).type =
358+
"AccessorProperty";
359+
return accessorPropertyNode;
360+
}
361+
347362
parseObjectMethod(
348363
prop: N.ObjectMethod,
349364
isGenerator: boolean,

packages/babel-parser/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,11 @@ export interface EstreePropertyDefinition extends EstreePropertyDefinitionBase {
14331433
value: Expression;
14341434
}
14351435

1436+
export interface EstreeAccessorProperty extends EstreePropertyDefinitionBase {
1437+
type: "AccessorProperty";
1438+
value: Expression;
1439+
}
1440+
14361441
export interface EstreeChainExpression extends NodeBase {
14371442
type: "ChainExpression";
14381443
expression: Expression;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A {
2+
accessor foo;
3+
accessor bar = 0;
4+
accessor #qux;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": false
3+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":64,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":64,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ClassDeclaration",
12+
"start":0,"end":64,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
13+
"id": {
14+
"type": "Identifier",
15+
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"},
16+
"name": "A"
17+
},
18+
"superClass": null,
19+
"body": {
20+
"type": "ClassBody",
21+
"start":8,"end":64,"loc":{"start":{"line":1,"column":8},"end":{"line":5,"column":1}},
22+
"body": [
23+
{
24+
"type": "ClassAccessorProperty",
25+
"start":12,"end":25,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":15}},
26+
"static": false,
27+
"key": {
28+
"type": "Identifier",
29+
"start":21,"end":24,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":14},"identifierName":"foo"},
30+
"name": "foo"
31+
},
32+
"computed": false,
33+
"value": null
34+
},
35+
{
36+
"type": "ClassAccessorProperty",
37+
"start":28,"end":45,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":19}},
38+
"static": false,
39+
"key": {
40+
"type": "Identifier",
41+
"start":37,"end":40,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":14},"identifierName":"bar"},
42+
"name": "bar"
43+
},
44+
"computed": false,
45+
"value": {
46+
"type": "Literal",
47+
"start":43,"end":44,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":18}},
48+
"value": 0,
49+
"raw": "0"
50+
}
51+
},
52+
{
53+
"type": "ClassAccessorProperty",
54+
"start":48,"end":62,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":16}},
55+
"static": false,
56+
"key": {
57+
"type": "PrivateName",
58+
"start":57,"end":61,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":15}},
59+
"id": {
60+
"type": "Identifier",
61+
"start":58,"end":61,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":15},"identifierName":"qux"},
62+
"name": "qux"
63+
}
64+
},
65+
"computed": false,
66+
"value": null
67+
}
68+
]
69+
}
70+
}
71+
]
72+
}
73+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A {
2+
accessor foo;
3+
accessor bar = 0;
4+
accessor #qux;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["decoratorAutoAccessors", ["estree", { "classFeatures": true }]]
3+
}

0 commit comments

Comments
 (0)