From 7b5a1552e20adc0ba9eb749cbf7cb5c0ab69fa54 Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Fri, 2 Mar 2018 13:26:24 -0500 Subject: [PATCH] Fix off-by-one error in array expression error message h/t @drewbo --- src/style-spec/expression/definitions/at.js | 8 ++++++-- test/integration/expression-tests/at/basic/test.json | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/style-spec/expression/definitions/at.js b/src/style-spec/expression/definitions/at.js index 306eaee83bf..c47d8102c48 100644 --- a/src/style-spec/expression/definitions/at.js +++ b/src/style-spec/expression/definitions/at.js @@ -42,8 +42,12 @@ class At implements Expression { const index = ((this.index.evaluate(ctx): any): number); const array = ((this.input.evaluate(ctx): any): Array); - if (index < 0 || index >= array.length) { - throw new RuntimeError(`Array index out of bounds: ${index} > ${array.length}.`); + if (index < 0) { + throw new RuntimeError(`Array index out of bounds: ${index} < 0.`); + } + + if (index >= array.length) { + throw new RuntimeError(`Array index out of bounds: ${index} > ${array.length - 1}.`); } if (index !== Math.floor(index)) { diff --git a/test/integration/expression-tests/at/basic/test.json b/test/integration/expression-tests/at/basic/test.json index 800c8a6ca19..47ba77bccfc 100644 --- a/test/integration/expression-tests/at/basic/test.json +++ b/test/integration/expression-tests/at/basic/test.json @@ -18,8 +18,8 @@ }, "outputs": [ 8, - {"error": "Array index out of bounds: -1 > 3."}, - {"error": "Array index out of bounds: 4 > 3."}, + {"error": "Array index out of bounds: -1 < 0."}, + {"error": "Array index out of bounds: 4 > 2."}, {"error": "Array index must be an integer, but found 1.5 instead."} ], "serialized": [