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

Commit 6b049c7

Browse files
committed
feat($parse): support trailing commas in object & array literals
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.
1 parent c99dd22 commit 6b049c7

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/ng/parse.js

+8
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,10 @@ Parser.prototype = {
785785
var allConstant = true;
786786
if (this.peekToken().text !== ']') {
787787
do {
788+
if (this.peek(']')) {
789+
// Support trailing commas per ES5.1.
790+
break;
791+
}
788792
var elementFn = this.expression();
789793
elementFns.push(elementFn);
790794
if (!elementFn.constant) {
@@ -811,6 +815,10 @@ Parser.prototype = {
811815
var allConstant = true;
812816
if (this.peekToken().text !== '}') {
813817
do {
818+
if (this.peek('}')) {
819+
// Support trailing commas per ES5.1.
820+
break;
821+
}
814822
var token = this.expect(),
815823
key = token.string || token.text;
816824
this.consume(':');

test/ng/parseSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ describe('parser', function() {
460460
expect(scope.$eval("[1, 2]").length).toEqual(2);
461461
expect(scope.$eval("[1, 2]")[0]).toEqual(1);
462462
expect(scope.$eval("[1, 2]")[1]).toEqual(2);
463+
expect(scope.$eval("[1, 2,]")[1]).toEqual(2);
464+
expect(scope.$eval("[1, 2,]").length).toEqual(2);
463465
});
464466

465467
it('should evaluate array access', function() {
@@ -474,6 +476,9 @@ describe('parser', function() {
474476
expect(toJson(scope.$eval("{a:'b'}"))).toEqual('{"a":"b"}');
475477
expect(toJson(scope.$eval("{'a':'b'}"))).toEqual('{"a":"b"}');
476478
expect(toJson(scope.$eval("{\"a\":'b'}"))).toEqual('{"a":"b"}');
479+
expect(toJson(scope.$eval("{a:'b',}"))).toEqual('{"a":"b"}');
480+
expect(toJson(scope.$eval("{'a':'b',}"))).toEqual('{"a":"b"}');
481+
expect(toJson(scope.$eval("{\"a\":'b',}"))).toEqual('{"a":"b"}');
477482
});
478483

479484
it('should evaluate object access', function() {

0 commit comments

Comments
 (0)