Skip to content

Commit b233021

Browse files
vouillonhhugo
authored andcommitted
Make sure that the name of an identifier does not bleed on another one
1 parent 8057833 commit b233021

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

compiler/lib/js_output.ml

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,22 @@ struct
150150

151151
let current_loc = ref U
152152

153-
let on_ident = ref false
153+
let last_mapping_has_a_name = ref false
154154

155155
let output_debug_info f loc =
156156
let loc =
157157
(* We force a new mapping after an identifier, to avoid its name
158158
to bleed over other identifiers, using the current location
159159
when none is provided. *)
160160
match loc with
161-
| N when !on_ident -> !current_loc
161+
| N when !last_mapping_has_a_name -> !current_loc
162162
| _ -> loc
163163
in
164164
match loc with
165165
| N -> ()
166166
| _ ->
167167
let location_changed = Poly.(loc <> !current_loc) in
168-
(if source_map_enabled && (!on_ident || location_changed)
168+
(if source_map_enabled && (!last_mapping_has_a_name || location_changed)
169169
then
170170
match loc with
171171
| N | U | Pi { Parse_info.src = None | Some ""; _ } ->
@@ -192,45 +192,51 @@ struct
192192
PP.string f (Format.sprintf "/*<<%s>>*/" (Parse_info.to_string pi));
193193
PP.non_breaking_space f);
194194
current_loc := loc;
195-
on_ident := false
195+
last_mapping_has_a_name := false
196196

197-
let output_debug_info_ident f nm =
197+
let output_debug_info_ident f nm_opt =
198198
if source_map_enabled
199-
then (
200-
on_ident := true;
201-
push_mapping
202-
(PP.pos f)
203-
(match !current_loc with
204-
| N | U | Pi { Parse_info.src = Some "" | None; _ } ->
205-
(* Use a dummy location. It is going to be ignored anyway *)
206-
let ori_source =
207-
match hidden_location with
208-
| Source_map.Gen_Ori { ori_source; _ } -> ori_source
209-
| _ -> 0
210-
in
211-
Source_map.Gen_Ori_Name
212-
{ gen_line = -1
213-
; gen_col = -1
214-
; ori_source
215-
; ori_line = 1
216-
; ori_col = 0
217-
; ori_name = get_name_index nm
218-
}
219-
| Pi { Parse_info.src = Some file; line; col; _ } ->
220-
Source_map.Gen_Ori_Name
221-
{ gen_line = -1
222-
; gen_col = -1
223-
; ori_source = get_file_index file
224-
; ori_line = line
225-
; ori_col = col
226-
; ori_name = get_name_index nm
227-
}))
199+
then
200+
match nm_opt with
201+
| None ->
202+
(* Make sure that the name of a previous identifier does not
203+
bleed on this one. *)
204+
output_debug_info f N
205+
| Some nm ->
206+
last_mapping_has_a_name := true;
207+
push_mapping
208+
(PP.pos f)
209+
(match !current_loc with
210+
| N | U | Pi { Parse_info.src = Some "" | None; _ } ->
211+
(* Use a dummy location. It is going to be ignored anyway *)
212+
let ori_source =
213+
match hidden_location with
214+
| Source_map.Gen_Ori { ori_source; _ } -> ori_source
215+
| _ -> 0
216+
in
217+
Source_map.Gen_Ori_Name
218+
{ gen_line = -1
219+
; gen_col = -1
220+
; ori_source
221+
; ori_line = 1
222+
; ori_col = 0
223+
; ori_name = get_name_index nm
224+
}
225+
| Pi { Parse_info.src = Some file; line; col; _ } ->
226+
Source_map.Gen_Ori_Name
227+
{ gen_line = -1
228+
; gen_col = -1
229+
; ori_source = get_file_index file
230+
; ori_line = line
231+
; ori_col = col
232+
; ori_name = get_name_index nm
233+
})
228234

229235
let ident f ~kind = function
230236
| S { name = Utf8 name; var = Some v; _ } ->
231-
(match kind, Code.Var.get_name v with
232-
| `Binding, Some nm -> output_debug_info_ident f nm
233-
| `Reference, _ | `Binding, None -> ());
237+
(match kind with
238+
| `Binding -> output_debug_info_ident f (Code.Var.get_name v)
239+
| `Reference -> ());
234240
if false then PP.string f (Printf.sprintf "/* %d */" (Code.Var.idx v));
235241
PP.string f name
236242
| S { name = Utf8 name; var = None; _ } -> PP.string f name

0 commit comments

Comments
 (0)