Skip to content

Commit 81f5705

Browse files
committed
Use term "array index" in error message and add detail for other numbers
1 parent a862bf1 commit 81f5705

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/utils/__tests__/__snapshots__/rttc.ts.snap

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8317,8 +8317,8 @@ Object {
83178317

83188318
exports[`Member expression type combinations: Invalid type combinations return TypeError 54`] = `
83198319
Object {
8320-
"elaborate": "Expected integer as prop, got boolean.",
8321-
"explain": "Expected integer as prop, got boolean.",
8320+
"elaborate": "Expected array index as prop, got boolean.",
8321+
"explain": "Expected array index as prop, got boolean.",
83228322
"left": Array [
83238323
2,
83248324
],
@@ -8328,8 +8328,8 @@ Object {
83288328

83298329
exports[`Member expression type combinations: Invalid type combinations return TypeError 55`] = `
83308330
Object {
8331-
"elaborate": "Expected integer as prop, got string.",
8332-
"explain": "Expected integer as prop, got string.",
8331+
"elaborate": "Expected array index as prop, got string.",
8332+
"explain": "Expected array index as prop, got string.",
83338333
"left": Array [
83348334
2,
83358335
],
@@ -8339,8 +8339,8 @@ Object {
83398339

83408340
exports[`Member expression type combinations: Invalid type combinations return TypeError 56`] = `
83418341
Object {
8342-
"elaborate": "Expected integer as prop, got function.",
8343-
"explain": "Expected integer as prop, got function.",
8342+
"elaborate": "Expected array index as prop, got function.",
8343+
"explain": "Expected array index as prop, got function.",
83448344
"left": Array [
83458345
2,
83468346
],
@@ -8350,8 +8350,8 @@ Object {
83508350

83518351
exports[`Member expression type combinations: Invalid type combinations return TypeError 57`] = `
83528352
Object {
8353-
"elaborate": "Expected integer as prop, got function.",
8354-
"explain": "Expected integer as prop, got function.",
8353+
"elaborate": "Expected array index as prop, got function.",
8354+
"explain": "Expected array index as prop, got function.",
83558355
"left": Array [
83568356
2,
83578357
],
@@ -8361,8 +8361,8 @@ Object {
83618361

83628362
exports[`Member expression type combinations: Invalid type combinations return TypeError 58`] = `
83638363
Object {
8364-
"elaborate": "Expected integer as prop, got object.",
8365-
"explain": "Expected integer as prop, got object.",
8364+
"elaborate": "Expected array index as prop, got object.",
8365+
"explain": "Expected array index as prop, got object.",
83668366
"left": Array [
83678367
2,
83688368
],
@@ -8374,8 +8374,8 @@ Object {
83748374

83758375
exports[`Member expression type combinations: Invalid type combinations return TypeError 59`] = `
83768376
Object {
8377-
"elaborate": "Expected integer as prop, got array.",
8378-
"explain": "Expected integer as prop, got array.",
8377+
"elaborate": "Expected array index as prop, got array.",
8378+
"explain": "Expected array index as prop, got array.",
83798379
"left": Array [
83808380
2,
83818381
],
@@ -8387,8 +8387,8 @@ Object {
83878387

83888388
exports[`Member expression type combinations: Invalid type combinations return TypeError 60`] = `
83898389
Object {
8390-
"elaborate": "Expected integer as prop, got undefined.",
8391-
"explain": "Expected integer as prop, got undefined.",
8390+
"elaborate": "Expected array index as prop, got undefined.",
8391+
"explain": "Expected array index as prop, got undefined.",
83928392
"left": Array [
83938393
2,
83948394
],
@@ -8398,8 +8398,8 @@ Object {
83988398

83998399
exports[`Member expression type combinations: Invalid type combinations return TypeError 61`] = `
84008400
Object {
8401-
"elaborate": "Expected integer as prop, got null.",
8402-
"explain": "Expected integer as prop, got null.",
8401+
"elaborate": "Expected array index as prop, got null.",
8402+
"explain": "Expected array index as prop, got null.",
84038403
"left": Array [
84048404
2,
84058405
],
@@ -8579,8 +8579,8 @@ Object {
85798579

85808580
exports[`Member expression type combinations: Invalid type combinations return TypeError 80`] = `
85818581
Object {
8582-
"elaborate": "Expected integer as prop, got number.",
8583-
"explain": "Expected integer as prop, got number.",
8582+
"elaborate": "Expected array index as prop, got other number.",
8583+
"explain": "Expected array index as prop, got other number.",
85848584
"left": Array [
85858585
2,
85868586
],
@@ -8590,8 +8590,8 @@ Object {
85908590

85918591
exports[`Member expression type combinations: Invalid type combinations return TypeError 81`] = `
85928592
Object {
8593-
"elaborate": "Expected integer as prop, got number.",
8594-
"explain": "Expected integer as prop, got number.",
8593+
"elaborate": "Expected array index as prop, got other number.",
8594+
"explain": "Expected array index as prop, got other number.",
85958595
"left": Array [
85968596
2,
85978597
],
@@ -8601,8 +8601,8 @@ Object {
86018601

86028602
exports[`Member expression type combinations: Invalid type combinations return TypeError 82`] = `
86038603
Object {
8604-
"elaborate": "Expected integer as prop, got number.",
8605-
"explain": "Expected integer as prop, got number.",
8604+
"elaborate": "Expected array index as prop, got other number.",
8605+
"explain": "Expected array index as prop, got other number.",
86068606
"left": Array [
86078607
2,
86088608
],

src/utils/rttc.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ export const checkMemberAccess = (node: es.Node, obj: Value, prop: Value) => {
9999
if (isObject(obj)) {
100100
return isString(prop) ? undefined : new TypeError(node, ' as prop', 'string', typeOf(prop))
101101
} else if (isArray(obj)) {
102-
return isArrayIndex(prop) ? undefined : new TypeError(node, ' as prop', 'integer', typeOf(prop))
102+
return isArrayIndex(prop)
103+
? undefined
104+
: isNumber(prop)
105+
? new TypeError(node, ' as prop', 'array index', 'other number')
106+
: new TypeError(node, ' as prop', 'array index', typeOf(prop))
103107
} else {
104108
return new TypeError(node, '', 'object or array', typeOf(obj))
105109
}

0 commit comments

Comments
 (0)