Skip to content

Commit

Permalink
Merge pull request #116 from dinoboff/fix/computed-property-access-115
Browse files Browse the repository at this point in the history
fix: allow literal computed property access (#115)
  • Loading branch information
dinoboff authored Jan 24, 2017
2 parents f13b371 + 426e99d commit 595fc2f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/parser/statement/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class Node {
this.inferredType = this.inferType(scope);
}

/**
* AST node type.
*
* @return {string} This expression node type
*/
get type() {
return this.astNode.type;
}

/**
* Hook called before type inferring.
*
Expand Down
26 changes: 21 additions & 5 deletions lib/parser/statement/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ class MemberNode extends Node {

this.assertType(objectType, ['RuleDataSnapshot', 'string'], {msg});

if (objectType === 'RuleDataSnapshot' && this.computed) {
throw new ParseError(this, 'Invalid property access');
}

if (types.isPrimitive(objectType)) {
objectType = this.object.inferredType = 'string';
}
Expand All @@ -111,7 +107,27 @@ class MemberNode extends Node {
}

inferType() {
return this.computed ? 'any' : this.property.inferredType;
if (!this.computed) {
return this.property.inferredType;
}

if (this.property.type !== 'Literal') {
return 'any';
}

const scope = MemberNode.properties[this.object.inferredType];

if (scope == null) {
return 'any';
}

const type = scope[this.property.value];

if (type == null) {
throw new ParseError(this, 'Invalid property access.');
}

return type;
}

inferAsStringMethod() {
Expand Down
20 changes: 20 additions & 0 deletions test/spec/lib/parser/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,26 @@
"user": "unauth",
"isValid": true,
"failAtRuntime": true
},
{
"rule": "root[\"exists\"]() == false",
"user": "unauth",
"isValid": true,
"failAtRuntime": false,
"evaluateTo": true
},
{
"rule": "root[\"exi\" + \"sts\"]() == false",
"user": "unauth",
"isValid": false
},
{
"rule": "root[$foo]() == false",
"user": "unauth",
"wildchildren": {
"$foo": "exists"
},
"isValid": false
}
]
}

0 comments on commit 595fc2f

Please sign in to comment.