Skip to content

Commit 556a2b5

Browse files
authored
Fix minor issues related to class auto accessors (#360)
* Fix minor issue in setting up write context in AstToJsonConverter.VisitAccessorProperty * Add missing VisitAccessorProperty override to AstRewriter * Maintain alphabetical order in visitors
1 parent 29ccbb2 commit 556a2b5

File tree

5 files changed

+96
-85
lines changed

5 files changed

+96
-85
lines changed

src/Esprima/Utils/AstRewriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public virtual bool VisitAndConvert<T>(in NodeList<T> nodes, out NodeList<T> new
6464
return false;
6565
}
6666

67+
protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
68+
{
69+
VisitAndConvert(accessorProperty.Decorators, out var decorators);
70+
var key = VisitAndConvert(accessorProperty.Key);
71+
var value = VisitAndConvert(accessorProperty.Value, allowNull: true);
72+
73+
return accessorProperty.UpdateWith(key, value, decorators);
74+
}
75+
6776
protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
6877
{
6978
VisitAndConvert(arrayExpression.Elements, out var elements, allowNullElement: true);

src/Esprima/Utils/AstToJavascriptConverter.cs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,47 @@ public void Convert(Node node)
6767
return result;
6868
}
6969

70+
protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
71+
{
72+
if (accessorProperty.Decorators.Count > 0)
73+
{
74+
_writeContext.SetNodeProperty(nameof(accessorProperty.Decorators), static node => ref node.As<AccessorProperty>().Decorators);
75+
VisitAuxiliaryNodeList(accessorProperty.Decorators, separator: string.Empty);
76+
77+
_writeContext.ClearNodeProperty();
78+
}
79+
80+
if (accessorProperty.Static)
81+
{
82+
_writeContext.SetNodeProperty(nameof(accessorProperty.Static), static node => node.As<AccessorProperty>().Static);
83+
Writer.WriteKeyword("static", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);
84+
85+
_writeContext.ClearNodeProperty();
86+
}
87+
else
88+
{
89+
Writer.SpaceRecommendedAfterLastToken();
90+
}
91+
92+
Writer.WriteKeyword("accessor", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);
93+
94+
_writeContext.SetNodeProperty(nameof(accessorProperty.Key), static node => node.As<AccessorProperty>().Key);
95+
VisitPropertyKey(accessorProperty.Key, accessorProperty.Computed, leadingBracketFlags: TokenFlags.LeadingSpaceRecommended);
96+
97+
if (accessorProperty.Value is not null)
98+
{
99+
_writeContext.ClearNodeProperty();
100+
Writer.WritePunctuator("=", TokenFlags.InBetween | TokenFlags.SurroundingSpaceRecommended, ref _writeContext);
101+
102+
_writeContext.SetNodeProperty(nameof(accessorProperty.Value), static node => node.As<AccessorProperty>().Value);
103+
VisitRootExpression(accessorProperty.Value, RootExpressionFlags(needsBrackets: ExpressionNeedsBracketsInList(accessorProperty.Value)));
104+
}
105+
106+
Writer.WritePunctuator(";", TokenFlags.Trailing | TokenFlags.TrailingSpaceRecommended, ref _writeContext);
107+
108+
return accessorProperty;
109+
}
110+
70111
protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
71112
{
72113
_writeContext.SetNodeProperty(nameof(arrayExpression.Elements), static node => ref node.As<ArrayExpression>().Elements);
@@ -1395,45 +1436,6 @@ binaryExpression.Right is UnaryExpression rightUnaryExpression &&
13951436
return propertyDefinition;
13961437
}
13971438

1398-
protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
1399-
{
1400-
if (accessorProperty.Decorators.Count > 0)
1401-
{
1402-
_writeContext.SetNodeProperty(nameof(accessorProperty.Decorators), static node => ref node.As<AccessorProperty>().Decorators);
1403-
VisitAuxiliaryNodeList(accessorProperty.Decorators, separator: string.Empty);
1404-
1405-
_writeContext.ClearNodeProperty();
1406-
}
1407-
1408-
if (accessorProperty.Static)
1409-
{
1410-
_writeContext.SetNodeProperty(nameof(accessorProperty.Static), static node => node.As<AccessorProperty>().Static);
1411-
Writer.WriteKeyword("static", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);
1412-
}
1413-
else
1414-
{
1415-
Writer.SpaceRecommendedAfterLastToken();
1416-
}
1417-
1418-
Writer.WriteKeyword("accessor", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);
1419-
1420-
_writeContext.SetNodeProperty(nameof(accessorProperty.Key), static node => node.As<AccessorProperty>().Key);
1421-
VisitPropertyKey(accessorProperty.Key, accessorProperty.Computed, leadingBracketFlags: TokenFlags.LeadingSpaceRecommended);
1422-
1423-
if (accessorProperty.Value is not null)
1424-
{
1425-
_writeContext.ClearNodeProperty();
1426-
Writer.WritePunctuator("=", TokenFlags.InBetween | TokenFlags.SurroundingSpaceRecommended, ref _writeContext);
1427-
1428-
_writeContext.SetNodeProperty(nameof(accessorProperty.Value), static node => node.As<AccessorProperty>().Value);
1429-
VisitRootExpression(accessorProperty.Value, RootExpressionFlags(needsBrackets: ExpressionNeedsBracketsInList(accessorProperty.Value)));
1430-
}
1431-
1432-
Writer.WritePunctuator(";", TokenFlags.Trailing | TokenFlags.TrailingSpaceRecommended, ref _writeContext);
1433-
1434-
return accessorProperty;
1435-
}
1436-
14371439
protected internal override object? VisitRestElement(RestElement restElement)
14381440
{
14391441
_writeContext.SetNodeProperty(nameof(restElement.Argument), static node => node.As<RestElement>().Argument);

src/Esprima/Utils/AstToJsonConverter.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ public void Convert(Node node)
257257
}
258258
}
259259

260+
protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
261+
{
262+
using (StartNodeObject(accessorProperty))
263+
{
264+
Member("key", accessorProperty.Key);
265+
Member("computed", accessorProperty.Computed);
266+
Member("value", accessorProperty.Value);
267+
Member("kind", accessorProperty.Kind);
268+
Member("static", accessorProperty.Static);
269+
if (accessorProperty.Decorators.Count > 0)
270+
{
271+
Member("decorators", accessorProperty.Decorators);
272+
}
273+
}
274+
275+
return accessorProperty;
276+
}
277+
260278
protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
261279
{
262280
using (StartNodeObject(arrayExpression))
@@ -974,24 +992,6 @@ public ImportCompat() : base(Nodes.Import) { }
974992
return propertyDefinition;
975993
}
976994

977-
protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
978-
{
979-
using (StartNodeObject(accessorProperty))
980-
{
981-
Member("key", accessorProperty.Key);
982-
Member("computed", accessorProperty.Computed);
983-
Member("value", accessorProperty.Value);
984-
Member("kind", accessorProperty.Kind);
985-
Member("static", accessorProperty.Static);
986-
if (accessorProperty.Decorators.Count > 0)
987-
{
988-
Member("decorators", accessorProperty.Decorators);
989-
}
990-
}
991-
992-
return accessorProperty;
993-
}
994-
995995
protected internal override object? VisitRestElement(RestElement restElement)
996996
{
997997
using (StartNodeObject(restElement))

src/Esprima/Utils/AstVisitor.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ private static Exception UnsupportedNodeType(Type nodeType, [CallerMemberName] s
1313
return node.Accept(this);
1414
}
1515

16+
protected internal virtual object? VisitAccessorProperty(AccessorProperty accessorProperty)
17+
{
18+
ref readonly var decorators = ref accessorProperty.Decorators;
19+
for (var i = 0; i < decorators.Count; i++)
20+
{
21+
Visit(decorators[i]);
22+
}
23+
24+
Visit(accessorProperty.Key);
25+
26+
if (accessorProperty.Value is not null)
27+
{
28+
Visit(accessorProperty.Value);
29+
}
30+
31+
return accessorProperty;
32+
}
33+
1634
protected internal virtual object? VisitArrayExpression(ArrayExpression arrayExpression)
1735
{
1836
ref readonly var elements = ref arrayExpression.Elements;
@@ -602,24 +620,6 @@ private static Exception UnsupportedNodeType(Type nodeType, [CallerMemberName] s
602620
return propertyDefinition;
603621
}
604622

605-
protected internal virtual object? VisitAccessorProperty(AccessorProperty accessorProperty)
606-
{
607-
ref readonly var decorators = ref accessorProperty.Decorators;
608-
for (var i = 0; i < decorators.Count; i++)
609-
{
610-
Visit(decorators[i]);
611-
}
612-
613-
Visit(accessorProperty.Key);
614-
615-
if (accessorProperty.Value is not null)
616-
{
617-
Visit(accessorProperty.Value);
618-
}
619-
620-
return accessorProperty;
621-
}
622-
623623
protected internal virtual object? VisitRestElement(RestElement restElement)
624624
{
625625
Visit(restElement.Argument);

src/Esprima/Utils/AstVisitorEventSource.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Esprima.Utils;
88
/// </summary>
99
public class AstVisitorEventSource : AstVisitor
1010
{
11+
public event EventHandler<AccessorProperty>? VisitingAccessorProperty;
12+
public event EventHandler<AccessorProperty>? VisitedAccessorProperty;
1113
public event EventHandler<ArrayExpression>? VisitingArrayExpression;
1214
public event EventHandler<ArrayExpression>? VisitedArrayExpression;
1315
public event EventHandler<ArrayPattern>? VisitedArrayPattern;
@@ -112,8 +114,6 @@ public class AstVisitorEventSource : AstVisitor
112114
public event EventHandler<Property>? VisitedProperty;
113115
public event EventHandler<PropertyDefinition>? VisitingPropertyDefinition;
114116
public event EventHandler<PropertyDefinition>? VisitedPropertyDefinition;
115-
public event EventHandler<AccessorProperty>? VisitingAccessorProperty;
116-
public event EventHandler<AccessorProperty>? VisitedAccessorProperty;
117117
public event EventHandler<RestElement>? VisitingRestElement;
118118
public event EventHandler<RestElement>? VisitedRestElement;
119119
public event EventHandler<ReturnStatement>? VisitingReturnStatement;
@@ -163,6 +163,14 @@ public class AstVisitorEventSource : AstVisitor
163163
return result;
164164
}
165165

166+
protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
167+
{
168+
VisitingAccessorProperty?.Invoke(this, accessorProperty);
169+
var result = base.VisitAccessorProperty(accessorProperty);
170+
VisitedAccessorProperty?.Invoke(this, accessorProperty);
171+
return result;
172+
}
173+
166174
protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
167175
{
168176
VisitingArrayExpression?.Invoke(this, arrayExpression);
@@ -571,14 +579,6 @@ public class AstVisitorEventSource : AstVisitor
571579
return result;
572580
}
573581

574-
protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
575-
{
576-
VisitingAccessorProperty?.Invoke(this, accessorProperty);
577-
var result = base.VisitAccessorProperty(accessorProperty);
578-
VisitedAccessorProperty?.Invoke(this, accessorProperty);
579-
return result;
580-
}
581-
582582
protected internal override object? VisitRestElement(RestElement restElement)
583583
{
584584
VisitingRestElement?.Invoke(this, restElement);

0 commit comments

Comments
 (0)