Skip to content

Commit

Permalink
[flow][match] Rename/move some match tests
Browse files Browse the repository at this point in the history
Summary:
- Now that we have match statement tests in `statement.js`, move the match expression tests to `expression.js`
- Move reference before declaration test case from `patterns.js` to `pattern-errors.js`
- Add test cases for reference of of binding introduced in pattern, which currently has a bad error message but will be improved

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D70025842

fbshipit-source-id: f18a05aa3b1090c3991279161849968f6818af0c
  • Loading branch information
gkz authored and facebook-github-bot committed Feb 22, 2025
1 parent 0b8cc59 commit 4b1a742
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 59 deletions.
File renamed without changes.
156 changes: 107 additions & 49 deletions tests/match/match.exp
Original file line number Diff line number Diff line change
@@ -1,122 +1,122 @@
Error ----------------------------------------------------------------------------------------------------- body.js:18:3
Error ----------------------------------------------------------------------------------------------- expression.js:18:3

Cannot cast `out` to empty because boolean [1] is incompatible with empty [2]. [incompatible-cast]

body.js:18:3
expression.js:18:3
18| out as empty; // ERROR
^^^

References:
body.js:13:8
expression.js:13:8
13| 1: true,
^^^^ [1]
body.js:18:10
expression.js:18:10
18| out as empty; // ERROR
^^^^^ [2]


Error ----------------------------------------------------------------------------------------------------- body.js:18:3
Error ----------------------------------------------------------------------------------------------- expression.js:18:3

Cannot cast `out` to empty because boolean [1] is incompatible with empty [2]. [incompatible-cast]

body.js:18:3
expression.js:18:3
18| out as empty; // ERROR
^^^

References:
body.js:14:8
expression.js:14:8
14| 2: false,
^^^^^ [1]
body.js:18:10
expression.js:18:10
18| out as empty; // ERROR
^^^^^ [2]


Error ----------------------------------------------------------------------------------------------------- body.js:27:3
Error ----------------------------------------------------------------------------------------------- expression.js:27:3

Cannot cast `out` to empty because boolean [1] is incompatible with empty [2]. [incompatible-cast]

body.js:27:3
expression.js:27:3
27| out as empty; // ERROR
^^^

References:
body.js:23:8
expression.js:23:8
23| 1: true,
^^^^ [1]
body.js:27:10
expression.js:27:10
27| out as empty; // ERROR
^^^^^ [2]


Error ----------------------------------------------------------------------------------------------------- body.js:27:3
Error ----------------------------------------------------------------------------------------------- expression.js:27:3

Cannot cast `out` to empty because string [1] is incompatible with empty [2]. [incompatible-cast]

body.js:27:3
expression.js:27:3
27| out as empty; // ERROR
^^^

References:
body.js:24:8
expression.js:24:8
24| 2: 's',
^^^ [1]
body.js:27:10
expression.js:27:10
27| out as empty; // ERROR
^^^^^ [2]


Error ----------------------------------------------------------------------------------------------------- body.js:38:3
Error ----------------------------------------------------------------------------------------------- expression.js:38:3

Cannot cast `out` to empty because boolean [1] is incompatible with empty [2]. [incompatible-cast]

body.js:38:3
expression.js:38:3
38| out as empty; // ERROR
^^^

References:
body.js:34:8
expression.js:34:8
34| 1: true,
^^^^ [1]
body.js:38:10
expression.js:38:10
38| out as empty; // ERROR
^^^^^ [2]


Error ----------------------------------------------------------------------------------------------------- body.js:46:3
Error ----------------------------------------------------------------------------------------------- expression.js:46:3

Unreachable code. [unreachable-code]

46| out; // ERROR: unreachable
^^^^


Error ----------------------------------------------------------------------------------------------------- body.js:56:3
Error ----------------------------------------------------------------------------------------------- expression.js:56:3

Cannot cast `out` to empty because string [1] is incompatible with empty [2]. [incompatible-cast]

body.js:56:3
expression.js:56:3
56| out as empty; // ERROR
^^^

References:
body.js:53:8
expression.js:53:8
53| _: 's',
^^^ [1]
body.js:56:10
expression.js:56:10
56| out as empty; // ERROR
^^^^^ [2]


Error ----------------------------------------------------------------------------------------------------- body.js:97:3
Error ----------------------------------------------------------------------------------------------- expression.js:97:3

Cannot resolve name `a`. [cannot-resolve-name]

97| a; // ERROR
^


Error ---------------------------------------------------------------------------------------------------- body.js:107:3
Error ---------------------------------------------------------------------------------------------- expression.js:107:3

Cannot resolve name `a`. [cannot-resolve-name]

Expand Down Expand Up @@ -699,6 +699,78 @@ Invalid `as` pattern. Direct use on a binding pattern is not allowed. [match-inv
^^^^^^^^^^^^^^^^^^


Error ------------------------------------------------------------------------------------------ pattern-errors.js:119:6

Cannot use variable `a` [1] because the declaration either comes later or was skipped. [reference-before-declaration]

pattern-errors.js:119:6
119| [a, const a]: a, // ERROR: reference before declaration
^

References:
pattern-errors.js:119:15
119| [a, const a]: a, // ERROR: reference before declaration
^ [1]


Error ------------------------------------------------------------------------------------------ pattern-errors.js:127:6

Cannot use variable `a` [1] because the declaration either comes later or was skipped. [reference-before-declaration]

pattern-errors.js:127:6
127| [a.foo, const a]: a, // ERROR: reference before declaration
^

References:
pattern-errors.js:127:19
127| [a.foo, const a]: a, // ERROR: reference before declaration
^ [1]


Error ----------------------------------------------------------------------------------------- pattern-errors.js:137:12

The following definitions recursively depend on each other, and Flow cannot compute their types:
- `a` [1] depends on other definition [2]
- destructuring [2] depends on other definition [3]
Please add type annotations to these definitions [definition-cycle]

pattern-errors.js:137:12
137| [const a, a]: a, // ERROR
^ [1]

References:
pattern-errors.js:137:5
137| [const a, a]: a, // ERROR
^^^^^^^^^^^^ [2]
pattern-errors.js:137:15
137| [const a, a]: a, // ERROR
^ [3]


Error ----------------------------------------------------------------------------------------- pattern-errors.js:145:12

The following definitions recursively depend on each other, and Flow cannot compute their types:
- `a` [1] depends on other definition [2]
- property `1` [3] depends on other definition [4]
- destructuring [2] depends on other definition [3]
Please add type annotations to these definitions [definition-cycle]

pattern-errors.js:145:12
145| [const a, a.foo]: a, // ERROR
^ [1]

References:
pattern-errors.js:145:5
145| [const a, a.foo]: a, // ERROR
^^^^^^^^^^^^^^^^ [2]
pattern-errors.js:145:15
145| [const a, a.foo]: a, // ERROR
^^^^^ [3]
pattern-errors.js:145:15
145| [const a, a.foo]: a, // ERROR
^ [4]


Error -------------------------------------------------------------------------------------------------- patterns.js:9:3

Cannot cast `out` to empty because number [1] is incompatible with empty [2]. [incompatible-cast]
Expand Down Expand Up @@ -792,34 +864,20 @@ References:
^^^^^ [2]


Error ------------------------------------------------------------------------------------------------- patterns.js:76:6

Cannot use variable `a` [1] because the declaration either comes later or was skipped. [reference-before-declaration]

patterns.js:76:6
76| [a, const a]: a, // ERROR: reference before declaration
^

References:
patterns.js:76:15
76| [a, const a]: a, // ERROR: reference before declaration
^ [1]


Error ------------------------------------------------------------------------------------------------ patterns.js:89:23
Error ------------------------------------------------------------------------------------------------ patterns.js:79:23

Cannot cast `n` to empty because number [1] is incompatible with empty [2]. [incompatible-cast]

patterns.js:89:23
89| {foo: const n} if n as empty: n, // ERROR
patterns.js:79:23
79| {foo: const n} if n as empty: n, // ERROR
^

References:
patterns.js:83:26
83| declare const x: {foo: number};
patterns.js:73:26
73| declare const x: {foo: number};
^^^^^^ [1]
patterns.js:89:28
89| {foo: const n} if n as empty: n, // ERROR
patterns.js:79:28
79| {foo: const n} if n as empty: n, // ERROR
^^^^^ [2]


Expand Down Expand Up @@ -848,4 +906,4 @@ Cannot resolve name `a`. [cannot-resolve-name]



Found 70 errors
Found 73 errors
36 changes: 36 additions & 0 deletions tests/match/pattern-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,39 @@
const a as const b: 0, // ERROR
}
}

// Reference before declaration
{
declare const x: [number, number];

const out = match (x) {
[a, const a]: a, // ERROR: reference before declaration
_: 0,
};
}
{
declare const x: [number, {foo: number}];

const out = match (x) {
[a.foo, const a]: a, // ERROR: reference before declaration
_: 0,
};
}

// Reference binding introduced in same pattern
{
declare const x: [number, number];

const out = match (x) {
[const a, a]: a, // ERROR
_: 0,
};
}
{
declare const x: [{foo: number}, number];

const out = match (x) {
[const a, a.foo]: a, // ERROR
_: 0,
};
}
10 changes: 0 additions & 10 deletions tests/match/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@
out as empty; // ERROR
}

// Reference before declaration
{
declare const x: [boolean, number];

const out = match (x) {
[a, const a]: a, // ERROR: reference before declaration
_: 0,
};
}

// Guards
{
declare const x: {foo: number};
Expand Down

0 comments on commit 4b1a742

Please sign in to comment.