Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat($parse): support trailing commas in object & array literals
Browse files Browse the repository at this point in the history
Per ECMAScript 5.1 specification trailing commas are allowed in object and
array literals. All modern browsers as well as IE>8 support this syntax.
This commit adds support for such syntax to Angular expressions.
  • Loading branch information
mgol committed Feb 25, 2014
1 parent c99dd22 commit 6b049c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ Parser.prototype = {
var allConstant = true;
if (this.peekToken().text !== ']') {
do {
if (this.peek(']')) {
// Support trailing commas per ES5.1.
break;
}
var elementFn = this.expression();
elementFns.push(elementFn);
if (!elementFn.constant) {
Expand All @@ -811,6 +815,10 @@ Parser.prototype = {
var allConstant = true;
if (this.peekToken().text !== '}') {
do {
if (this.peek('}')) {
// Support trailing commas per ES5.1.
break;
}
var token = this.expect(),
key = token.string || token.text;
this.consume(':');
Expand Down
5 changes: 5 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ describe('parser', function() {
expect(scope.$eval("[1, 2]").length).toEqual(2);
expect(scope.$eval("[1, 2]")[0]).toEqual(1);
expect(scope.$eval("[1, 2]")[1]).toEqual(2);
expect(scope.$eval("[1, 2,]")[1]).toEqual(2);
expect(scope.$eval("[1, 2,]").length).toEqual(2);
});

it('should evaluate array access', function() {
Expand All @@ -474,6 +476,9 @@ describe('parser', function() {
expect(toJson(scope.$eval("{a:'b'}"))).toEqual('{"a":"b"}');
expect(toJson(scope.$eval("{'a':'b'}"))).toEqual('{"a":"b"}');
expect(toJson(scope.$eval("{\"a\":'b'}"))).toEqual('{"a":"b"}');
expect(toJson(scope.$eval("{a:'b',}"))).toEqual('{"a":"b"}');
expect(toJson(scope.$eval("{'a':'b',}"))).toEqual('{"a":"b"}');
expect(toJson(scope.$eval("{\"a\":'b',}"))).toEqual('{"a":"b"}');
});

it('should evaluate object access', function() {
Expand Down

0 comments on commit 6b049c7

Please sign in to comment.