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.
This commit is contained in:
Michał Gołębiowski
2013-08-21 11:25:04 +02:00
parent c99dd224a5
commit 6b049c74cc
2 changed files with 13 additions and 0 deletions

View File

@@ -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) {
@@ -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(':');

View File

@@ -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() {
@@ -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() {