Skip to content

Commit

Permalink
feat(Parser): parse es6 shorthand initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
zebrooks committed Jan 17, 2016
1 parent a875d74 commit 0d28ff9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/parser.js
Expand Up @@ -350,13 +350,21 @@ export class ParserImplementation {
do {
// TODO(kasperl): Stricter checking. Only allow identifiers
// and strings as keys. Maybe also keywords?
let value = this.peek.value;
keys.push(typeof value === 'string' ? value : this.peek.text);
let peek = this.peek;
let value = peek.value;
keys.push(typeof value === 'string' ? value : peek.text);

this.advance();
this.expect(':');
if ( peek.key && (this.peek.text === ',' || this.peek.text === '}') )
{
--this.index;
values.push(this.parseAccessOrCallScope());
}
else {
this.expect(':');
values.push(this.parseExpression());
}

values.push(this.parseExpression());
} while (this.optional(','));
}

Expand Down
12 changes: 12 additions & 0 deletions test/parser.spec.js
Expand Up @@ -212,4 +212,16 @@ describe('Parser', () => {
expect(expression.values[0] instanceof AccessThis).toBe(true);
expect(expression.values[0].ancestor).toBe(1);
});

it('parses es6 shorthand LiteralObject', () => {
let expression = parser.parse('{ foo, bar }');
expect(expression instanceof LiteralObject).toBe(true);
expect(expression.keys.length).toBe(2);
expect(expression.values.length).toBe(2);

expect(expression.values[0] instanceof AccessScope).toBe(true);
expect(expression.values[0].name).toBe('foo');
expect(expression.values[1] instanceof AccessScope).toBe(true);
expect(expression.values[1].name).toBe('bar');
});
});

0 comments on commit 0d28ff9

Please sign in to comment.