From 2f06922544f0ee41bd40740d52aa23664399db8a Mon Sep 17 00:00:00 2001 From: Damien Lebrun Date: Tue, 24 Jan 2017 14:15:43 +0000 Subject: [PATCH] chore: better property access error message Throw during in the inferring type of computed property instead of letting the call expression parsing do it later. --- lib/parser/statement/member.js | 15 +++++++++++---- test/spec/lib/parser/fixtures.json | 5 +++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/parser/statement/member.js b/lib/parser/statement/member.js index 1ff353f..1ada939 100644 --- a/lib/parser/statement/member.js +++ b/lib/parser/statement/member.js @@ -111,20 +111,27 @@ class MemberNode extends Node { return this.property.inferredType; } - if (this.property.type !== 'Literal') { + const msg = 'Invalid property access.'; + const objectType = this.object.inferredType; + + if (types.isFuzzy(objectType)) { return 'any'; } - const scope = MemberNode.properties[this.object.inferredType]; + if (this.property.type !== 'Literal') { + throw new ParseError(this, msg); + } + + const scope = MemberNode.properties[objectType]; if (scope == null) { - return 'any'; + throw new ParseError(this, msg); } const type = scope[this.property.value]; if (type == null) { - throw new ParseError(this, 'Invalid property access.'); + throw new ParseError(this, msg); } return type; diff --git a/test/spec/lib/parser/fixtures.json b/test/spec/lib/parser/fixtures.json index ad081b3..10566ff 100644 --- a/test/spec/lib/parser/fixtures.json +++ b/test/spec/lib/parser/fixtures.json @@ -1070,6 +1070,11 @@ "failAtRuntime": false, "evaluateTo": true }, + { + "rule": "root[\"doesNotExist\"]() == true", + "user": "unauth", + "isValid": false + }, { "rule": "root[\"exi\" + \"sts\"]() == false", "user": "unauth",