Skip to content

Commit

Permalink
Type checking support for new.target and import.meta as mixed
Browse files Browse the repository at this point in the history
Summary:
Rather than give an `unsupported-syntax` error when `new.target` or `import.meta` are used, type them as `mixed`.
This is a bit more useful (and still safe).

Fixes #1152
Reference to #6913

Changelog: [feature] Added type checking support for `new.target` and `import.meta` as `mixed`

Reviewed By: mroch

Differential Revision: D30745294

fbshipit-source-id: 1026ddde29936b7c5c8f81daf3a7b2515a514679
  • Loading branch information
gkz authored and facebook-github-bot committed Sep 7, 2021
1 parent e6831b4 commit 627e366
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 38 deletions.
10 changes: 0 additions & 10 deletions newtests/new_target/_flowconfig

This file was deleted.

28 changes: 0 additions & 28 deletions newtests/new_target/test.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/typing/statement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,22 @@ and expression_ ~cond ~annot cx loc e : (ALoc.t, ALoc.t * Type.t) Ast.Expression
| Generator _ ->
Flow.add_output cx Error_message.(EUnsupportedSyntax (loc, GeneratorExpression));
Tast_utils.error_mapper#expression ex
| MetaProperty
{
MetaProperty.meta = (_, { Ast.Identifier.name = "new"; _ }) as meta;
property = (_, { Ast.Identifier.name = "target"; _ }) as property;
comments;
} ->
let t = bogus_trust () |> MixedT.at loc in
((loc, t), MetaProperty { MetaProperty.meta; property; comments })
| MetaProperty
{
MetaProperty.meta = (_, { Ast.Identifier.name = "import"; _ }) as meta;
property = (_, { Ast.Identifier.name = "meta"; _ }) as property;
comments;
} ->
let t = bogus_trust () |> MixedT.at loc in
((loc, t), MetaProperty { MetaProperty.meta; property; comments })
| MetaProperty _ ->
Flow.add_output cx Error_message.(EUnsupportedSyntax (loc, MetaPropertyExpression));
Tast_utils.error_mapper#expression ex
Expand Down
Empty file added tests/meta_property/.flowconfig
Empty file.
4 changes: 4 additions & 0 deletions tests/meta_property/import_meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow

(import.meta: mixed); // OK
(import.meta: 1); // Error
45 changes: 45 additions & 0 deletions tests/meta_property/meta_property.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Error ----------------------------------------------------------------------------------------------- import_meta.js:4:2

Cannot cast `import.meta` to number literal `1` because mixed [1] is incompatible with number literal `1` [2].
[incompatible-cast]

import_meta.js:4:2
4| (import.meta: 1); // Error
^^^^^^^^^^^ [1]

References:
import_meta.js:4:15
4| (import.meta: 1); // Error
^ [2]


Error ------------------------------------------------------------------------------------------------ new_target.js:5:4

Cannot cast `new.target` to boolean because mixed [1] is incompatible with boolean [2]. [incompatible-cast]

new_target.js:5:4
5| (new.target: boolean); // Error
^^^^^^^^^^ [1]

References:
new_target.js:5:16
5| (new.target: boolean); // Error
^^^^^^^ [2]


Error ----------------------------------------------------------------------------------------------- new_target.js:13:6

Cannot cast `new.target` to boolean because mixed [1] is incompatible with boolean [2]. [incompatible-cast]

new_target.js:13:6
13| (new.target: boolean); // Error
^^^^^^^^^^ [1]

References:
new_target.js:13:18
13| (new.target: boolean); // Error
^^^^^^^ [2]



Found 3 errors
15 changes: 15 additions & 0 deletions tests/meta_property/new_target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @flow

function Foo() {
(new.target: mixed); // OK
(new.target: boolean); // Error

if (!new.target) {} // OK
}

class A {
constructor() {
(new.target: mixed); // OK
(new.target: boolean); // Error
}
}

0 comments on commit 627e366

Please sign in to comment.