@@ -20,15 +20,33 @@ export abstract class NodeAdapter extends Node {
20
20
21
21
abstract getPosition ( property ?: string ) : Position | undefined ;
22
22
23
- abstract getRole ( ) : string | undefined ;
23
+ getRole ( ) : string | symbol | undefined {
24
+ if ( this . parent ) { // Inefficient default implementation, searching in the parent's children
25
+ const props = this . parent . nodeDefinition ?. properties || { } ;
26
+ for ( const p in props ) {
27
+ const prop = props [ p ] ;
28
+ if ( prop . child ) {
29
+ if ( prop . multiple ) {
30
+ if ( this . parent . getChildren ( prop . name ) ?. find ( c => c . equals ( this ) ) ) {
31
+ return prop . name ;
32
+ }
33
+ } else if ( this . equals ( this . parent . getChild ( prop . name ) as NodeAdapter ) ) {
34
+ return prop . name ;
35
+ }
36
+ }
37
+ }
38
+ } else {
39
+ return undefined ;
40
+ }
41
+ }
24
42
25
43
abstract isDeclaration ( ) : boolean ;
26
44
27
45
abstract isExpression ( ) : boolean ;
28
46
29
47
abstract isStatement ( ) : boolean ;
30
48
31
- equals ( other : NodeAdapter ) {
49
+ equals ( other : NodeAdapter | undefined ) {
32
50
return other == this ;
33
51
}
34
52
}
@@ -84,10 +102,6 @@ export class AugmentedNode extends NodeAdapter {
84
102
return this . node . position ;
85
103
}
86
104
87
- getRole ( ) : string | undefined {
88
- return undefined ;
89
- }
90
-
91
105
isDeclaration ( ) : boolean {
92
106
return false ;
93
107
}
@@ -99,13 +113,17 @@ export class AugmentedNode extends NodeAdapter {
99
113
isStatement ( ) : boolean {
100
114
return false ;
101
115
}
116
+
117
+ equals ( other : NodeAdapter | undefined ) : boolean {
118
+ return other instanceof AugmentedNode && this . node == other . node ;
119
+ }
102
120
}
103
121
104
122
export abstract class TraceNode extends Node {
105
123
106
124
abstract parent ?: TraceNode ;
107
125
108
- protected constructor ( public wrappedNode : NodeAdapter ) {
126
+ protected constructor ( public nodeAdapter : NodeAdapter ) {
109
127
super ( ) ;
110
128
}
111
129
@@ -121,28 +139,28 @@ export abstract class TraceNode extends Node {
121
139
return this . nodeDefinition . name ! ;
122
140
}
123
141
124
- getRole ( ) : string | undefined {
125
- return this . wrappedNode . getRole ( ) ;
142
+ getRole ( ) : string | symbol | undefined {
143
+ return this . nodeAdapter . getRole ( ) ;
126
144
}
127
145
128
146
getPosition ( ) : Position | undefined {
129
- return this . wrappedNode . getPosition ( ) ;
147
+ return this . nodeAdapter . getPosition ( ) ;
130
148
}
131
149
132
150
get position ( ) : Position | undefined {
133
151
return this . getPosition ( ) ;
134
152
}
135
153
136
154
get nodeDefinition ( ) : NodeDefinition {
137
- return this . wrappedNode . nodeDefinition ;
155
+ return this . nodeAdapter . nodeDefinition ;
138
156
}
139
157
140
158
getAttributes ( ) : { [ name : string ] : any } {
141
- return this . wrappedNode . getAttributes ( ) ;
159
+ return this . nodeAdapter . getAttributes ( ) ;
142
160
}
143
161
144
162
doGetAttribute ( attrName : string ) : any {
145
- return this . wrappedNode . getAttribute ( attrName ) ;
163
+ return this . nodeAdapter . getAttribute ( attrName ) ;
146
164
}
147
165
148
166
getPathFromRoot ( ) : ( string | number ) [ ] {
@@ -167,18 +185,18 @@ export abstract class TraceNode extends Node {
167
185
}
168
186
169
187
equals ( node : TraceNode ) {
170
- return node === this || node . wrappedNode . equals ( this . wrappedNode ) ;
188
+ return node === this || node . nodeAdapter . equals ( this . nodeAdapter ) ;
171
189
}
172
190
173
191
isDeclaration ( ) : boolean {
174
- return this . wrappedNode . isDeclaration ( ) ;
192
+ return this . nodeAdapter . isDeclaration ( ) ;
175
193
}
176
194
177
195
isExpression ( ) : boolean {
178
- return this . wrappedNode . isExpression ( ) ;
196
+ return this . nodeAdapter . isExpression ( ) ;
179
197
}
180
198
181
199
isStatement ( ) : boolean {
182
- return this . wrappedNode . isStatement ( ) ;
200
+ return this . nodeAdapter . isStatement ( ) ;
183
201
}
184
202
}
0 commit comments