Skip to content

Commit b7269a6

Browse files
committed
fix printer
1 parent 33f8e7f commit b7269a6

25 files changed

+230
-246
lines changed

jscomp/ext/ext_ident.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ let is_exotic name =
138138
| _ -> false
139139

140140
let unwrap_exotic name =
141-
let len = String.length name in
142-
String.sub name 2 (len - 3)
141+
if is_exotic name then
142+
let len = String.length name in
143+
String.sub name 2 (len - 3)
144+
else name
143145

144146
exception Not_normal_letter of int
145147
let name_mangle name =

jscomp/frontend/ast_external_process.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,8 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
316316
| Pexp_constant (Pconst_string (s, _)) -> (
317317
match l.txt with
318318
| Longident.Lident "type_" -> Some ("type", s)
319-
| Longident.Lident name when Ext_ident.is_exotic name
320-
->
319+
| Longident.Lident name ->
321320
Some (Ext_ident.unwrap_exotic name, s)
322-
| Longident.Lident name -> Some (name, s)
323321
| _ ->
324322
Location.raise_errorf ~loc:exp.pexp_loc
325323
"Field must be a regular key.")

jscomp/syntax/src/res_outcome_printer.ml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ let classifyIdentContent ~allowUident txt =
6060
in
6161
if Token.isKeywordTxt txt then ExoticIdent else go 0
6262

63-
let printIdentLike ~allowUident txt =
64-
match classifyIdentContent ~allowUident txt with
65-
| ExoticIdent -> Doc.concat [Doc.text "\\\""; Doc.text txt; Doc.text "\""]
66-
| NormalIdent -> Doc.text txt
67-
6863
let printPolyVarIdent txt =
6964
(* numeric poly-vars don't need quotes: #644 *)
7065
if isValidNumericPolyvarNumber txt then Doc.text txt
@@ -117,9 +112,9 @@ let escapeStringContents s =
117112
print_ident fmt id2;
118113
Format.pp_print_char fmt ')' *)
119114

120-
let rec printOutIdentDoc ?(allowUident = true) (ident : Outcometree.out_ident) =
115+
let rec printOutIdentDoc (ident : Outcometree.out_ident) =
121116
match ident with
122-
| Oide_ident s -> printIdentLike ~allowUident s
117+
| Oide_ident s -> Doc.text s
123118
| Oide_dot (ident, s) ->
124119
Doc.concat [printOutIdentDoc ident; Doc.dot; Doc.text s]
125120
| Oide_apply (call, arg) ->
@@ -188,9 +183,7 @@ let rec printOutTypeDoc (outType : Outcometree.out_type) =
188183
[
189184
Doc.space;
190185
Doc.join ~sep:Doc.space
191-
(List.map
192-
(fun lbl -> printIdentLike ~allowUident:true lbl)
193-
tags);
186+
(List.map (fun lbl -> Doc.text lbl) tags);
194187
]));
195188
Doc.softLine;
196189
Doc.rbracket;
@@ -220,7 +213,7 @@ let rec printOutTypeDoc (outType : Outcometree.out_type) =
220213
| Otyp_constr (Oide_ident "function$", [Otyp_var _; _arity]) ->
221214
(* function$<'a, arity> -> _ => _ *)
222215
printOutTypeDoc (Otyp_stuff "_ => _")
223-
| Otyp_constr (outIdent, []) -> printOutIdentDoc ~allowUident:false outIdent
216+
| Otyp_constr (outIdent, []) -> printOutIdentDoc outIdent
224217
| Otyp_manifest (typ1, typ2) ->
225218
Doc.concat [printOutTypeDoc typ1; Doc.text " = "; printOutTypeDoc typ2]
226219
| Otyp_record record -> printRecordDeclarationDoc ~inline:true record
@@ -530,7 +523,7 @@ and printRecordDeclRowDoc (name, mut, opt, arg) =
530523
(Doc.concat
531524
[
532525
(if mut then Doc.text "mutable " else Doc.nil);
533-
printIdentLike ~allowUident:false name;
526+
Doc.text name;
534527
(if opt then Doc.text "?" else Doc.nil);
535528
Doc.text ": ";
536529
printOutTypeDoc arg;
@@ -733,7 +726,7 @@ let rec printOutSigItemDoc ?(printNameAsIs = false)
733726
attrs;
734727
kw;
735728
(if printNameAsIs then Doc.text outTypeDecl.otype_name
736-
else printIdentLike ~allowUident:false outTypeDecl.otype_name);
729+
else Doc.text outTypeDecl.otype_name);
737730
typeParams;
738731
kind;
739732
]);
@@ -865,7 +858,7 @@ and printOutExtensionConstructorDoc
865858
(Doc.concat
866859
[
867860
Doc.text "type ";
868-
printIdentLike ~allowUident:false outExt.oext_type_name;
861+
Doc.text outExt.oext_type_name;
869862
typeParams;
870863
Doc.text " += ";
871864
Doc.line;
@@ -904,7 +897,7 @@ and printOutTypeExtensionDoc (typeExtension : Outcometree.out_type_extension) =
904897
(Doc.concat
905898
[
906899
Doc.text "type ";
907-
printIdentLike ~allowUident:false typeExtension.otyext_name;
900+
Doc.text typeExtension.otyext_name;
908901
typeParams;
909902
Doc.text " += ";
910903
(if typeExtension.otyext_private = Asttypes.Private then

jscomp/syntax/src/res_printer.ml

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,6 @@ let classifyIdentContent ?(allowUident = false) ?(allowHyphen = false) txt =
398398
in
399399
loop 0
400400

401-
let printIdentLike ?allowUident ?allowHyphen txt =
402-
match classifyIdentContent ?allowUident ?allowHyphen txt with
403-
| ExoticIdent -> Doc.concat [Doc.text "\\\""; Doc.text txt; Doc.text "\""]
404-
| NormalIdent -> Doc.text txt
405-
406401
let rec unsafe_for_all_range s ~start ~finish p =
407402
start > finish
408403
|| p (String.unsafe_get s start)
@@ -433,7 +428,7 @@ let printPolyVarIdent txt =
433428
if isValidNumericPolyvarNumber txt then Doc.text txt
434429
else
435430
match classifyIdentContent ~allowUident:true txt with
436-
| ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""]
431+
| ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text (Ext_ident.unwrap_exotic txt); Doc.text "\""]
437432
| NormalIdent -> (
438433
match txt with
439434
| "" -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""]
@@ -453,7 +448,7 @@ let printLident l =
453448
flat [] lid
454449
in
455450
match l with
456-
| Longident.Lident txt -> printIdentLike txt
451+
| Longident.Lident txt -> Doc.text txt
457452
| Longident.Ldot (path, txt) ->
458453
let doc =
459454
match flatLidOpt path with
@@ -462,7 +457,7 @@ let printLident l =
462457
[
463458
Doc.join ~sep:Doc.dot (List.map Doc.text txts);
464459
Doc.dot;
465-
printIdentLike txt;
460+
Doc.text txt;
466461
]
467462
| None -> Doc.text "printLident: Longident.Lapply is not supported"
468463
in
@@ -484,7 +479,7 @@ let printIdentPath path cmtTbl =
484479
printComments doc cmtTbl path.loc
485480

486481
let printStringLoc sloc cmtTbl =
487-
let doc = printIdentLike sloc.Location.txt in
482+
let doc = Doc.text sloc.Location.txt in
488483
printComments doc cmtTbl sloc.loc
489484

490485
let printStringContents txt =
@@ -1060,7 +1055,7 @@ and printValueDescription ~state valueDescription cmtTbl =
10601055
attrs;
10611056
Doc.text header;
10621057
printComments
1063-
(printIdentLike valueDescription.pval_name.txt)
1058+
(Doc.text valueDescription.pval_name.txt)
10641059
cmtTbl valueDescription.pval_name.loc;
10651060
Doc.text ": ";
10661061
printTypExpr ~state valueDescription.pval_type cmtTbl;
@@ -1197,7 +1192,7 @@ and printTypeDeclaration ~state ~name ~equalSign ~recFlag i
11971192
and printTypeDeclaration2 ~state ~recFlag (td : Parsetree.type_declaration)
11981193
cmtTbl i =
11991194
let name =
1200-
let doc = printIdentLike td.Parsetree.ptype_name.txt in
1195+
let doc = Doc.text td.Parsetree.ptype_name.txt in
12011196
printComments doc cmtTbl td.ptype_name.loc
12021197
in
12031198
let equalSign = "=" in
@@ -1502,7 +1497,7 @@ and printLabelDeclaration ~state (ld : Parsetree.label_declaration) cmtTbl =
15021497
let name, isDot =
15031498
let doc, isDot =
15041499
if ld.pld_name.txt = "..." then (Doc.text ld.pld_name.txt, true)
1505-
else (printIdentLike ld.pld_name.txt, false)
1500+
else (Doc.text ld.pld_name.txt, false)
15061501
in
15071502
(printComments doc cmtTbl ld.pld_name.loc, isDot)
15081503
in
@@ -1603,7 +1598,7 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
16031598
match typExpr.ptyp_desc with
16041599
| Ptyp_any -> Doc.text "_"
16051600
| Ptyp_var var ->
1606-
Doc.concat [Doc.text "'"; printIdentLike ~allowUident:true var]
1601+
Doc.concat [Doc.text "'"; Doc.text var]
16071602
| Ptyp_extension extension ->
16081603
printExtension ~state ~atModuleLvl:false extension cmtTbl
16091604
| Ptyp_alias (typ, alias) ->
@@ -1622,7 +1617,7 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
16221617
if needsParens then Doc.concat [Doc.lparen; doc; Doc.rparen] else doc
16231618
in
16241619
Doc.concat
1625-
[typ; Doc.text " as "; Doc.concat [Doc.text "'"; printIdentLike alias]]
1620+
[typ; Doc.text " as "; Doc.concat [Doc.text "'"; Doc.text alias]]
16261621
(* object printings *)
16271622
| Ptyp_object (fields, openFlag) ->
16281623
printObject ~state ~inline:false fields openFlag cmtTbl
@@ -1879,9 +1874,9 @@ and printTypeParameter ~state (attrs, lbl, typ) cmtTbl =
18791874
match lbl with
18801875
| Asttypes.Nolabel -> Doc.nil
18811876
| Labelled lbl ->
1882-
Doc.concat [Doc.text "~"; printIdentLike lbl; Doc.text ": "]
1877+
Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
18831878
| Optional lbl ->
1884-
Doc.concat [Doc.text "~"; printIdentLike lbl; Doc.text ": "]
1879+
Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
18851880
in
18861881
let optionalIndicator =
18871882
match lbl with
@@ -2118,7 +2113,7 @@ and printExtension ~state ~atModuleLvl (stringLoc, payload) cmtTbl =
21182113
[
21192114
Doc.text "%";
21202115
(if atModuleLvl then Doc.text "%" else Doc.nil);
2121-
Doc.text txt;
2116+
Doc.text (Ext_ident.unwrap_exotic txt);
21222117
]
21232118
in
21242119
printComments doc cmtTbl stringLoc.Location.loc
@@ -2129,7 +2124,7 @@ and printPattern ~state (p : Parsetree.pattern) cmtTbl =
21292124
let patternWithoutAttributes =
21302125
match p.ppat_desc with
21312126
| Ppat_any -> Doc.text "_"
2132-
| Ppat_var var -> printIdentLike var.txt
2127+
| Ppat_var var -> Doc.text var.txt
21332128
| Ppat_constant c ->
21342129
let templateLiteral =
21352130
ParsetreeViewer.hasTemplateLiteralAttr p.ppat_attributes
@@ -4377,9 +4372,9 @@ and printJsxProp ~state arg cmtTbl =
43774372
when lblTxt = ident (* jsx punning *) -> (
43784373
match lbl with
43794374
| Nolabel -> Doc.nil
4380-
| Labelled _lbl -> printComments (printIdentLike ident) cmtTbl argLoc
4375+
| Labelled _lbl -> printComments (Doc.text ident) cmtTbl argLoc
43814376
| Optional _lbl ->
4382-
let doc = Doc.concat [Doc.question; printIdentLike ident] in
4377+
let doc = Doc.concat [Doc.question; Doc.text ident] in
43834378
printComments doc cmtTbl argLoc)
43844379
| ( ((Asttypes.Labelled lblTxt | Optional lblTxt) as lbl),
43854380
{
@@ -4389,8 +4384,8 @@ and printJsxProp ~state arg cmtTbl =
43894384
when lblTxt = ident (* jsx punning when printing from Reason *) -> (
43904385
match lbl with
43914386
| Nolabel -> Doc.nil
4392-
| Labelled _lbl -> printIdentLike ident
4393-
| Optional _lbl -> Doc.concat [Doc.question; printIdentLike ident])
4387+
| Labelled _lbl -> Doc.text ident
4388+
| Optional _lbl -> Doc.concat [Doc.question; Doc.text ident])
43944389
| Asttypes.Labelled "_spreadProps", expr ->
43954390
let doc = printExpressionWithComments ~state expr cmtTbl in
43964391
Doc.concat [Doc.lbrace; Doc.dotdotdot; doc; Doc.rbrace]
@@ -4404,10 +4399,10 @@ and printJsxProp ~state arg cmtTbl =
44044399
let lblDoc =
44054400
match lbl with
44064401
| Asttypes.Labelled lbl ->
4407-
let lbl = printComments (printIdentLike lbl) cmtTbl argLoc in
4402+
let lbl = printComments (Doc.text lbl) cmtTbl argLoc in
44084403
Doc.concat [lbl; Doc.equal]
44094404
| Asttypes.Optional lbl ->
4410-
let lbl = printComments (printIdentLike lbl) cmtTbl argLoc in
4405+
let lbl = printComments (Doc.text lbl) cmtTbl argLoc in
44114406
Doc.concat [lbl; Doc.equal; Doc.question]
44124407
| Nolabel -> Doc.nil
44134408
in
@@ -4431,7 +4426,7 @@ and printJsxProp ~state arg cmtTbl =
44314426
* Navabar.createElement -> Navbar
44324427
* Staff.Users.createElement -> Staff.Users *)
44334428
and printJsxName {txt = lident} =
4434-
let printIdent = printIdentLike ~allowUident:true ~allowHyphen:true in
4429+
let printIdent = Doc.text in
44354430
let rec flatten acc lident =
44364431
match lident with
44374432
| Longident.Lident txt -> printIdent txt :: acc
@@ -4458,9 +4453,9 @@ and printArgumentsWithCallbackInFirstPosition ~dotted ~state args cmtTbl =
44584453
match lbl with
44594454
| Asttypes.Nolabel -> Doc.nil
44604455
| Asttypes.Labelled txt ->
4461-
Doc.concat [Doc.tilde; printIdentLike txt; Doc.equal]
4456+
Doc.concat [Doc.tilde; Doc.text txt; Doc.equal]
44624457
| Asttypes.Optional txt ->
4463-
Doc.concat [Doc.tilde; printIdentLike txt; Doc.equal; Doc.question]
4458+
Doc.concat [Doc.tilde; Doc.text txt; Doc.equal; Doc.question]
44644459
in
44654460
let callback =
44664461
Doc.concat
@@ -4538,9 +4533,9 @@ and printArgumentsWithCallbackInLastPosition ~state ~dotted args cmtTbl =
45384533
match lbl with
45394534
| Asttypes.Nolabel -> Doc.nil
45404535
| Asttypes.Labelled txt ->
4541-
Doc.concat [Doc.tilde; printIdentLike txt; Doc.equal]
4536+
Doc.concat [Doc.tilde; Doc.text txt; Doc.equal]
45424537
| Asttypes.Optional txt ->
4543-
Doc.concat [Doc.tilde; printIdentLike txt; Doc.equal; Doc.question]
4538+
Doc.concat [Doc.tilde; Doc.text txt; Doc.equal; Doc.question]
45444539
in
45454540
let callbackFitsOnOneLine =
45464541
lazy
@@ -4702,7 +4697,7 @@ and printArgument ~state (argLbl, arg) cmtTbl =
47024697
| ({Location.txt = "res.namedArgLoc"; loc}, _) :: _ -> loc
47034698
| _ -> arg.pexp_loc
47044699
in
4705-
let doc = Doc.concat [Doc.tilde; printIdentLike lbl] in
4700+
let doc = Doc.concat [Doc.tilde; Doc.text lbl] in
47064701
printComments doc cmtTbl loc
47074702
(* ~a: int (punned)*)
47084703
| ( Labelled lbl,
@@ -4726,7 +4721,7 @@ and printArgument ~state (argLbl, arg) cmtTbl =
47264721
Doc.concat
47274722
[
47284723
Doc.tilde;
4729-
printIdentLike lbl;
4724+
Doc.text lbl;
47304725
Doc.text ": ";
47314726
printTypExpr ~state typ cmtTbl;
47324727
]
@@ -4744,7 +4739,7 @@ and printArgument ~state (argLbl, arg) cmtTbl =
47444739
| ({Location.txt = "res.namedArgLoc"; loc}, _) :: _ -> loc
47454740
| _ -> arg.pexp_loc
47464741
in
4747-
let doc = Doc.concat [Doc.tilde; printIdentLike lbl; Doc.question] in
4742+
let doc = Doc.concat [Doc.tilde; Doc.text lbl; Doc.question] in
47484743
printComments doc cmtTbl loc
47494744
| _lbl, expr ->
47504745
let argLoc, expr =
@@ -4760,11 +4755,11 @@ and printArgument ~state (argLbl, arg) cmtTbl =
47604755
let doc = Doc.text "..." in
47614756
(printComments doc cmtTbl argLoc, true)
47624757
| Labelled lbl ->
4763-
let doc = Doc.concat [Doc.tilde; printIdentLike lbl; Doc.equal] in
4758+
let doc = Doc.concat [Doc.tilde; Doc.text lbl; Doc.equal] in
47644759
(printComments doc cmtTbl argLoc, false)
47654760
| Optional lbl ->
47664761
let doc =
4767-
Doc.concat [Doc.tilde; printIdentLike lbl; Doc.equal; Doc.question]
4762+
Doc.concat [Doc.tilde; Doc.text lbl; Doc.equal; Doc.question]
47684763
in
47694764
(printComments doc cmtTbl argLoc, false)
47704765
in
@@ -4898,7 +4893,7 @@ and printExprFunParameters ~state ~inCallback ~async ~uncurried ~hasConstraint
48984893
]
48994894
when not dotted ->
49004895
let txtDoc =
4901-
let var = printIdentLike stringLoc.txt in
4896+
let var = Doc.text stringLoc.txt in
49024897
let var =
49034898
match attrs with
49044899
| [] -> if hasConstraint then addParens var else var
@@ -4973,7 +4968,7 @@ and printExpFunParameter ~state parameter cmtTbl =
49734968
(List.map
49744969
(fun lbl ->
49754970
printComments
4976-
(printIdentLike lbl.Asttypes.txt)
4971+
(Doc.text lbl.Asttypes.txt)
49774972
cmtTbl lbl.Asttypes.loc)
49784973
lbls);
49794974
])
@@ -5002,7 +4997,7 @@ and printExpFunParameter ~state parameter cmtTbl =
50024997
[
50034998
printAttributes ~state ppat_attributes cmtTbl;
50044999
Doc.text "~";
5005-
printIdentLike lbl;
5000+
Doc.text lbl;
50065001
]
50075002
| ( (Asttypes.Labelled lbl | Optional lbl),
50085003
{
@@ -5015,7 +5010,7 @@ and printExpFunParameter ~state parameter cmtTbl =
50155010
[
50165011
printAttributes ~state ppat_attributes cmtTbl;
50175012
Doc.text "~";
5018-
printIdentLike lbl;
5013+
Doc.text lbl;
50195014
Doc.text ": ";
50205015
printTypExpr ~state typ cmtTbl;
50215016
]
@@ -5024,7 +5019,7 @@ and printExpFunParameter ~state parameter cmtTbl =
50245019
Doc.concat
50255020
[
50265021
Doc.text "~";
5027-
printIdentLike lbl;
5022+
Doc.text lbl;
50285023
Doc.text " as ";
50295024
printPattern ~state pattern cmtTbl;
50305025
]
@@ -5433,7 +5428,7 @@ and printAttribute ?(standalone = false) ~state
54335428
(Doc.concat
54345429
[
54355430
Doc.text (if standalone then "@@" else "@");
5436-
Doc.text id.txt;
5431+
Doc.text (Ext_ident.unwrap_exotic id.txt);
54375432
printPayload ~state payload cmtTbl;
54385433
]),
54395434
Doc.line )

0 commit comments

Comments
 (0)