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

Commit 8f78fcb

Browse files
authored
Parse <Foo.bar /> elements (#125)
* Parse <Foo.bar /> elements * jsx: add parse, print and error tests
1 parent 85b332c commit 8f78fcb

File tree

7 files changed

+143
-4
lines changed

7 files changed

+143
-4
lines changed

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 />"

tests/parsing/errors/expressions/__snapshots__/parse.spec.js.snap

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,82 @@ module LicenseList = struct end
324324
I'm not sure what to parse here when looking at \\"}\\".
325325
326326
327+
========================================================"
328+
`;
329+
330+
exports[`jsx.js 1`] = `
331+
"=====Parsetree==========================================
332+
let x = ((di ~children:[] ())[@JSX ]) - (v / ([%rescript.exprhole ]))
333+
let x = ((Unclosed.createElement ~children:[] ())[@JSX ])
334+
let x =
335+
((Foo.Bar.createElement ~children:[] ())[@JSX ]) > ([%rescript.exprhole ])
336+
let x =
337+
((Foo.Bar.Baz.createElement ~children:[] ())[@JSX ]) >
338+
([%rescript.exprhole ])
339+
let x =
340+
((Foo.bar.createElement ~children:[] ())[@JSX ]) > ([%rescript.exprhole ])
341+
let x =
342+
((Foo.bar.createElement ~baz:((baz)[@ns.namedArgLoc ]) ~children:[] ())
343+
[@JSX ])
344+
=====Errors=============================================
345+
346+
Syntax error!
347+
parsing/errors/expressions/jsx.js 1:12
348+
1 │ let x = <di-v />
349+
2 │ let x = <Unclosed >;
350+
3 │ let x = <Foo.Bar></Free.Will>;
351+
352+
I'm not sure what to parse here when looking at \\"-\\".
353+
354+
Syntax error!
355+
parsing/errors/expressions/jsx.js 2:20
356+
1 │ let x = <di-v />
357+
2 │ let x = <Unclosed >;
358+
3 │ let x = <Foo.Bar></Free.Will>;
359+
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
360+
361+
Did you forget a \`</\` here?
362+
363+
Syntax error!
364+
parsing/errors/expressions/jsx.js 3:9-28
365+
1 │ let x = <di-v />
366+
2 │ let x = <Unclosed >;
367+
3 │ let x = <Foo.Bar></Free.Will>;
368+
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
369+
5 │ let x = <Foo.bar> </Foo.baz>
370+
371+
Missing </Foo.Bar>
372+
373+
Syntax error!
374+
parsing/errors/expressions/jsx.js 4:9-34
375+
2 │ let x = <Unclosed >;
376+
3 │ let x = <Foo.Bar></Free.Will>;
377+
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
378+
5 │ let x = <Foo.bar> </Foo.baz>
379+
6 │ let x = <Foo.bar.baz />
380+
381+
Missing </Foo.Bar.Baz>
382+
383+
Syntax error!
384+
parsing/errors/expressions/jsx.js 5:9-27
385+
3 │ let x = <Foo.Bar></Free.Will>;
386+
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
387+
5 │ let x = <Foo.bar> </Foo.baz>
388+
6 │ let x = <Foo.bar.baz />
389+
7 │
390+
391+
Missing </Foo.bar>
392+
393+
Syntax error!
394+
parsing/errors/expressions/jsx.js 6:17
395+
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
396+
5 │ let x = <Foo.bar> </Foo.baz>
397+
6 │ let x = <Foo.bar.baz />
398+
7 │
399+
400+
I'm not sure what to parse here when looking at \\".\\".
401+
402+
327403
========================================================"
328404
`;
329405
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let x = <di-v />
2+
let x = <Unclosed >;
3+
let x = <Foo.Bar></Free.Will>;
4+
let x = <Foo.Bar.Baz></Foo.Bar.Boo>
5+
let x = <Foo.bar> </Foo.baz>
6+
let x = <Foo.bar.baz />

tests/parsing/grammar/expressions/__snapshots__/parse.spec.js.snap

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,42 @@ let _ =
541541
~children:[] ())
542542
[@JSX ])
543543
let _ = ((Navbar.createElement ~children:[] ())[@JSX ])
544-
let _ = ((Nav.Navbar.createElement ~children:[] ())[@JSX ])
544+
let _ = ((Navbar.createElement ~children:[] ())[@JSX ])
545+
let _ = ((Navbar.createElement ~children:[] ())[@JSX ])
546+
let _ =
547+
((Navbar.createElement ~className:((\\"menu\\")[@ns.namedArgLoc ]) ~children:[]
548+
())
549+
[@JSX ])
550+
let _ = ((Dot.Up.createElement ~children:[] ())[@JSX ])
551+
let _ = ((Dot.Up.createElement ~children:[] ())[@JSX ])
552+
let _ = ((Dot.Up.createElement ~children:[] ())[@JSX ])
553+
let _ =
554+
((Dot.Up.createElement
555+
~children:[((Dot.low.createElement ~children:[] ())[@JSX ])] ())
556+
[@JSX ])
557+
let _ =
558+
((Dot.Up.createElement
559+
~children:[((Dot.Up.createElement ~children:[] ())[@JSX ])] ())
560+
[@JSX ])
561+
let _ =
562+
((Dot.Up.createElement ~className:((\\"menu\\")[@ns.namedArgLoc ]) ~children:[]
563+
())
564+
[@JSX ])
565+
let _ = ((Dot.low.createElement ~children:[] ())[@JSX ])
566+
let _ = ((Dot.low.createElement ~children:[] ())[@JSX ])
567+
let _ = ((Dot.low.createElement ~children:[] ())[@JSX ])
568+
let _ =
569+
((Dot.low.createElement
570+
~children:[((Dot.Up.createElement ~children:[] ())[@JSX ])] ())
571+
[@JSX ])
572+
let _ =
573+
((Dot.low.createElement
574+
~children:[((Dot.low.createElement ~children:[] ())[@JSX ])] ())
575+
[@JSX ])
576+
let _ =
577+
((Dot.low.createElement ~className:((\\"menu\\")[@ns.namedArgLoc ])
578+
~children:[] ())
579+
[@JSX ])
545580
let _ = ((el ~punned:((punned)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
546581
let _ = ((el ?punned:((punned)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
547582
let _ = ((el ~punned:((punned)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])

tests/parsing/grammar/expressions/jsx.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,24 @@ let _ = <div className="menu"></div>
1818
let _ = <div className="menu" onClick={_ => Js.log("click")}> </div>
1919
let _ = <div className="menu" onClick={_ => Js.log("click")}></div>
2020

21+
let _ = <Navbar />
2122
let _ = <Navbar> </Navbar>
22-
let _ = <Nav.Navbar> </Nav.Navbar>
23+
let _ = <Navbar></Navbar>
24+
let _ = <Navbar className="menu"> </Navbar>
25+
26+
let _ = <Dot.Up />
27+
let _ = <Dot.Up> </Dot.Up>
28+
let _ = <Dot.Up></Dot.Up>
29+
let _ = <Dot.Up><Dot.low /></Dot.Up>
30+
let _ = <Dot.Up><Dot.Up /></Dot.Up>
31+
let _ = <Dot.Up className="menu"> </Dot.Up>
32+
33+
let _ = <Dot.low />
34+
let _ = <Dot.low> </Dot.low>
35+
let _ = <Dot.low></Dot.low>
36+
let _ = <Dot.low><Dot.Up /></Dot.low>
37+
let _ = <Dot.low><Dot.low /></Dot.low>
38+
let _ = <Dot.low className="menu"> </Dot.low>
2339

2440
// punning
2541
let _ = <el punned> </el>

tests/printer/expr/__snapshots__/render.spec.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,8 @@ exports[`jsx.js 1`] = `
24202420
let x = <Foo className=\\"container\\" />
24212421
let x = <Foo.Bar className=\\"container\\" />
24222422
let x = <Foo.Bar.Baz className=\\"container\\" />
2423+
let x = <Foo.bar className=\\"container\\" />
2424+
let x = <Foo.baz className=\\"multiline\\" />
24232425
24242426
let x =
24252427
<div

tests/printer/expr/jsx.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ let x = <div className="container" className2="container2" className3="container
22
let x = <Foo className="container" />
33
let x = <Foo.Bar className="container" />
44
let x = <Foo.Bar.Baz className="container" />
5+
let x = <Foo.bar className="container" />
6+
let x = <Foo.baz
7+
className="multiline"
8+
/>
59

610

711
let x =

0 commit comments

Comments
 (0)