Skip to content

Commit 8c8653f

Browse files
committed
More robust jsx self-closing test.
Does not rely on the code to be formatted in order to determine if there are children.
1 parent f7955fe commit 8c8653f

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

analysis/src/SemanticTokens.ml

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -225,27 +225,33 @@ let parser ~debug ~emitter ~path =
225225
Component names:
226226
- handled like other Longitent.t, except lowercase id is marked Token.JsxLowercase
227227
*)
228-
let rec isSelfClosing args =
229-
match args with
230-
| [] -> false
231-
| [
232-
( Asttypes.Labelled "children",
233-
{
234-
Parsetree.pexp_desc =
235-
Pexp_construct ({txt = Longident.Lident "[]"}, None);
236-
} );
237-
_;
238-
] ->
239-
true
240-
| _ :: rest -> isSelfClosing rest
241-
in
242-
emitter (* --> <div.. *)
228+
emitter (* --> <div... *)
243229
|> emitJsxTag ~debug ~name:"<"
244230
~pos:
245231
(let pos = Utils.tupleOfLexing e.pexp_loc.loc_start in
246232
(fst pos, snd pos - 1 (* the AST skips the loc of < somehow *)));
247233
emitter |> emitJsxOpen ~lid:lident.txt ~debug ~loc:pexp_loc;
248-
(if not (isSelfClosing args) then
234+
235+
let posOfGreatherthanAfterProps =
236+
let rec loop = function
237+
| (Asttypes.Labelled "children", {Parsetree.pexp_loc = {loc_start}})
238+
:: _ ->
239+
Utils.tupleOfLexing loc_start
240+
| _ :: args -> loop args
241+
| [] -> (-1, -1)
242+
(* should not happen *)
243+
in
244+
loop args
245+
in
246+
let posOfFinalGreatherthan =
247+
let pos = Utils.tupleOfLexing e.pexp_loc.loc_end in
248+
(fst pos, snd pos - 1)
249+
in
250+
let selfClosing =
251+
fst posOfGreatherthanAfterProps == fst posOfFinalGreatherthan
252+
&& snd posOfGreatherthanAfterProps + 1 == snd posOfFinalGreatherthan
253+
in
254+
(if not selfClosing then
249255
let lineStart, colStart = Utils.tupleOfLexing pexp_loc.loc_start in
250256
let lineEnd, colEnd = Utils.tupleOfLexing pexp_loc.loc_end in
251257
let length = if lineStart = lineEnd then colEnd - colStart else 0 in
@@ -254,22 +260,10 @@ let parser ~debug ~emitter ~path =
254260
emitter
255261
|> emitJsxClose ~debug ~lid:lident.txt
256262
~pos:(lineEndWhole, colEndWhole - 1);
257-
258-
let rec emitGreatherthanAfterProps args =
259-
match args with
260-
| (Asttypes.Labelled "children", {Parsetree.pexp_loc = {loc_start}})
261-
:: _ ->
262-
emitter
263-
|> emitJsxTag ~debug ~name:">" ~pos:(Utils.tupleOfLexing loc_start)
264-
| _ :: args -> emitGreatherthanAfterProps args
265-
| [] -> ()
266-
in
267-
emitGreatherthanAfterProps args (* <foo ...props > <-- *);
263+
emitter (* <foo ...props > <-- *)
264+
|> emitJsxTag ~debug ~name:">" ~pos:posOfGreatherthanAfterProps;
268265
emitter (* <foo> ... </foo> <-- *)
269-
|> emitJsxTag ~debug ~name:">"
270-
~pos:
271-
(let pos = Utils.tupleOfLexing e.pexp_loc.loc_end in
272-
(fst pos, snd pos - 1))));
266+
|> emitJsxTag ~debug ~name:">" ~pos:posOfFinalGreatherthan));
273267

274268
let _ = args |> List.map (fun (_lbl, arg) -> mapper.expr mapper arg) in
275269
e

0 commit comments

Comments
 (0)