From 6b049c74ccc9ee19688bb9bbe504c300e61776dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski?= Date: Wed, 21 Aug 2013 11:25:04 +0200 Subject: [PATCH] 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. --- src/ng/parse.js | 8 ++++++++ test/ng/parseSpec.js | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/ng/parse.js b/src/ng/parse.js index bd3aa048df44..885dc50afab9 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -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(':'); diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index 466be755d523..38fce3f78793 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -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() {