forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsVariableDeclaration.cs
More file actions
132 lines (112 loc) · 3.79 KB
/
Copy pathJsVariableDeclaration.cs
File metadata and controls
132 lines (112 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public class JsVariableDeclaration : JsExpression
{
public JsVariableDeclarationKind Kind { get; }
public JsDeclaration[] Declarations { get; }
public JsVariableDeclaration(JsVariableDeclarationKind kind, params JsDeclaration[] declarations)
{
Kind = kind;
Declarations = declarations;
}
protected bool Equals(JsVariableDeclaration other) =>
Kind == other.Kind && Declarations.SequenceEqual(other.Declarations);
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsVariableDeclaration) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((int) Kind * 397) ^ (Declarations != null ? Declarations.GetHashCode() : 0);
}
}
public override string ToRawString()
{
var sb = StringBuilderCache.Allocate();
sb.Append(Kind.ToString().ToLower()).Append(" ");
for (var i = 0; i < Declarations.Length; i++)
{
var dec = Declarations[i];
if (i > 0)
sb.Append(", ");
sb.Append(dec.ToRawString());
}
return StringBuilderCache.ReturnAndFree(sb);
}
public override object Evaluate(ScriptScopeContext scope)
{
foreach (var declaration in Declarations)
{
scope.ScopedParams[declaration.Id.Name] = declaration.Init?.Evaluate(scope);
}
return JsNull.Value;
}
public override Dictionary<string, object> ToJsAst()
{
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["declarations"] = Declarations.Map(x => (object)x.ToJsAst()),
["kind"] = Kind.ToString().ToLower(),
};
return to;
}
}
public enum JsVariableDeclarationKind
{
Var,
Let,
Const,
}
public class JsDeclaration : JsExpression
{
public JsIdentifier Id { get; set; }
public JsToken Init { get; set; }
public JsDeclaration(JsIdentifier id, JsToken init)
{
Id = id;
Init = init;
}
protected bool Equals(JsDeclaration other) => Equals(Id, other.Id) && Equals(Init, other.Init);
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsDeclaration) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((Id != null ? Id.GetHashCode() : 0) * 397) ^ (Init != null ? Init.GetHashCode() : 0);
}
}
public override string ToRawString() => Init != null
? $"{Id.Name} = {Init.ToRawString()}"
: Id.Name;
public override object Evaluate(ScriptScopeContext scope)
{
throw new System.NotImplementedException();
}
public override Dictionary<string, object> ToJsAst()
{
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["id"] = Id.Name,
["init"] = Init?.ToJsAst(),
};
return to;
}
}
}