From f8026a0852af4230ad5d807a3ad6894736455782 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Sun, 26 May 2024 08:31:52 +0900 Subject: [PATCH 1/3] refactor: clarify uppercase exotic ident path --- jscomp/syntax/src/res_printer.ml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index f9d370af46..bc6823456f 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -378,7 +378,7 @@ let print_longident = function | Longident.Lident txt -> Doc.text txt | lid -> Doc.join ~sep:Doc.dot (print_longident_aux [] lid) -type identifier_style = ExoticIdent | NormalIdent +type identifier_style = UppercaseExoticIdent | ExoticIdent | NormalIdent let classify_ident_content ?(allow_uident = false) ?(allow_hyphen = false) txt = if Token.is_keyword_txt txt then ExoticIdent @@ -388,9 +388,9 @@ let classify_ident_content ?(allow_uident = false) ?(allow_hyphen = false) txt = if i == len then NormalIdent else if i == 0 then match String.unsafe_get txt i with + | '\\' -> UppercaseExoticIdent | 'A' .. 'Z' when allow_uident -> loop (i + 1) | 'a' .. 'z' | '_' -> loop (i + 1) - | '-' when allow_hyphen -> loop (i + 1) | _ -> ExoticIdent else match String.unsafe_get txt i with @@ -404,7 +404,7 @@ let print_ident_like ?allow_uident ?allow_hyphen txt = let txt = Ext_ident.unwrap_uppercase_exotic txt in match classify_ident_content ?allow_uident ?allow_hyphen txt with | ExoticIdent -> Doc.concat [Doc.text "\\\""; Doc.text txt; Doc.text "\""] - | NormalIdent -> Doc.text txt + | UppercaseExoticIdent | NormalIdent -> Doc.text txt let rec unsafe_for_all_range s ~start ~finish p = start > finish @@ -435,8 +435,14 @@ let print_poly_var_ident txt = (* numeric poly-vars don't need quotes: #644 *) if is_valid_numeric_polyvar_number txt then Doc.text txt else - let txt = Ext_ident.unwrap_uppercase_exotic txt in match classify_ident_content ~allow_uident:true txt with + | UppercaseExoticIdent -> + Doc.concat + [ + Doc.text "\""; + Doc.text (Ext_ident.unwrap_uppercase_exotic txt); + Doc.text "\""; + ] | ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""] | NormalIdent -> ( match txt with From 842fcad6d9ab2b12577779fc4adc755867177702 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Sun, 26 May 2024 22:30:13 +0900 Subject: [PATCH 2/3] remove unnecessary unwrap --- jscomp/syntax/src/res_printer.ml | 1 - 1 file changed, 1 deletion(-) diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index bc6823456f..86b9bf51c4 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -401,7 +401,6 @@ let classify_ident_content ?(allow_uident = false) ?(allow_hyphen = false) txt = loop 0 let print_ident_like ?allow_uident ?allow_hyphen txt = - let txt = Ext_ident.unwrap_uppercase_exotic txt in match classify_ident_content ?allow_uident ?allow_hyphen txt with | ExoticIdent -> Doc.concat [Doc.text "\\\""; Doc.text txt; Doc.text "\""] | UppercaseExoticIdent | NormalIdent -> Doc.text txt From b0c6f1e70d544e235e611f11078e76ba8428ee26 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Mon, 27 May 2024 17:34:08 +0900 Subject: [PATCH 3/3] use `String.sub` intead of `unwrap_uppercase_exotic` --- jscomp/syntax/src/res_printer.ml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index 86b9bf51c4..109a053bca 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -436,12 +436,10 @@ let print_poly_var_ident txt = else match classify_ident_content ~allow_uident:true txt with | UppercaseExoticIdent -> - Doc.concat - [ - Doc.text "\""; - Doc.text (Ext_ident.unwrap_uppercase_exotic txt); - Doc.text "\""; - ] + let len = String.length txt in + (* UppercaseExoticIdent follows the \"..." format, + so removing the leading backslash is enough to transform it into polyvar style *) + Doc.text ((String.sub [@doesNotRaise]) txt 1 (len - 1)) | ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""] | NormalIdent -> ( match txt with