Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor issues related to class auto accessors #360

Merged
merged 3 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Esprima/Utils/AstRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public virtual bool VisitAndConvert<T>(in NodeList<T> nodes, out NodeList<T> new
return false;
}

protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
VisitAndConvert(accessorProperty.Decorators, out var decorators);
var key = VisitAndConvert(accessorProperty.Key);
var value = VisitAndConvert(accessorProperty.Value, allowNull: true);

return accessorProperty.UpdateWith(key, value, decorators);
}

protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
{
VisitAndConvert(arrayExpression.Elements, out var elements, allowNullElement: true);
Expand Down
80 changes: 41 additions & 39 deletions src/Esprima/Utils/AstToJavascriptConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ public void Convert(Node node)
return result;
}

protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
if (accessorProperty.Decorators.Count > 0)
{
_writeContext.SetNodeProperty(nameof(accessorProperty.Decorators), static node => ref node.As<AccessorProperty>().Decorators);
VisitAuxiliaryNodeList(accessorProperty.Decorators, separator: string.Empty);

_writeContext.ClearNodeProperty();
}

if (accessorProperty.Static)
{
_writeContext.SetNodeProperty(nameof(accessorProperty.Static), static node => node.As<AccessorProperty>().Static);
Writer.WriteKeyword("static", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);

_writeContext.ClearNodeProperty();
}
else
{
Writer.SpaceRecommendedAfterLastToken();
}

Writer.WriteKeyword("accessor", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);

_writeContext.SetNodeProperty(nameof(accessorProperty.Key), static node => node.As<AccessorProperty>().Key);
VisitPropertyKey(accessorProperty.Key, accessorProperty.Computed, leadingBracketFlags: TokenFlags.LeadingSpaceRecommended);

if (accessorProperty.Value is not null)
{
_writeContext.ClearNodeProperty();
Writer.WritePunctuator("=", TokenFlags.InBetween | TokenFlags.SurroundingSpaceRecommended, ref _writeContext);

_writeContext.SetNodeProperty(nameof(accessorProperty.Value), static node => node.As<AccessorProperty>().Value);
VisitRootExpression(accessorProperty.Value, RootExpressionFlags(needsBrackets: ExpressionNeedsBracketsInList(accessorProperty.Value)));
}

Writer.WritePunctuator(";", TokenFlags.Trailing | TokenFlags.TrailingSpaceRecommended, ref _writeContext);

return accessorProperty;
}

protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
{
_writeContext.SetNodeProperty(nameof(arrayExpression.Elements), static node => ref node.As<ArrayExpression>().Elements);
Expand Down Expand Up @@ -1395,45 +1436,6 @@ binaryExpression.Right is UnaryExpression rightUnaryExpression &&
return propertyDefinition;
}

protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
if (accessorProperty.Decorators.Count > 0)
{
_writeContext.SetNodeProperty(nameof(accessorProperty.Decorators), static node => ref node.As<AccessorProperty>().Decorators);
VisitAuxiliaryNodeList(accessorProperty.Decorators, separator: string.Empty);

_writeContext.ClearNodeProperty();
}

if (accessorProperty.Static)
{
_writeContext.SetNodeProperty(nameof(accessorProperty.Static), static node => node.As<AccessorProperty>().Static);
Writer.WriteKeyword("static", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);
}
else
{
Writer.SpaceRecommendedAfterLastToken();
}

Writer.WriteKeyword("accessor", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);

_writeContext.SetNodeProperty(nameof(accessorProperty.Key), static node => node.As<AccessorProperty>().Key);
VisitPropertyKey(accessorProperty.Key, accessorProperty.Computed, leadingBracketFlags: TokenFlags.LeadingSpaceRecommended);

if (accessorProperty.Value is not null)
{
_writeContext.ClearNodeProperty();
Writer.WritePunctuator("=", TokenFlags.InBetween | TokenFlags.SurroundingSpaceRecommended, ref _writeContext);

_writeContext.SetNodeProperty(nameof(accessorProperty.Value), static node => node.As<AccessorProperty>().Value);
VisitRootExpression(accessorProperty.Value, RootExpressionFlags(needsBrackets: ExpressionNeedsBracketsInList(accessorProperty.Value)));
}

Writer.WritePunctuator(";", TokenFlags.Trailing | TokenFlags.TrailingSpaceRecommended, ref _writeContext);

return accessorProperty;
}

protected internal override object? VisitRestElement(RestElement restElement)
{
_writeContext.SetNodeProperty(nameof(restElement.Argument), static node => node.As<RestElement>().Argument);
Expand Down
36 changes: 18 additions & 18 deletions src/Esprima/Utils/AstToJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,24 @@ public void Convert(Node node)
}
}

protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
using (StartNodeObject(accessorProperty))
{
Member("key", accessorProperty.Key);
Member("computed", accessorProperty.Computed);
Member("value", accessorProperty.Value);
Member("kind", accessorProperty.Kind);
Member("static", accessorProperty.Static);
if (accessorProperty.Decorators.Count > 0)
{
Member("decorators", accessorProperty.Decorators);
}
}

return accessorProperty;
}

protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
{
using (StartNodeObject(arrayExpression))
Expand Down Expand Up @@ -974,24 +992,6 @@ public ImportCompat() : base(Nodes.Import) { }
return propertyDefinition;
}

protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
using (StartNodeObject(accessorProperty))
{
Member("key", accessorProperty.Key);
Member("computed", accessorProperty.Computed);
Member("value", accessorProperty.Value);
Member("kind", accessorProperty.Kind);
Member("static", accessorProperty.Static);
if (accessorProperty.Decorators.Count > 0)
{
Member("decorators", accessorProperty.Decorators);
}
}

return accessorProperty;
}

protected internal override object? VisitRestElement(RestElement restElement)
{
using (StartNodeObject(restElement))
Expand Down
36 changes: 18 additions & 18 deletions src/Esprima/Utils/AstVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ private static Exception UnsupportedNodeType(Type nodeType, [CallerMemberName] s
return node.Accept(this);
}

protected internal virtual object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
ref readonly var decorators = ref accessorProperty.Decorators;
for (var i = 0; i < decorators.Count; i++)
{
Visit(decorators[i]);
}

Visit(accessorProperty.Key);

if (accessorProperty.Value is not null)
{
Visit(accessorProperty.Value);
}

return accessorProperty;
}

protected internal virtual object? VisitArrayExpression(ArrayExpression arrayExpression)
{
ref readonly var elements = ref arrayExpression.Elements;
Expand Down Expand Up @@ -602,24 +620,6 @@ private static Exception UnsupportedNodeType(Type nodeType, [CallerMemberName] s
return propertyDefinition;
}

protected internal virtual object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
ref readonly var decorators = ref accessorProperty.Decorators;
for (var i = 0; i < decorators.Count; i++)
{
Visit(decorators[i]);
}

Visit(accessorProperty.Key);

if (accessorProperty.Value is not null)
{
Visit(accessorProperty.Value);
}

return accessorProperty;
}

protected internal virtual object? VisitRestElement(RestElement restElement)
{
Visit(restElement.Argument);
Expand Down
20 changes: 10 additions & 10 deletions src/Esprima/Utils/AstVisitorEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Esprima.Utils;
/// </summary>
public class AstVisitorEventSource : AstVisitor
{
public event EventHandler<AccessorProperty>? VisitingAccessorProperty;
public event EventHandler<AccessorProperty>? VisitedAccessorProperty;
public event EventHandler<ArrayExpression>? VisitingArrayExpression;
public event EventHandler<ArrayExpression>? VisitedArrayExpression;
public event EventHandler<ArrayPattern>? VisitedArrayPattern;
Expand Down Expand Up @@ -112,8 +114,6 @@ public class AstVisitorEventSource : AstVisitor
public event EventHandler<Property>? VisitedProperty;
public event EventHandler<PropertyDefinition>? VisitingPropertyDefinition;
public event EventHandler<PropertyDefinition>? VisitedPropertyDefinition;
public event EventHandler<AccessorProperty>? VisitingAccessorProperty;
public event EventHandler<AccessorProperty>? VisitedAccessorProperty;
public event EventHandler<RestElement>? VisitingRestElement;
public event EventHandler<RestElement>? VisitedRestElement;
public event EventHandler<ReturnStatement>? VisitingReturnStatement;
Expand Down Expand Up @@ -163,6 +163,14 @@ public class AstVisitorEventSource : AstVisitor
return result;
}

protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
VisitingAccessorProperty?.Invoke(this, accessorProperty);
var result = base.VisitAccessorProperty(accessorProperty);
VisitedAccessorProperty?.Invoke(this, accessorProperty);
return result;
}

protected internal override object? VisitArrayExpression(ArrayExpression arrayExpression)
{
VisitingArrayExpression?.Invoke(this, arrayExpression);
Expand Down Expand Up @@ -571,14 +579,6 @@ public class AstVisitorEventSource : AstVisitor
return result;
}

protected internal override object? VisitAccessorProperty(AccessorProperty accessorProperty)
{
VisitingAccessorProperty?.Invoke(this, accessorProperty);
var result = base.VisitAccessorProperty(accessorProperty);
VisitedAccessorProperty?.Invoke(this, accessorProperty);
return result;
}

protected internal override object? VisitRestElement(RestElement restElement)
{
VisitingRestElement?.Invoke(this, restElement);
Expand Down