Skip to content

Commit 551a172

Browse files
authored
refactor: clarify uppercase exotic ident path (#6779)
* refactor: clarify uppercase exotic ident path * remove unnecessary unwrap * use `String.sub` intead of `unwrap_uppercase_exotic`
1 parent 326fd32 commit 551a172

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

jscomp/syntax/src/res_printer.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ let print_longident = function
378378
| Longident.Lident txt -> Doc.text txt
379379
| lid -> Doc.join ~sep:Doc.dot (print_longident_aux [] lid)
380380

381-
type identifier_style = ExoticIdent | NormalIdent
381+
type identifier_style = UppercaseExoticIdent | ExoticIdent | NormalIdent
382382

383383
let classify_ident_content ?(allow_uident = false) ?(allow_hyphen = false) txt =
384384
if Token.is_keyword_txt txt then ExoticIdent
@@ -388,9 +388,9 @@ let classify_ident_content ?(allow_uident = false) ?(allow_hyphen = false) txt =
388388
if i == len then NormalIdent
389389
else if i == 0 then
390390
match String.unsafe_get txt i with
391+
| '\\' -> UppercaseExoticIdent
391392
| 'A' .. 'Z' when allow_uident -> loop (i + 1)
392393
| 'a' .. 'z' | '_' -> loop (i + 1)
393-
| '-' when allow_hyphen -> loop (i + 1)
394394
| _ -> ExoticIdent
395395
else
396396
match String.unsafe_get txt i with
@@ -401,10 +401,9 @@ let classify_ident_content ?(allow_uident = false) ?(allow_hyphen = false) txt =
401401
loop 0
402402

403403
let print_ident_like ?allow_uident ?allow_hyphen txt =
404-
let txt = Ext_ident.unwrap_uppercase_exotic txt in
405404
match classify_ident_content ?allow_uident ?allow_hyphen txt with
406405
| ExoticIdent -> Doc.concat [Doc.text "\\\""; Doc.text txt; Doc.text "\""]
407-
| NormalIdent -> Doc.text txt
406+
| UppercaseExoticIdent | NormalIdent -> Doc.text txt
408407

409408
let rec unsafe_for_all_range s ~start ~finish p =
410409
start > finish
@@ -435,8 +434,12 @@ let print_poly_var_ident txt =
435434
(* numeric poly-vars don't need quotes: #644 *)
436435
if is_valid_numeric_polyvar_number txt then Doc.text txt
437436
else
438-
let txt = Ext_ident.unwrap_uppercase_exotic txt in
439437
match classify_ident_content ~allow_uident:true txt with
438+
| UppercaseExoticIdent ->
439+
let len = String.length txt in
440+
(* UppercaseExoticIdent follows the \"..." format,
441+
so removing the leading backslash is enough to transform it into polyvar style *)
442+
Doc.text ((String.sub [@doesNotRaise]) txt 1 (len - 1))
440443
| ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""]
441444
| NormalIdent -> (
442445
match txt with

0 commit comments

Comments
 (0)