Skip to content

Commit 3a2c2e6

Browse files
committed
#16 - refactor ComplexLiteral hierarchy: make Id property abstract and implement in derived classes
1 parent d985039 commit 3a2c2e6

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ArrayLiteral.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public partial class ArrayLiteral : ComplexLiteral
1010

1111
public IReadOnlyList<Expression> Expressions => _expressions;
1212

13+
public override string Id => Parent is AssignmentExpression assignment
14+
? assignment.Destination.Id
15+
: NullId;
16+
1317
public ArrayLiteral(List<Expression> expressions)
1418
{
1519
_expressions = expressions;

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ComplexLiteral.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexL
22

33
public abstract class ComplexLiteral : Expression
44
{
5-
public string Id => Parent is AssignmentExpression assignment
6-
? assignment.Destination.Id
7-
: $"_t{GetHashCode()}";
5+
private string? _nullId;
6+
7+
protected string NullId
8+
{
9+
get
10+
{
11+
_nullId ??= $"{GetHashCode()}";
12+
return _nullId;
13+
}
14+
}
15+
16+
public abstract string Id { get; }
817
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ public partial class ObjectLiteral : ComplexLiteral
1010

1111
public IReadOnlyList<Property> Properties => _properties;
1212

13+
public override string Id
14+
{
15+
get
16+
{
17+
if (Parent is AssignmentExpression assignment)
18+
return assignment.Destination.Id;
19+
20+
if (Parent is WithExpression{Parent:AssignmentExpression withAssignment})
21+
return withAssignment.Destination.Id;
22+
23+
return NullId;
24+
}
25+
}
26+
1327
public ObjectLiteral(List<Property> properties)
1428
{
1529
_properties = properties;

0 commit comments

Comments
 (0)