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

Parse <Foo.bar /> elements #125

Merged
merged 2 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ let verifyJsxOpeningClosingName p nameExpr =
let closing = match p.Parser.token with
| Lident lident -> Parser.next p; Longident.Lident lident
| Uident _ ->
(parseModuleLongIdent ~lowercase:false p).txt
(parseModuleLongIdent ~lowercase:true p).txt
| _ -> Longident.Lident ""
in
match nameExpr.Parsetree.pexp_desc with
Expand Down Expand Up @@ -2422,7 +2422,7 @@ and parseJsxName p =
let loc = mkLoc identStart identEnd in
Location.mkloc (Longident.Lident ident) loc
| Uident _ ->
let longident = parseModuleLongIdent ~lowercase:false p in
let longident = parseModuleLongIdent ~lowercase:true p in
Location.mkloc (Longident.Ldot (longident.txt, "createElement")) longident.loc
| _ ->
let msg = "A jsx name should start with a lowercase or uppercase identifier, like: div in <div /> or Navbar in <Navbar />"
Expand Down
76 changes: 76 additions & 0 deletions tests/parsing/errors/expressions/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,82 @@ module LicenseList = struct end
I'm not sure what to parse here when looking at \\"}\\".


========================================================"
`;

exports[`jsx.js 1`] = `
"=====Parsetree==========================================
let x = ((di ~children:[] ())[@JSX ]) - (v / ([%rescript.exprhole ]))
let x = ((Unclosed.createElement ~children:[] ())[@JSX ])
let x =
((Foo.Bar.createElement ~children:[] ())[@JSX ]) > ([%rescript.exprhole ])
let x =
((Foo.Bar.Baz.createElement ~children:[] ())[@JSX ]) >
([%rescript.exprhole ])
let x =
((Foo.bar.createElement ~children:[] ())[@JSX ]) > ([%rescript.exprhole ])
let x =
((Foo.bar.createElement ~baz:((baz)[@ns.namedArgLoc ]) ~children:[] ())
[@JSX ])
=====Errors=============================================

Syntax error!
parsing/errors/expressions/jsx.js 1:12
1 │ let x = <di-v />
2 │ let x = <Unclosed >;
3 │ let x = <Foo.Bar></Free.Will>;

I'm not sure what to parse here when looking at \\"-\\".

Syntax error!
parsing/errors/expressions/jsx.js 2:20
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>

Did you forget a \`</\` here?

Syntax error!
parsing/errors/expressions/jsx.js 3:9-28
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>

Missing </Foo.Bar>

Syntax error!
parsing/errors/expressions/jsx.js 4:9-34
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 />

Missing </Foo.Bar.Baz>

Syntax error!
parsing/errors/expressions/jsx.js 5:9-27
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 />
7 │

Missing </Foo.bar>

Syntax error!
parsing/errors/expressions/jsx.js 6:17
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
5 │ let x = <Foo.bar> </Foo.baz>
6 │ let x = <Foo.bar.baz />
7 │

I'm not sure what to parse here when looking at \\".\\".


========================================================"
`;

Expand Down
6 changes: 6 additions & 0 deletions tests/parsing/errors/expressions/jsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let x = <di-v />
let x = <Unclosed >;
let x = <Foo.Bar></Free.Will>;
let x = <Foo.Bar.Baz></Foo.Bar.Boo>
let x = <Foo.bar> </Foo.baz>
let x = <Foo.bar.baz />
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,42 @@ let _ =
~children:[] ())
[@JSX ])
let _ = ((Navbar.createElement ~children:[] ())[@JSX ])
let _ = ((Nav.Navbar.createElement ~children:[] ())[@JSX ])
let _ = ((Navbar.createElement ~children:[] ())[@JSX ])
let _ = ((Navbar.createElement ~children:[] ())[@JSX ])
let _ =
((Navbar.createElement ~className:((\\"menu\\")[@ns.namedArgLoc ]) ~children:[]
())
[@JSX ])
let _ = ((Dot.Up.createElement ~children:[] ())[@JSX ])
let _ = ((Dot.Up.createElement ~children:[] ())[@JSX ])
let _ = ((Dot.Up.createElement ~children:[] ())[@JSX ])
let _ =
((Dot.Up.createElement
~children:[((Dot.low.createElement ~children:[] ())[@JSX ])] ())
[@JSX ])
let _ =
((Dot.Up.createElement
~children:[((Dot.Up.createElement ~children:[] ())[@JSX ])] ())
[@JSX ])
let _ =
((Dot.Up.createElement ~className:((\\"menu\\")[@ns.namedArgLoc ]) ~children:[]
())
[@JSX ])
let _ = ((Dot.low.createElement ~children:[] ())[@JSX ])
let _ = ((Dot.low.createElement ~children:[] ())[@JSX ])
let _ = ((Dot.low.createElement ~children:[] ())[@JSX ])
let _ =
((Dot.low.createElement
~children:[((Dot.Up.createElement ~children:[] ())[@JSX ])] ())
[@JSX ])
let _ =
((Dot.low.createElement
~children:[((Dot.low.createElement ~children:[] ())[@JSX ])] ())
[@JSX ])
let _ =
((Dot.low.createElement ~className:((\\"menu\\")[@ns.namedArgLoc ])
~children:[] ())
[@JSX ])
let _ = ((el ~punned:((punned)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
let _ = ((el ?punned:((punned)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
let _ = ((el ~punned:((punned)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
Expand Down
18 changes: 17 additions & 1 deletion tests/parsing/grammar/expressions/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,24 @@ let _ = <div className="menu"></div>
let _ = <div className="menu" onClick={_ => Js.log("click")}> </div>
let _ = <div className="menu" onClick={_ => Js.log("click")}></div>

let _ = <Navbar />
let _ = <Navbar> </Navbar>
let _ = <Nav.Navbar> </Nav.Navbar>
let _ = <Navbar></Navbar>
let _ = <Navbar className="menu"> </Navbar>

let _ = <Dot.Up />
let _ = <Dot.Up> </Dot.Up>
let _ = <Dot.Up></Dot.Up>
let _ = <Dot.Up><Dot.low /></Dot.Up>
let _ = <Dot.Up><Dot.Up /></Dot.Up>
let _ = <Dot.Up className="menu"> </Dot.Up>

let _ = <Dot.low />
let _ = <Dot.low> </Dot.low>
let _ = <Dot.low></Dot.low>
let _ = <Dot.low><Dot.Up /></Dot.low>
let _ = <Dot.low><Dot.low /></Dot.low>
let _ = <Dot.low className="menu"> </Dot.low>

// punning
let _ = <el punned> </el>
Expand Down
2 changes: 2 additions & 0 deletions tests/printer/expr/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2420,6 +2420,8 @@ exports[`jsx.js 1`] = `
let x = <Foo className=\\"container\\" />
let x = <Foo.Bar className=\\"container\\" />
let x = <Foo.Bar.Baz className=\\"container\\" />
let x = <Foo.bar className=\\"container\\" />
let x = <Foo.baz className=\\"multiline\\" />

let x =
<div
Expand Down
4 changes: 4 additions & 0 deletions tests/printer/expr/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ let x = <div className="container" className2="container2" className3="container
let x = <Foo className="container" />
let x = <Foo.Bar className="container" />
let x = <Foo.Bar.Baz className="container" />
let x = <Foo.bar className="container" />
let x = <Foo.baz
className="multiline"
/>


let x =
Expand Down