Skip to content

Commit 126d916

Browse files
committed
Add global jsx pragma
1 parent 07679f6 commit 126d916

File tree

9 files changed

+69
-8
lines changed

9 files changed

+69
-8
lines changed

src/commands/commandUtils.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ let make_options ~flowconfig_name ~flowconfig ~lazy_mode ~root (options_flags: O
11001100
opt_recursion_limit = FlowConfig.recursion_limit flowconfig;
11011101
opt_max_files_checked_per_worker = FlowConfig.max_files_checked_per_worker flowconfig;
11021102
opt_type_asserts = FlowConfig.type_asserts flowconfig;
1103+
opt_jsx_pragma = FlowConfig.jsx_pragma flowconfig;
11031104
}
11041105

11051106
let make_env flowconfig_name connect_flags root =

src/commands/config/flowConfig.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ module Opts = struct
101101
types_first: bool;
102102
wait_for_recheck: bool;
103103
weak: bool;
104+
jsx_pragma: string option;
104105
}
105106

106107
let warn_on_unknown_opts (raw_opts, config) : (t * warning list, error) result =
@@ -192,6 +193,7 @@ module Opts = struct
192193
types_first = false;
193194
wait_for_recheck = false;
194195
weak = false;
196+
jsx_pragma = None;
195197
}
196198

197199
let parse_lines : line list -> (raw_options, error) result =
@@ -634,6 +636,9 @@ module Opts = struct
634636

635637
"experimental.types_first.max_files_checked_per_worker",
636638
uint (fun opts v -> Ok { opts with max_files_checked_per_worker = v });
639+
640+
"jsx.pragma",
641+
string (fun opts v -> Ok { opts with jsx_pragma = Some v });
637642
]
638643

639644
let parse =
@@ -1070,6 +1075,7 @@ let traces c = c.options.Opts.traces
10701075
let trust_mode c = c.options.Opts.trust_mode
10711076
let type_asserts c = c.options.Opts.type_asserts
10721077
let types_first c = c.options.Opts.types_first
1078+
let jsx_pragma c = c.options.Opts.jsx_pragma
10731079
let required_version c = c.version
10741080
let wait_for_recheck c = c.options.Opts.wait_for_recheck
10751081
let weak c = c.options.Opts.weak

src/commands/config/flowConfig.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ val traces: config -> int
9595
val trust_mode: config -> Options.trust_mode
9696
val type_asserts: config -> bool
9797
val types_first: config -> bool
98+
val jsx_pragma: config -> string option
9899
val wait_for_recheck: config -> bool
99100
val weak: config -> bool
100101

src/common/options.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ type t = {
117117
opt_include_suppressions : bool;
118118
opt_trust_mode: trust_mode;
119119
opt_type_asserts: bool;
120+
opt_jsx_pragma: string option;
120121
}
121122

122123
let all opts = opts.opt_all
@@ -182,6 +183,7 @@ let strict_mode opts = opts.opt_strict_mode
182183

183184
let trust_mode opts = opts.opt_trust_mode
184185
let type_asserts opts = opts.opt_type_asserts
186+
let jsx_pragma opts = opts.opt_jsx_pragma
185187

186188

187189
let lazy_mode_to_string lazy_mode =

src/parsing/parsing_service_js.ml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ let does_content_match_file_hash ~reader file content =
472472
* Add success/error info to passed accumulator. *)
473473
let reducer
474474
~worker_mutator ~reader ~parse_options ~skip_hash_mismatch
475-
~max_header_tokens ~noflow ~parse_unchanged
475+
~max_header_tokens ~noflow ~jsx_pragma ~parse_unchanged
476476
parse_results file
477477
: results =
478478
(* It turns out that sometimes files appear and disappear very quickly. Just
@@ -525,6 +525,13 @@ let reducer
525525
if noflow file then { info with Docblock.flow = Some Docblock.OptOut }
526526
else info
527527
in
528+
let info = match jsx_pragma with
529+
| Some jsx_pragma ->
530+
let padding = (String.make 1 '\n') ^ (String.make 1 ' ') in
531+
let (jsx_expr, _) = Parser_flow.jsx_pragma_expression (padding ^ jsx_pragma) (Some file) in
532+
{ info with Docblock.jsx = Some (Docblock.Jsx_pragma (jsx_pragma, jsx_expr)) }
533+
| None -> info
534+
in
528535
begin match (do_parse ~parse_options ~info content file) with
529536
| Parse_ok parse_ok ->
530537
let ast, file_sig = basic parse_ok in
@@ -602,13 +609,13 @@ let next_of_filename_set ?(with_progress=false) workers filenames =
602609
else MultiWorkerLwt.next workers (FilenameSet.elements filenames)
603610

604611
let parse ~worker_mutator ~reader ~parse_options ~skip_hash_mismatch ~profile
605-
~max_header_tokens ~noflow ~parse_unchanged workers next
612+
~max_header_tokens ~noflow ~jsx_pragma ~parse_unchanged workers next
606613
: results Lwt.t =
607614
let t = Unix.gettimeofday () in
608615
let reducer =
609616
reducer
610617
~worker_mutator ~reader ~parse_options ~skip_hash_mismatch
611-
~max_header_tokens ~noflow ~parse_unchanged
618+
~max_header_tokens ~noflow ~jsx_pragma ~parse_unchanged
612619
in
613620
let%lwt results = MultiWorkerLwt.call
614621
workers
@@ -632,15 +639,15 @@ let parse ~worker_mutator ~reader ~parse_options ~skip_hash_mismatch ~profile
632639
Lwt.return results
633640

634641
let reparse
635-
~transaction ~reader ~parse_options ~profile ~max_header_tokens ~noflow
642+
~transaction ~reader ~parse_options ~profile ~max_header_tokens ~noflow ~jsx_pragma
636643
~parse_unchanged ~with_progress ~workers ~modified:files ~deleted =
637644
(* save old parsing info for files *)
638645
let all_files = FilenameSet.union files deleted in
639646
let master_mutator, worker_mutator = Parsing_heaps.Reparse_mutator.create transaction all_files in
640647
let next = next_of_filename_set ?with_progress workers files in
641648
let%lwt results =
642649
parse ~worker_mutator ~reader ~parse_options ~skip_hash_mismatch:false ~profile
643-
~max_header_tokens ~noflow ~parse_unchanged workers next
650+
~max_header_tokens ~noflow ~jsx_pragma ~parse_unchanged workers next
644651
in
645652
let modified = results.parse_ok |> FilenameMap.keys |> FilenameSet.of_list in
646653
let modified = List.fold_left (fun acc (fail, _, _) ->
@@ -664,6 +671,7 @@ let parse_with_defaults ?types_mode ?use_strict ~reader options workers next =
664671
let facebook_fbt = Options.facebook_fbt options in
665672
let arch = Options.arch options in
666673
let abstract_locations = options.Options.opt_abstract_locations in
674+
let jsx_pragma = Options.jsx_pragma options in
667675
let parse_options =
668676
make_parse_options ~arch ~abstract_locations ~types_mode ~use_strict ~module_ref_prefix
669677
~facebook_fbt ()
@@ -673,7 +681,7 @@ let parse_with_defaults ?types_mode ?use_strict ~reader options workers next =
673681
let worker_mutator = Parsing_heaps.Parse_mutator.create () in
674682
parse
675683
~worker_mutator ~reader ~parse_options ~skip_hash_mismatch:false
676-
~profile ~max_header_tokens ~noflow ~parse_unchanged workers next
684+
~profile ~max_header_tokens ~noflow ~jsx_pragma ~parse_unchanged workers next
677685

678686
let reparse_with_defaults
679687
~transaction ~reader ?types_mode ?use_strict ?with_progress
@@ -686,12 +694,13 @@ let reparse_with_defaults
686694
let facebook_fbt = Options.facebook_fbt options in
687695
let arch = Options.arch options in
688696
let abstract_locations = options.Options.opt_abstract_locations in
697+
let jsx_pragma = Options.jsx_pragma options in
689698
let parse_options =
690699
make_parse_options ~arch ~abstract_locations ~types_mode ~use_strict ~module_ref_prefix
691700
~facebook_fbt ()
692701
in
693702
reparse
694-
~transaction ~reader ~parse_options ~profile ~max_header_tokens ~noflow
703+
~transaction ~reader ~parse_options ~profile ~max_header_tokens ~noflow ~jsx_pragma
695704
~parse_unchanged ~with_progress ~workers ~modified ~deleted
696705

697706
(* ensure_parsed takes a set of files, finds the files which haven't been parsed, and parses them.
@@ -730,6 +739,7 @@ let ensure_parsed ~reader options workers files =
730739
let facebook_fbt = Options.facebook_fbt options in
731740
let arch = Options.arch options in
732741
let abstract_locations = options.Options.opt_abstract_locations in
742+
let jsx_pragma = Options.jsx_pragma options in
733743

734744
let parse_options =
735745
make_parse_options ~types_mode ~use_strict ~module_ref_prefix ~facebook_fbt ~arch
@@ -738,7 +748,7 @@ let ensure_parsed ~reader options workers files =
738748

739749
let%lwt results = parse
740750
~worker_mutator ~reader ~parse_options ~skip_hash_mismatch:true
741-
~profile ~max_header_tokens ~noflow ~parse_unchanged workers next
751+
~profile ~max_header_tokens ~noflow ~jsx_pragma ~parse_unchanged workers next
742752
in
743753

744754
Lwt.return results.parse_hash_mismatch_skips
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[options]
2+
jsx.pragma=bar

tests/jsx_pragma_option/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@flow
2+
3+
import { bar } from './jsx'
4+
5+
6+
// ok
7+
const Hello = <hello a="nice" />;
8+
9+
// error
10+
const Bye = <a />;

tests/jsx_pragma_option/jsx.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//@flow
2+
3+
declare export var bar: {
4+
(type: 'hello', props: {|a: string|}, children: any): {| hello: 'div' |}
5+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Error --------------------------------------------------------------------------------------------------- index.js:10:13
2+
3+
Cannot create `a` element because:
4+
- `a` [1] is incompatible with string literal `hello` [2].
5+
- inexact null [3] is incompatible with exact object type [4].
6+
7+
index.js:10:13
8+
10| const Bye = <a />;
9+
^^^^^ [3]
10+
11+
References:
12+
index.js:10:14
13+
10| const Bye = <a />;
14+
^ [1]
15+
jsx.js:4:10
16+
4| (type: 'hello', props: {|a: string|}, children: any): {| hello: 'div' |}
17+
^^^^^^^ [2]
18+
jsx.js:4:26
19+
4| (type: 'hello', props: {|a: string|}, children: any): {| hello: 'div' |}
20+
^^^^^^^^^^^^^ [4]
21+
22+
23+
24+
Found 2 errors

0 commit comments

Comments
 (0)