Skip to content

Commit edea94e

Browse files
committed
first pass at allowing hyphens in jsx tag names
1 parent 1d4910a commit edea94e

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

jscomp/syntax/src/res_core.ml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,6 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p =
26212621
| GreaterThan -> (
26222622
(* <foo a=b> bar </foo> *)
26232623
let childrenStartPos = p.Parser.startPos in
2624-
Scanner.setJsxMode p.scanner;
26252624
Parser.next p;
26262625
let spread, children = parseJsxChildren p in
26272626
let childrenEndPos = p.Parser.startPos in
@@ -2747,6 +2746,7 @@ and parseJsxProp p =
27472746
Parser.next p;
27482747
(* no punning *)
27492748
let optional = Parser.optional p Question in
2749+
Scanner.popMode p.scanner Jsx;
27502750
let attrExpr =
27512751
let e = parsePrimaryExpr ~operand:(parseAtomicExpr p) p in
27522752
{e with pexp_attributes = propLocAttr :: e.pexp_attributes}
@@ -2794,9 +2794,7 @@ and parseJsxProps p =
27942794
and parseJsxChildren p =
27952795
let rec loop p children =
27962796
match p.Parser.token with
2797-
| Token.Eof | LessThanSlash ->
2798-
Scanner.popMode p.scanner Jsx;
2799-
List.rev children
2797+
| Token.Eof | LessThanSlash -> children
28002798
| LessThan ->
28012799
(* Imagine: <div> <Navbar /> <
28022800
* is `<` the start of a jsx-child? <div …
@@ -2812,23 +2810,23 @@ and parseJsxChildren p =
28122810
else
28132811
(* LessThanSlash *)
28142812
let () = p.token <- token in
2815-
let () = Scanner.popMode p.scanner Jsx in
2816-
List.rev children
2813+
children
28172814
| token when Grammar.isJsxChildStart token ->
28182815
let () = Scanner.popMode p.scanner Jsx in
28192816
let child =
28202817
parsePrimaryExpr ~operand:(parseAtomicExpr p) ~noCall:true p
28212818
in
28222819
loop p (child :: children)
2823-
| _ ->
2824-
Scanner.popMode p.scanner Jsx;
2825-
List.rev children
2820+
| _ -> children
28262821
in
28272822
match p.Parser.token with
28282823
| DotDotDot ->
28292824
Parser.next p;
28302825
(true, [parsePrimaryExpr ~operand:(parseAtomicExpr p) ~noCall:true p])
2831-
| _ -> (false, loop p [])
2826+
| _ ->
2827+
let children = List.rev (loop p []) in
2828+
Scanner.popMode p.scanner Jsx;
2829+
(false, children)
28322830

28332831
and parseBracedOrRecordExpr p =
28342832
let startPos = p.Parser.startPos in

jscomp/syntax/src/res_printer.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ let printLongident = function
421421

422422
type identifierStyle = ExoticIdent | NormalIdent
423423

424-
let classifyIdentContent ?(allowUident = false) txt =
424+
let classifyIdentContent ?(allowUident = false) ?(allowHyphen = false) txt =
425425
if Token.isKeywordTxt txt then ExoticIdent
426426
else
427427
let len = String.length txt in
@@ -431,16 +431,18 @@ let classifyIdentContent ?(allowUident = false) txt =
431431
match String.unsafe_get txt i with
432432
| 'A' .. 'Z' when allowUident -> loop (i + 1)
433433
| 'a' .. 'z' | '_' -> loop (i + 1)
434+
| '-' when allowHyphen -> loop (i + 1)
434435
| _ -> ExoticIdent
435436
else
436437
match String.unsafe_get txt i with
437438
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '\'' | '_' -> loop (i + 1)
439+
| '-' when allowHyphen -> loop (i + 1)
438440
| _ -> ExoticIdent
439441
in
440442
loop 0
441443

442-
let printIdentLike ?allowUident txt =
443-
match classifyIdentContent ?allowUident txt with
444+
let printIdentLike ?allowUident ?allowHyphen txt =
445+
match classifyIdentContent ?allowUident ?allowHyphen txt with
444446
| ExoticIdent -> Doc.concat [Doc.text "\\\""; Doc.text txt; Doc.text "\""]
445447
| NormalIdent -> Doc.text txt
446448

@@ -4404,7 +4406,7 @@ and printJsxProp ~state arg cmtTbl =
44044406
* Navabar.createElement -> Navbar
44054407
* Staff.Users.createElement -> Staff.Users *)
44064408
and printJsxName {txt = lident} =
4407-
let printIdent = printIdentLike ~allowUident:true in
4409+
let printIdent = printIdentLike ~allowUident:true ~allowHyphen:true in
44084410
let rec flatten acc lident =
44094411
match lident with
44104412
| Longident.Lident txt -> printIdent txt :: acc

jscomp/syntax/src/res_scanner.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,11 @@ let digitValue ch =
182182
let scanIdentifier scanner =
183183
let startOff = scanner.offset in
184184
let rec skipGoodChars scanner =
185-
match scanner.ch with
186-
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '\'' ->
185+
match (scanner.ch, inJsxMode scanner) with
186+
| ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '\''), false ->
187+
next scanner;
188+
skipGoodChars scanner
189+
| ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '\'' | '-'), true ->
187190
next scanner;
188191
skipGoodChars scanner
189192
| _ -> ()

0 commit comments

Comments
 (0)