Permalink
Browse files

Strip strict typechecking

Summary:
When lowering coroutines, the classes generated often have untyped fields/parameters, which will fail to pass the typechecking test if the raised code/file is in strict mode.

In this diff, the code that we generate will strip the strict mode from the file, producing a partially typed lowered code file.

Reviewed By: michaeltingley

Differential Revision: D6756402

fbshipit-source-id: 5aa70962bf1bde18b531e22f920f4fda84eddf9f
  • Loading branch information...
Thomas Jiang authored and hhvm-bot committed Jan 23, 2018
1 parent 89b646a commit 7f9d4e6c6ebc124df1a869b34b755a881d02342e
@@ -288,6 +288,37 @@ let rewrite_all_declarations declaration_list =
rewrite_declaration_acc 0 declaration_list in
rewritten_declarations
(**
* Because there are many instances in the lowered code where we are unable
* to annotate types, files with // strict in them will not pass typechecking
* when lowered, so we need to strip the strict mode when lowering the files
*
* We only strip the trailing text if the trailing text doesn't match // decl
*)
let remove_strict_mode hh_decl =
let remove_strict_trailing_text node =
match syntax node with
| MarkupSuffix ({
markup_suffix_name = ({
syntax = Token name;
_ ;
} as suffix_name);
_ ;
} as markup_suffix) ->
if matches_decl name
then Rewriter.Result.Keep
else
let new_suffix = Token.with_trailing_text single_space name in
let markup_suffix_name =
Syntax.synthesize_from suffix_name (Token new_suffix) in
let new_node =
Syntax.synthesize_from
node
(MarkupSuffix { markup_suffix with markup_suffix_name; }) in
Rewriter.Result.Replace new_node
| _ -> Rewriter.Result.Keep in
Rewriter.rewrite_post remove_strict_trailing_text hh_decl
(**
Lowers all coroutines found in a script
@@ -320,6 +351,7 @@ let lower_coroutines root =
let declarations = syntax_node_to_list script_declarations in
begin match declarations with
| hh_decl :: declarations ->
let hh_decl = remove_strict_mode hh_decl in
let rewritten_declarations = rewrite_all_declarations declarations in
let rewritten_declarations = hh_decl :: rewritten_declarations in
make_script (make_list rewritten_declarations)
@@ -1018,3 +1018,9 @@ let make_goto_statement_syntax label =
let boolval_syntax =
make_simple_name_syntax "boolval"
(* To detect whether a file's first token is marked with // decl *)
let matches_decl token =
let trailing_text = Token.trailing_text token in
let decl_regexp = Str.regexp "^\\([ \\t]*//[ \\t]*decl\\)" in
Str.string_match decl_regexp trailing_text 0
@@ -175,6 +175,9 @@ let with_updated_original_source_data token update_original_source_data =
let with_leading leading token =
with_updated_original_source_data token (SourceData.with_leading leading)
let with_trailing_text trailing_text token =
{ token with trailing_text }
let with_kind token kind =
{ token with kind }

0 comments on commit 7f9d4e6

Please sign in to comment.