@@ -107,6 +107,11 @@ func funcDefMap(typ string) Mapping {
107107 ))
108108}
109109
110+ // useFullSpan is a set of node types that use FullSpan for positions instead of Span
111+ var useFullSpan = []nodes.Value {
112+ nodes .String ("SingleLineDocumentationCommentTrivia" ),
113+ }
114+
110115// Preprocessors is a block of AST preprocessing rules rules.
111116var Preprocessors = []Mapping {
112117 // Erase Whitespace and EndOfLine trivias.
@@ -168,7 +173,7 @@ var Preprocessors = []Mapping{
168173 Part ("_" , Obj {}),
169174 ),
170175
171- // Positional info is stored in a child node in FullSpan field.
176+ // Positional info is stored in a child node in Span field.
172177 //
173178 // This is not supported by ObjectToNode helper, and we are
174179 // too lazy to create positional node by hand.
@@ -177,22 +182,64 @@ var Preprocessors = []Mapping{
177182 // "spanStart" and "spanEnd" fields of the root node, and
178183 // ObjectToNode will pick them up later to build a proper
179184 // positional node.
185+ //
186+ // There is also a FullSpan field that includes leading/trailing
187+ // whitespaces and sometimes node tokens. We ignore this second
188+ // position for most nodes, but there are few exceptions where
189+ // we use FullSpan and ignore Span.
180190 Map (
181- Part ("_" , Obj {
182- "FullSpan" : Obj {
183- uast .KeyType : String ("TextSpan" ),
184- "Length" : Any (),
185- "Start" : Var ("start" ),
186- "End" : Var ("end" ),
191+ Part ("_" , CasesObj ("case" , Obj {}, Objs {
192+ // exceptions - use FullSpan
193+ {
194+ uast .KeyType : Check (
195+ In (useFullSpan ... ),
196+ Var ("typ" ),
197+ ),
198+ "FullSpan" : Obj {
199+ uast .KeyType : String ("TextSpan" ),
200+ "Length" : Any (),
201+ "Start" : Var ("start" ),
202+ "End" : Var ("end" ),
203+ },
204+ // TODO(dennwc): add it as a custom position field?
205+ "Span" : Any (),
187206 },
188- // TODO(dennwc): add it as a custom position field?
189- "Span" : Any (),
190- }),
191- Part ("_" , Obj {
207+ // other nodes - use Span
208+ {
209+ uast .KeyType : Check (
210+ Not (In (useFullSpan ... )),
211+ Var ("typ" ),
212+ ),
213+ "Span" : Obj {
214+ uast .KeyType : String ("TextSpan" ),
215+ "Length" : Any (),
216+ "Start" : Var ("start" ),
217+ "End" : Var ("end" ),
218+ },
219+ // TODO(dennwc): add it as a custom position field?
220+ "FullSpan" : Any (),
221+ },
222+ })),
223+ Part ("_" , CasesObj ("case" , Obj {
192224 // remap to temporary keys and let ObjectToNode to pick them up
193225 "spanStart" : Var ("start" ),
194226 "spanEnd" : Var ("end" ),
195- }),
227+ }, Objs {
228+ // exceptions
229+ {
230+ uast .KeyType : Check (
231+ In (useFullSpan ... ),
232+ Var ("typ" ),
233+ ),
234+ },
235+ // other nodes
236+ {
237+ uast .KeyType : Check (
238+ Not (In (useFullSpan ... )),
239+ Var ("typ" ),
240+ ),
241+ },
242+ })),
196243 ),
197244
198245 // Use temporary fields from the previous transform to create positional node.
@@ -379,14 +426,13 @@ var Normalizers = []Mapping{
379426 CommentNode (false , "text" , nil ),
380427 )),
381428
382- // FIXME: doesn't work
383- //MapSemantic("MultiLineCommentTrivia", uast.Comment{}, MapObj(
384- //Obj{
385- //uast.KeyToken: CommentText([2]string{"/*", "*/"}, uast.KeyToken),
386- //"IsDirective": Bool(false),
387- //},
388- //CommentNode(true, uast.KeyToken, nil),
389- //)),
429+ MapSemantic ("MultiLineCommentTrivia" , uast.Comment {}, MapObj (
430+ Obj {
431+ uast .KeyToken : CommentText ([2 ]string {"/*" , "*/" }, uast .KeyToken ),
432+ "IsDirective" : Bool (false ),
433+ },
434+ CommentNode (true , uast .KeyToken , nil ),
435+ )),
390436
391437 // TODO(dennwc): differentiate from regular comments
392438 MapSemantic ("SingleLineDocumentationCommentTrivia" , uast.Comment {}, MapObj (
0 commit comments