Skip to content

Commit f0501ff

Browse files
mrochfacebook-github-bot
authored andcommitted
Explicitly make react the default JSX mode in Context
Summary: Closes facebook#6043 Reviewed By: gabelevi Differential Revision: D7403293 fbshipit-source-id: d2573cf7c877471b014f717a4bdbbb086e207e35
1 parent 2308c92 commit f0501ff

File tree

9 files changed

+46
-21
lines changed

9 files changed

+46
-21
lines changed

src/common/docblock.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,27 @@
88

99
type flow_mode = OptIn | OptInStrict | OptInWeak | OptOut
1010

11+
type jsx_pragma =
12+
(**
13+
* Specifies a function that should be invoked instead of React.createElement
14+
* when interpreting JSX syntax. Otherwise, the usual rules of JSX are
15+
* followed: children are varargs after a props argument.
16+
*)
17+
| Jsx_pragma of (string * Loc.t Ast.Expression.t)
18+
19+
(**
20+
* Alternate mode for interpreting JSX syntax. The element name is treated
21+
* as a function to be directly invoked, e.g. <Foo /> -> Foo({}).
22+
* Children are part of props instead of a separate argument.
23+
*)
24+
| Csx_pragma
25+
1126
type t = {
1227
flow: flow_mode option;
1328
preventMunge: bool option;
1429
providesModule: string option;
1530
isDeclarationFile: bool;
16-
jsx: Options.jsx_mode option;
31+
jsx: jsx_pragma option;
1732
}
1833

1934
let default_info = {

src/common/options.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ type lazy_mode =
1919
| LAZY_MODE_IDE
2020

2121
type jsx_mode =
22+
(* JSX desugars into a `React.createElement(name, props, ...children)` call *)
23+
| Jsx_react
24+
2225
(**
2326
* Specifies a function that should be invoked instead of React.createElement
2427
* when interpreting JSX syntax. Otherwise, the usual rules of JSX are
2528
* followed: children are varargs after a props argument.
2629
*)
27-
| JSXPragma of (string * Loc.t Ast.Expression.t)
30+
| Jsx_pragma of (string * Loc.t Ast.Expression.t)
31+
2832
(**
2933
* Alternate mode for interpreting JSX syntax. The element name is treated
3034
* as a function to be directly invoked, e.g. <Foo /> -> Foo({}).
3135
* Children are part of props instead of a separate argument.
3236
*)
33-
| CSX
37+
| Jsx_csx
3438

3539
type file_watcher =
3640
| NoFileWatcher

src/flow_dot_js.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ let stub_metadata ~root ~checked = { Context.
127127
munge_underscores = false;
128128
verbose = None;
129129
weak = false;
130-
jsx = None;
130+
jsx = Options.Jsx_react;
131131
strict = false;
132132

133133
(* global *)

src/parsing/parsing_service_js.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ let extract_docblock =
215215
if info.jsx <> None then
216216
(csx_loc, MultipleJSXAttributes)::errors, info
217217
else
218-
errors, { info with jsx = Some Options.CSX }
218+
errors, { info with jsx = Some Csx_pragma }
219219
in
220220
parse_attributes acc xs
221221
| [jsx_loc, "@jsx"] -> (jsx_loc, InvalidJSXAttribute None)::errors, info
@@ -232,8 +232,7 @@ let extract_docblock =
232232
let (jsx_expr, _) = Parser_flow.jsx_pragma_expression
233233
(padding ^ expr)
234234
expr_loc.Loc.source in
235-
let jsx_pragma = Options.JSXPragma (expr, jsx_expr) in
236-
errors, { info with jsx = Some jsx_pragma }
235+
errors, { info with jsx = Some (Jsx_pragma (expr, jsx_expr)) }
237236
with
238237
| Parse_error.Error [] ->
239238
(expr_loc, InvalidJSXAttribute None)::errors, info

src/typing/context.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type metadata = {
2121
munge_underscores: bool;
2222
verbose: Verbose.t option;
2323
weak: bool;
24-
jsx: Options.jsx_mode option;
24+
jsx: Options.jsx_mode;
2525
strict: bool;
2626

2727
(* global *)
@@ -132,7 +132,7 @@ let metadata_of_options options = {
132132
munge_underscores = Options.should_munge_underscores options;
133133
verbose = Options.verbose options;
134134
weak = Options.weak_by_default options;
135-
jsx = None;
135+
jsx = Options.Jsx_react;
136136
strict = false;
137137

138138
(* global *)

src/typing/context.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type metadata = {
2424
munge_underscores: bool;
2525
verbose: Verbose.t option;
2626
weak: bool;
27-
jsx: Options.jsx_mode option;
27+
jsx: Options.jsx_mode;
2828
strict: bool;
2929
(* global *)
3030
enable_const_params: bool;
@@ -104,7 +104,7 @@ val type_graph: t -> Graph_explorer.graph
104104
val type_table: t -> Type_table.t
105105
val verbose: t -> Verbose.t option
106106
val max_workers: t -> int
107-
val jsx: t -> Options.jsx_mode option
107+
val jsx: t -> Options.jsx_mode
108108
val exists_checks: t -> ExistsCheck.t LocMap.t
109109
val exists_excuses: t -> ExistsCheck.t LocMap.t
110110
val use_def: t -> Scope_api.info * Ssa_api.values

src/typing/merge_js.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ let detect_test_prop_misses cx =
151151
let apply_docblock_overrides (metadata: Context.metadata) docblock_info =
152152
let open Context in
153153

154-
let metadata = { metadata with jsx = Docblock.jsx docblock_info } in
154+
let metadata =
155+
let jsx = match Docblock.jsx docblock_info with
156+
| Some (Docblock.Jsx_pragma (expr, jsx_expr)) -> Options.Jsx_pragma (expr, jsx_expr)
157+
| Some Docblock.Csx_pragma -> Options.Jsx_csx
158+
| None -> Options.Jsx_react
159+
in
160+
{ metadata with jsx }
161+
in
155162

156163
let metadata = match Docblock.flow docblock_info with
157164
| None -> metadata

src/typing/statement.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3867,7 +3867,7 @@ and jsx_title cx openingElement children locs = Ast.JSX.(
38673867
let fbt_reason = mk_reason RFbt loc_element in
38683868
Flow.get_builtin_type cx fbt_reason facebook_fbt
38693869

3870-
| Identifier (loc, { Identifier.name }), _, None ->
3870+
| Identifier (loc, { Identifier.name }), _, Options.Jsx_react ->
38713871
if Type_inference_hooks_js.dispatch_id_hook cx name loc then AnyT.at loc_element else
38723872
let reason = mk_reason (RReactElement (Some name)) loc_element in
38733873
let c =
@@ -3879,7 +3879,7 @@ and jsx_title cx openingElement children locs = Ast.JSX.(
38793879
let o = jsx_mk_props cx reason c name attributes children in
38803880
jsx_desugar cx name c o attributes children locs
38813881

3882-
| Identifier (loc, { Identifier.name }), _, Some Options.JSXPragma _ ->
3882+
| Identifier (loc, { Identifier.name }), _, Options.Jsx_pragma _ ->
38833883
if Type_inference_hooks_js.dispatch_id_hook cx name loc then AnyT.at loc_element else
38843884
let reason = mk_reason (RJSXElement (Some name)) loc_element in
38853885
let c =
@@ -3891,7 +3891,7 @@ and jsx_title cx openingElement children locs = Ast.JSX.(
38913891
let o = jsx_mk_props cx reason c name attributes children in
38923892
jsx_desugar cx name c o attributes children locs
38933893

3894-
| Identifier (loc, { Identifier.name }), _, Some Options.CSX ->
3894+
| Identifier (loc, { Identifier.name }), _, Options.Jsx_csx ->
38953895
(**
38963896
* It's a bummer to duplicate this case, but CSX does not want the
38973897
* "if name = String.capitalize name" restriction.
@@ -3902,7 +3902,7 @@ and jsx_title cx openingElement children locs = Ast.JSX.(
39023902
let o = jsx_mk_props cx reason c name attributes children in
39033903
jsx_desugar cx name c o attributes children locs
39043904

3905-
| MemberExpression member, _, None ->
3905+
| MemberExpression member, _, Options.Jsx_react ->
39063906
let name = jsx_title_member_to_string member in
39073907
let el = RReactElement (Some name) in
39083908
let reason = mk_reason el loc_element in
@@ -3917,7 +3917,7 @@ and jsx_title cx openingElement children locs = Ast.JSX.(
39173917
)
39183918

39193919
and jsx_mk_props cx reason c name attributes children = Ast.JSX.(
3920-
let is_react = Context.jsx cx = None in
3920+
let is_react = Context.jsx cx = Options.Jsx_react in
39213921
let reason_props = replace_reason_const
39223922
(if is_react then RReactProps else RJSXElementProps name)
39233923
reason in
@@ -4026,7 +4026,7 @@ and jsx_mk_props cx reason c name attributes children = Ast.JSX.(
40264026
and jsx_desugar cx name component_t props attributes children locs =
40274027
let loc_element, loc_opening, loc_children = locs in
40284028
match Context.jsx cx with
4029-
| None ->
4029+
| Options.Jsx_react ->
40304030
let reason = mk_reason (RReactElement (Some name)) loc_element in
40314031
let react = Env.var_ref ~lookup_mode:ForValue cx "React" loc_opening in
40324032
let children = List.map (function
@@ -4055,7 +4055,7 @@ and jsx_desugar cx name component_t props attributes children locs =
40554055
None
40564056
))
40574057
)
4058-
| Some Options.JSXPragma (raw_jsx_expr, jsx_expr) ->
4058+
| Options.Jsx_pragma (raw_jsx_expr, jsx_expr) ->
40594059
let reason = mk_reason (RJSXFunctionCall raw_jsx_expr) loc_element in
40604060

40614061
(* A JSX element with no attributes should pass in null as the second
@@ -4088,7 +4088,7 @@ and jsx_desugar cx name component_t props attributes children locs =
40884088
let f = jsx_pragma_expression cx raw_jsx_expr loc_element jsx_expr in
40894089
func_call cx reason ~use_op ~call_strict_arity:false f argts
40904090
)
4091-
| Some Options.CSX ->
4091+
| Options.Jsx_csx ->
40924092
let reason = mk_reason (RJSXFunctionCall name) loc_element in
40934093
let use_op = Op (JSXCreateElement {
40944094
op = reason;

testgen/flowtestgen_utils.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ let stub_metadata ~root ~checked = { Context.
524524
*)
525525
verbose = None;
526526
weak = false;
527-
jsx = None;
527+
jsx = Options.Jsx_react;
528528
strict = true;
529529
(* global *)
530530
enable_const_params = false;

0 commit comments

Comments
 (0)