Skip to content

Commit

Permalink
Use term "array index" in error message and add detail for other numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
thomastanck committed Jul 10, 2019
1 parent a862bf1 commit 81f5705
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
44 changes: 22 additions & 22 deletions src/utils/__tests__/__snapshots__/rttc.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8317,8 +8317,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 54`] = `
Object {
"elaborate": "Expected integer as prop, got boolean.",
"explain": "Expected integer as prop, got boolean.",
"elaborate": "Expected array index as prop, got boolean.",
"explain": "Expected array index as prop, got boolean.",
"left": Array [
2,
],
Expand All @@ -8328,8 +8328,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 55`] = `
Object {
"elaborate": "Expected integer as prop, got string.",
"explain": "Expected integer as prop, got string.",
"elaborate": "Expected array index as prop, got string.",
"explain": "Expected array index as prop, got string.",
"left": Array [
2,
],
Expand All @@ -8339,8 +8339,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 56`] = `
Object {
"elaborate": "Expected integer as prop, got function.",
"explain": "Expected integer as prop, got function.",
"elaborate": "Expected array index as prop, got function.",
"explain": "Expected array index as prop, got function.",
"left": Array [
2,
],
Expand All @@ -8350,8 +8350,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 57`] = `
Object {
"elaborate": "Expected integer as prop, got function.",
"explain": "Expected integer as prop, got function.",
"elaborate": "Expected array index as prop, got function.",
"explain": "Expected array index as prop, got function.",
"left": Array [
2,
],
Expand All @@ -8361,8 +8361,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 58`] = `
Object {
"elaborate": "Expected integer as prop, got object.",
"explain": "Expected integer as prop, got object.",
"elaborate": "Expected array index as prop, got object.",
"explain": "Expected array index as prop, got object.",
"left": Array [
2,
],
Expand All @@ -8374,8 +8374,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 59`] = `
Object {
"elaborate": "Expected integer as prop, got array.",
"explain": "Expected integer as prop, got array.",
"elaborate": "Expected array index as prop, got array.",
"explain": "Expected array index as prop, got array.",
"left": Array [
2,
],
Expand All @@ -8387,8 +8387,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 60`] = `
Object {
"elaborate": "Expected integer as prop, got undefined.",
"explain": "Expected integer as prop, got undefined.",
"elaborate": "Expected array index as prop, got undefined.",
"explain": "Expected array index as prop, got undefined.",
"left": Array [
2,
],
Expand All @@ -8398,8 +8398,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 61`] = `
Object {
"elaborate": "Expected integer as prop, got null.",
"explain": "Expected integer as prop, got null.",
"elaborate": "Expected array index as prop, got null.",
"explain": "Expected array index as prop, got null.",
"left": Array [
2,
],
Expand Down Expand Up @@ -8579,8 +8579,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 80`] = `
Object {
"elaborate": "Expected integer as prop, got number.",
"explain": "Expected integer as prop, got number.",
"elaborate": "Expected array index as prop, got other number.",
"explain": "Expected array index as prop, got other number.",
"left": Array [
2,
],
Expand All @@ -8590,8 +8590,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 81`] = `
Object {
"elaborate": "Expected integer as prop, got number.",
"explain": "Expected integer as prop, got number.",
"elaborate": "Expected array index as prop, got other number.",
"explain": "Expected array index as prop, got other number.",
"left": Array [
2,
],
Expand All @@ -8601,8 +8601,8 @@ Object {

exports[`Member expression type combinations: Invalid type combinations return TypeError 82`] = `
Object {
"elaborate": "Expected integer as prop, got number.",
"explain": "Expected integer as prop, got number.",
"elaborate": "Expected array index as prop, got other number.",
"explain": "Expected array index as prop, got other number.",
"left": Array [
2,
],
Expand Down
6 changes: 5 additions & 1 deletion src/utils/rttc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export const checkMemberAccess = (node: es.Node, obj: Value, prop: Value) => {
if (isObject(obj)) {
return isString(prop) ? undefined : new TypeError(node, ' as prop', 'string', typeOf(prop))
} else if (isArray(obj)) {
return isArrayIndex(prop) ? undefined : new TypeError(node, ' as prop', 'integer', typeOf(prop))
return isArrayIndex(prop)
? undefined
: isNumber(prop)
? new TypeError(node, ' as prop', 'array index', 'other number')
: new TypeError(node, ' as prop', 'array index', typeOf(prop))
} else {
return new TypeError(node, '', 'object or array', typeOf(obj))
}
Expand Down

0 comments on commit 81f5705

Please sign in to comment.