-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathProperty.java
More file actions
131 lines (111 loc) · 3.47 KB
/
Property.java
File metadata and controls
131 lines (111 loc) · 3.47 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
package com.semmle.js.ast;
import com.semmle.util.data.StringUtil;
import java.util.ArrayList;
import java.util.List;
/**
* A property in an object literal or an object pattern.
*
* <p>This includes both regular properties as well as accessor properties, method properties,
* properties with computed names, and spread/rest properties.
*/
public class Property extends Node {
public static enum Kind {
/** Either a normal property or a spread/rest property. */
INIT(false),
/** Getter property. */
GET(true),
/** Setter property. */
SET(true);
public final boolean isAccessor;
private Kind(boolean isAccessor) {
this.isAccessor = isAccessor;
}
};
private final Expression key;
private final Expression value, rawValue;
private final Expression defaultValue; // only applies to property patterns
private final Kind kind;
private final boolean computed, method;
private final List<Decorator> decorators;
public Property(
SourceLocation loc,
Expression key,
Expression rawValue,
String kind,
Boolean computed,
Boolean method) {
super("Property", loc);
this.key = key;
if (rawValue instanceof AssignmentPattern) {
AssignmentPattern ap = (AssignmentPattern) rawValue;
if (ap.getLeft() == key)
rawValue =
ap =
new AssignmentPattern(
ap.getLoc(), ap.getOperator(), new NodeCopier().copy(key), ap.getRight());
this.value = ap.getLeft();
this.defaultValue = ap.getRight();
} else {
this.value = rawValue == key ? new NodeCopier().copy(rawValue) : rawValue;
this.defaultValue = null;
}
this.rawValue = rawValue;
this.kind = Kind.valueOf(StringUtil.uc(kind));
this.computed = Boolean.TRUE.equals(computed);
this.method = Boolean.TRUE.equals(method);
this.decorators = new ArrayList<Decorator>();
}
@Override
public <Q, A> A accept(Visitor<Q, A> v, Q q) {
return v.visit(this, q);
}
/**
* The key of this property; usually a {@link Literal} or an {@link Identifier}, but may be an
* arbitrary expression for properties with computed names. For spread/rest properties this method
* returns {@code null}.
*/
public Expression getKey() {
return key;
}
/** The value expression of this property. */
public Expression getValue() {
return value;
}
/** The default value of this property pattern. */
public Expression getDefaultValue() {
return defaultValue;
}
/** Is this a property pattern with a default value? */
public boolean hasDefaultValue() {
return defaultValue != null;
}
/** The kind of this property. */
public Kind getKind() {
return kind;
}
/** Is the name of this property computed? */
public boolean isComputed() {
return computed;
}
/** Is this property declared using method syntax? */
public boolean isMethod() {
return method;
}
/** Is this property declared using shorthand syntax? */
public boolean isShorthand() {
return key != null && key.getLoc().equals(value.getLoc());
}
/**
* The raw value expression of this property; if this property is a property pattern with a
* default value, this method returns an {@link AssignmentPattern}.
*/
public Expression getRawValue() {
return rawValue;
}
public void addDecorators(List<Decorator> decorators) {
this.decorators.addAll(decorators);
}
public List<Decorator> getDecorators() {
return this.decorators;
}
}