Skip to content

Commit

Permalink
[tests] Add numeric keys indexer test
Browse files Browse the repository at this point in the history
  • Loading branch information
goodmind committed Jun 7, 2019
1 parent f94fe3f commit 89a15ca
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 15 deletions.
52 changes: 37 additions & 15 deletions tests/indexer/A.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
//@flow

// No indexer should be fine
function foo0(): {} {
return { foo: "bar" }
return { foo: "bar" };
}

// Matching indexer should be fine
function foo1(): {[key: string]: string} {
return { foo: "bar" }
function foo1(): { [key: string]: string } {
return { foo: "bar" };
}

// Indexer with different key type is an error when it matches
function foo2(): {[key: number]: string} {
return { foo: "bar" }
function foo2(): { [key: number]: string } {
return { foo: "bar" };
}

// Matching indexer with different value type is an error
function foo3(): {[key: string]: number} {
return { foo: "bar" }
function foo3(): { [key: string]: number } {
return { foo: "bar" };
}

// Indexer with different key type and different value type is twice an error
function foo4(): {[key: number]: number} {
return { foo: "bar" }
function foo4(): { [key: number]: number } {
return { foo: "bar" };
}

// If key exists in object type then indexer is not matched
function foo5(): {[key: string]: number; foo: string} {
return { foo: "bar" }
function foo5(): { [key: string]: number, foo: string } {
return { foo: "bar" };
}

// If key exists in object type then indexer is not matched
function foo6(): {[key: number]: number; foo: string} {
return { foo: "bar" }
function foo6(): { [key: number]: number, foo: string } {
return { foo: "bar" };
}

// Should still complain about mistyped properties
function foo7(): {[key: string]: number; foo: number} {
return { foo: "bar" }
function foo7(): { [key: string]: number, foo: number } {
return { foo: "bar" };
}

// TODO: ok
function foo8(): { [key: number]: string } {
return { 0: "bar" };
}

// TODO: error
function foo9(): { [key: string]: string } {
return { 0: "bar" };
}

// TODO: error
function foo9(): { [key: string]: string } {
return { 0: "bar" };
}

// TODO: ok
function foo10(): { [key: string | number]: string } {
return { 1: "nice", a: "wtf" };
}
127 changes: 127 additions & 0 deletions tests/indexer/indexer.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
--- indexer.exp
+++ indexer.out
@@ -1,71 +1,85 @@
-Error ------------------------------------------------------------------------------------------------------- A.js:13:10
+Error ------------------------------------------------------------------------------------------------------- A.js:15:10

Cannot return object literal because string `foo` [1] is incompatible with number [2] in the indexer property's key.

- A.js:13:10
- 13| return { foo: "bar" }
+ A.js:15:10
+ 15| return { foo: "bar" };
^^^^^^^^^^^^^^ [1]

References:
- A.js:12:25
- 12| function foo2(): {[key: number]: string} {
- ^^^^^^ [2]
+ A.js:14:26
+ 14| function foo2(): { [key: number]: string } {
+ ^^^^^^ [2]


-Error ------------------------------------------------------------------------------------------------------- A.js:18:10
+Error ------------------------------------------------------------------------------------------------------- A.js:20:10

Cannot return object literal because string [1] is incompatible with number [2] in property `foo`.

- A.js:18:10
- 18| return { foo: "bar" }
+ A.js:20:10
+ 20| return { foo: "bar" };
^^^^^^^^^^^^^^

References:
- A.js:18:17
- 18| return { foo: "bar" }
+ A.js:20:17
+ 20| return { foo: "bar" };
^^^^^ [1]
- A.js:17:34
- 17| function foo3(): {[key: string]: number} {
- ^^^^^^ [2]
+ A.js:19:35
+ 19| function foo3(): { [key: string]: number } {
+ ^^^^^^ [2]


-Error ------------------------------------------------------------------------------------------------------- A.js:23:10
+Error ------------------------------------------------------------------------------------------------------- A.js:25:10

Cannot return object literal because:
- string `foo` [1] is incompatible with number [2] in the indexer property's key.
- string [3] is incompatible with number [4] in property `foo`.

- A.js:23:10
- 23| return { foo: "bar" }
+ A.js:25:10
+ 25| return { foo: "bar" };
^^^^^^^^^^^^^^ [1]

References:
- A.js:22:25
- 22| function foo4(): {[key: number]: number} {
- ^^^^^^ [2]
- A.js:23:17
- 23| return { foo: "bar" }
+ A.js:24:26
+ 24| function foo4(): { [key: number]: number } {
+ ^^^^^^ [2]
+ A.js:25:17
+ 25| return { foo: "bar" };
^^^^^ [3]
- A.js:22:34
- 22| function foo4(): {[key: number]: number} {
- ^^^^^^ [4]
+ A.js:24:35
+ 24| function foo4(): { [key: number]: number } {
+ ^^^^^^ [4]


-Error ------------------------------------------------------------------------------------------------------- A.js:38:10
+Error ------------------------------------------------------------------------------------------------------- A.js:40:10

Cannot return object literal because string [1] is incompatible with number [2] in property `foo`.

- A.js:38:10
- 38| return { foo: "bar" }
+ A.js:40:10
+ 40| return { foo: "bar" };
^^^^^^^^^^^^^^

References:
- A.js:38:17
- 38| return { foo: "bar" }
+ A.js:40:17
+ 40| return { foo: "bar" };
^^^^^ [1]
- A.js:37:47
- 37| function foo7(): {[key: string]: number; foo: number} {
- ^^^^^^ [2]
+ A.js:39:48
+ 39| function foo7(): { [key: string]: number, foo: number } {
+ ^^^^^^ [2]
+
+
+Error ------------------------------------------------------------------------------------------------------- A.js:45:10
+
+Cannot return object literal because string `0` [1] is incompatible with number [2] in the indexer property's key.
+
+ A.js:45:10
+ 45| return { 0: "bar" };
+ ^^^^^^^^^^^^ [1]
+
+References:
+ A.js:44:26
+ 44| function foo8(): { [key: number]: string } {
+ ^^^^^^ [2]


Error ------------------------------------------------------------------------------------------------------ call.js:4:2
@@ -97,4 +111,4 @@



-Found 8 errors
+Found 9 errors

0 comments on commit 89a15ca

Please sign in to comment.