Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit a3f0b44

Browse files
committed
Merge branch 'master' into react-ppx
2 parents d24c0df + ef260b2 commit a3f0b44

File tree

37 files changed

+686
-215
lines changed

37 files changed

+686
-215
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ReScript Syntax ![Tests](https://github.com/rescript-lang/syntax/workflows/CI/badge.svg)
22

3-
Blog post: https://reasonml.org/blog/bucklescript-8-1-new-syntax
3+
Blog post: https://rescript-lang.org/blog/bucklescript-8-1-new-syntax
44

55
Documentation: https://rescript-lang.org/docs/manual/latest/overview
66

src/res_cli.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,11 @@ module CliArgProcessor = struct
237237
backend.stringOfDiagnostics
238238
~source:parseResult.source
239239
~filename:parseResult.filename
240-
parseResult.diagnostics;
241-
if react then
242-
exit 1
243-
else if recover then
240+
parseResult.diagnostics;
241+
if recover then
244242
printEngine.printInterface
245243
~width ~filename ~comments:parseResult.comments parseResult.parsetree
246-
else ()
244+
else exit 1
247245
end
248246
else
249247
let parsetree =
@@ -258,12 +256,10 @@ module CliArgProcessor = struct
258256
~source:parseResult.source
259257
~filename:parseResult.filename
260258
parseResult.diagnostics;
261-
if react then
262-
exit 1
263-
else if recover then
259+
if recover then
264260
printEngine.printImplementation
265261
~width ~filename ~comments:parseResult.comments parseResult.parsetree
266-
else ()
262+
else exit 1
267263
end
268264
else
269265
let parsetree =

src/res_comments_table.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ let log t =
8585
]
8686
) |> Doc.toString ~width:80 |> print_endline
8787
[@@live]
88+
8889
let attach tbl loc comments =
8990
match comments with
9091
| [] -> ()
@@ -776,6 +777,12 @@ let rec walkStructure s t comments =
776777
partitionLeadingTrailing comments longident.loc in
777778
attach t.leading longident.loc leading;
778779
attach t.trailing longident.loc trailing;
780+
| Pexp_let (
781+
_recFlag,
782+
valueBindings,
783+
{pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, None)}
784+
) ->
785+
walkValueBindings valueBindings t comments
779786
| Pexp_let (_recFlag, valueBindings, expr2) ->
780787
let comments = visitListButContinueWithRemainingComments
781788
~getLoc:(fun n ->
@@ -1423,6 +1430,8 @@ and walkExprArgument (_argLabel, expr) t comments =
14231430
let (before, after) = partitionLeadingTrailing comments longident.loc in
14241431
attach t.leading longident.loc before;
14251432
attach t.trailing longident.loc after
1433+
| Pmod_structure [] ->
1434+
attach t.inside modExpr.pmod_loc comments
14261435
| Pmod_structure structure ->
14271436
walkStructure structure t comments
14281437
| Pmod_extension extension ->
@@ -1516,6 +1525,8 @@ and walkExprArgument (_argLabel, expr) t comments =
15161525
let (leading, trailing) = partitionLeadingTrailing comments longident.loc in
15171526
attach t.leading longident.loc leading;
15181527
attach t.trailing longident.loc trailing;
1528+
| Pmty_signature [] ->
1529+
attach t.inside modType.pmty_loc comments
15191530
| Pmty_signature signature ->
15201531
walkSignature signature t comments
15211532
| Pmty_extension extension ->

src/res_core.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ let verifyJsxOpeningClosingName p nameExpr =
618618
let closing = match p.Parser.token with
619619
| Lident lident -> Parser.next p; Longident.Lident lident
620620
| Uident _ ->
621-
(parseModuleLongIdent ~lowercase:false p).txt
621+
(parseModuleLongIdent ~lowercase:true p).txt
622622
| _ -> Longident.Lident ""
623623
in
624624
match nameExpr.Parsetree.pexp_desc with
@@ -2422,7 +2422,7 @@ and parseJsxName p =
24222422
let loc = mkLoc identStart identEnd in
24232423
Location.mkloc (Longident.Lident ident) loc
24242424
| Uident _ ->
2425-
let longident = parseModuleLongIdent ~lowercase:false p in
2425+
let longident = parseModuleLongIdent ~lowercase:true p in
24262426
Location.mkloc (Longident.Ldot (longident.txt, "createElement")) longident.loc
24272427
| _ ->
24282428
let msg = "A jsx name should start with a lowercase or uppercase identifier, like: div in <div /> or Navbar in <Navbar />"

src/res_diagnostics.ml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ let explain t =
7272
end
7373
| Expected {context; token = t} ->
7474
let hint = match context with
75-
| Some grammar -> "It signals the start of " ^ (Grammar.toString grammar)
75+
| Some grammar -> " It signals the start of " ^ (Grammar.toString grammar)
7676
| None -> ""
7777
in
78-
"Did you forget a `" ^ (Token.toString t) ^ "` here? " ^ hint
78+
"Did you forget a `" ^ (Token.toString t) ^ "` here?" ^ hint
7979
| Unexpected {token = t; context = breadcrumbs} ->
8080
let name = (Token.toString t) in
8181
begin match breadcrumbs with
@@ -142,15 +142,24 @@ let make ~startPos ~endPos category = {
142142
}
143143

144144
let printReport diagnostics src =
145+
let rec print diagnostics src =
146+
match diagnostics with
147+
| [] -> ()
148+
| d::rest ->
149+
Res_diagnostics_printing_utils.Super_location.super_error_reporter
150+
Format.err_formatter
151+
~src
152+
~startPos:d.startPos
153+
~endPos:d.endPos
154+
~msg:(explain d);
155+
begin match rest with
156+
| [] -> ()
157+
| _ -> Format.fprintf Format.err_formatter "@."
158+
end;
159+
print rest src
160+
in
145161
Format.fprintf Format.err_formatter "@[<v>";
146-
List.rev diagnostics |> List.iter (fun d ->
147-
Res_diagnostics_printing_utils.Super_location.super_error_reporter
148-
Format.err_formatter
149-
~src
150-
~startPos:d.startPos
151-
~endPos:d.endPos
152-
~msg:(explain d)
153-
);
162+
print (List.rev diagnostics) src;
154163
Format.fprintf Format.err_formatter "@]@."
155164

156165
let unexpected token context =

src/res_diagnostics_printing_utils.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ let print_loc ~normalizedRange ppf (startPos: Lexing.position) =
176176
| Some ((start_line, start_line_start_char), (end_line, end_line_end_char)) ->
177177
if start_line = end_line then
178178
if start_line_start_char = end_line_end_char then
179-
fprintf ppf " @{<dim>%i:%i@}" start_line start_line_start_char
179+
fprintf ppf ":@{<dim>%i:%i@}" start_line start_line_start_char
180180
else
181-
fprintf ppf " @{<dim>%i:%i-%i@}" start_line start_line_start_char end_line_end_char
181+
fprintf ppf ":@{<dim>%i:%i-%i@}" start_line start_line_start_char end_line_end_char
182182
else
183-
fprintf ppf " @{<dim>%i:%i-%i:%i@}" start_line start_line_start_char end_line end_line_end_char
183+
fprintf ppf ":@{<dim>%i:%i-%i:%i@}" start_line start_line_start_char end_line end_line_end_char
184184
in
185185
fprintf ppf "@{<filename>%a@}%a" Location.print_filename startPos.pos_fname dim_loc normalizedRange
186186
;;

src/res_doc.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ let toString ~width doc =
209209
end
210210
in
211211
process ~pos:0 [] [0, Flat, doc];
212-
213-
let len = MiniBuffer.length buffer in
214-
if len > 0 && MiniBuffer.unsafe_get buffer (len - 1) != '\n' then
215-
MiniBuffer.add_char buffer '\n';
216212
MiniBuffer.contents buffer
217213

218214

src/res_minibuffer.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ let create n =
1111

1212
let contents b = Bytes.sub_string b.buffer 0 b.position
1313

14-
let unsafe_get b ofs =
15-
Bytes.unsafe_get b.buffer ofs
16-
17-
let length b = b.position
18-
1914
(* Can't be called directly, don't add to the interface *)
2015
let resize_internal b more =
2116
let len = b.length in
@@ -52,4 +47,4 @@ let flush_newline b =
5247
position := !position - 1;
5348
done;
5449
b.position <- !position;
55-
add_char b '\n'
50+
add_char b '\n'

src/res_minibuffer.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
type t
2-
val add_char : t -> char -> unit
32
val add_string : t -> string -> unit
43
val contents : t -> string
54
val create : int -> t
65
val flush_newline : t -> unit
7-
val length : t -> int
8-
val unsafe_get : t -> int -> char

src/res_printer.ml

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ let printMultilineCommentContent txt =
9090
Doc.text "*/";
9191
]
9292

93-
let printTrailingComment (nodeLoc : Location.t) comment =
93+
let printTrailingComment (prevLoc: Location.t) (nodeLoc : Location.t) comment =
9494
let singleLine = Comment.isSingleLineComment comment in
9595
let content =
9696
let txt = Comment.txt comment in
@@ -101,8 +101,7 @@ let printTrailingComment (nodeLoc : Location.t) comment =
101101
in
102102
let diff =
103103
let cmtStart = (Comment.loc comment).loc_start in
104-
let prevTokEndPos = Comment.prevTokEndPos comment in
105-
cmtStart.pos_lnum - prevTokEndPos.pos_lnum
104+
cmtStart.pos_lnum - prevLoc.loc_end.pos_lnum
106105
in
107106
let isBelow =
108107
(Comment.loc comment).loc_start.pos_lnum > nodeLoc.loc_end.pos_lnum in
@@ -224,12 +223,12 @@ let printLeadingComments node tbl loc =
224223
loop [] comments
225224

226225
let printTrailingComments node tbl loc =
227-
let rec loop acc comments =
226+
let rec loop prev acc comments =
228227
match comments with
229228
| [] -> Doc.concat (List.rev acc)
230229
| comment::comments ->
231-
let cmtDoc = printTrailingComment loc comment in
232-
loop (cmtDoc::acc) comments
230+
let cmtDoc = printTrailingComment prev loc comment in
231+
loop (Comment.loc comment) (cmtDoc::acc) comments
233232
in
234233
match Hashtbl.find tbl loc with
235234
| exception Not_found -> node
@@ -238,7 +237,7 @@ let printTrailingComments node tbl loc =
238237
(* Remove comments from tbl: Some ast nodes have the same location.
239238
* We only want to print comments once *)
240239
Hashtbl.remove tbl loc;
241-
let cmtsDoc = loop [] comments in
240+
let cmtsDoc = loop loc [] comments in
242241
Doc.concat [
243242
node;
244243
cmtsDoc;
@@ -583,6 +582,23 @@ and printModType modType cmtTbl =
583582
printAttributes ~loc:longident.loc modType.pmty_attributes cmtTbl;
584583
printLongidentLocation longident cmtTbl
585584
]
585+
| Pmty_signature [] ->
586+
let shouldBreak =
587+
modType.pmty_loc.loc_start.pos_lnum < modType.pmty_loc.loc_end.pos_lnum
588+
in
589+
Doc.breakableGroup ~forceBreak:shouldBreak (
590+
Doc.concat [
591+
Doc.lbrace;
592+
Doc.indent (
593+
Doc.concat [
594+
Doc.softLine;
595+
printCommentsInside cmtTbl modType.pmty_loc;
596+
];
597+
);
598+
Doc.softLine;
599+
Doc.rbrace;
600+
]
601+
)
586602
| Pmty_signature signature ->
587603
let signatureDoc = Doc.breakableGroup ~forceBreak:true (
588604
Doc.concat [
@@ -4678,6 +4694,23 @@ and printModExpr modExpr cmtTbl =
46784694
let doc = match modExpr.pmod_desc with
46794695
| Pmod_ident longidentLoc ->
46804696
printLongidentLocation longidentLoc cmtTbl
4697+
| Pmod_structure [] ->
4698+
let shouldBreak =
4699+
modExpr.pmod_loc.loc_start.pos_lnum < modExpr.pmod_loc.loc_end.pos_lnum
4700+
in
4701+
Doc.breakableGroup ~forceBreak:shouldBreak (
4702+
Doc.concat [
4703+
Doc.lbrace;
4704+
Doc.indent (
4705+
Doc.concat [
4706+
Doc.softLine;
4707+
printCommentsInside cmtTbl modExpr.pmod_loc;
4708+
];
4709+
);
4710+
Doc.softLine;
4711+
Doc.rbrace;
4712+
]
4713+
)
46814714
| Pmod_structure structure ->
46824715
Doc.breakableGroup ~forceBreak:true (
46834716
Doc.concat [
@@ -4970,9 +5003,9 @@ let printImplementation ~width (s: Parsetree.structure) ~comments =
49705003
(* CommentTable.log cmtTbl; *)
49715004
let doc = printStructure s cmtTbl in
49725005
(* Doc.debug doc; *)
4973-
Doc.toString ~width doc
5006+
Doc.toString ~width doc ^ "\n"
49745007

49755008
let printInterface ~width (s: Parsetree.signature) ~comments =
49765009
let cmtTbl = CommentTable.make () in
49775010
CommentTable.walkSignature s cmtTbl comments;
4978-
Doc.toString ~width (printSignature s cmtTbl)
5011+
Doc.toString ~width (printSignature s cmtTbl) ^ "\n"

tests/conversion/reason/__snapshots__/render.spec.js.snap

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ module type my_module_type = {
997997
module M: {
998998
@ocaml.doc(\\" The comment for value y. \\")
999999
let y: int
1000+
10001001
/* ... */
10011002
}
10021003
}
@@ -1237,6 +1238,65 @@ let () = switch [Color.red, Color.blue, Color.green] {
12371238
"
12381239
`;
12391240
1241+
exports[`ppx.re 1`] = `
1242+
"%graphql(
1243+
\`
1244+
query Site {
1245+
site {
1246+
siteMetadata {
1247+
title
1248+
description
1249+
siteUrl
1250+
}
1251+
}
1252+
}
1253+
\`
1254+
{taggedTemplate: false}
1255+
)
1256+
1257+
module Form = %form(
1258+
type input = {
1259+
name: string,
1260+
email: string,
1261+
message: string,
1262+
@bs.as(\\"form-name\\")
1263+
formName: string,
1264+
}
1265+
type output = input
1266+
let validators = {
1267+
name: {
1268+
strategy: OnFirstBlur,
1269+
validate: ({name, _}) =>
1270+
switch name {
1271+
| \\"\\" => Error(\\"Name is required.\\")
1272+
| name => Ok(name)
1273+
},
1274+
},
1275+
email: {
1276+
strategy: OnFirstBlur,
1277+
validate: ({email, _}) =>
1278+
switch email {
1279+
| \\"\\" => Error(\\"Email is required.\\")
1280+
| email => Ok(email)
1281+
},
1282+
},
1283+
message: {
1284+
strategy: OnFirstBlur,
1285+
validate: ({message, _}) =>
1286+
switch message {
1287+
| \\"\\" => Error(\\"Message is required.\\")
1288+
| message => Ok(message)
1289+
},
1290+
},
1291+
formName: {
1292+
strategy: OnSubmit,
1293+
validate: ({formName, _}) => Ok(formName),
1294+
},
1295+
}
1296+
)
1297+
"
1298+
`;
1299+
12401300
exports[`recursiveType.ml 1`] = `
12411301
"type rec tree = {\\"label\\": string, \\"left\\": option<tree>, \\"right\\": option<tree>}
12421302
"

0 commit comments

Comments
 (0)