Browse Source

Require commas in object literals. Fixes #82

Dmitry Panov 6 years ago
parent
commit
2a18e28a73
2 changed files with 14 additions and 8 deletions
  1. 4 3
      parser/expression.go
  2. 10 5
      parser/parser_test.go

+ 4 - 3
parser/expression.go

@@ -272,9 +272,10 @@ func (self *_parser) parseObjectLiteral() ast.Expression {
 	for self.token != token.RIGHT_BRACE && self.token != token.EOF {
 	for self.token != token.RIGHT_BRACE && self.token != token.EOF {
 		property := self.parseObjectProperty()
 		property := self.parseObjectProperty()
 		value = append(value, property)
 		value = append(value, property)
-		if self.token == token.COMMA {
-			self.next()
-			continue
+		if self.token != token.RIGHT_BRACE {
+			self.expect(token.COMMA)
+		} else {
+			break
 		}
 		}
 	}
 	}
 	idx1 := self.expect(token.RIGHT_BRACE)
 	idx1 := self.expect(token.RIGHT_BRACE)

+ 10 - 5
parser/parser_test.go

@@ -360,11 +360,6 @@ func TestParserErr(t *testing.T) {
 
 
 		test(`if(0) do { } while(0); else do { } while(0)`, nil)
 		test(`if(0) do { } while(0); else do { } while(0)`, nil)
 
 
-		if false {
-			// TODO When strict mode is implemented
-			test("(function () { 'use strict'; delete abc; }())", "")
-		}
-
 		test("_: _: while (true) {]", "(anonymous): Line 1:4 Label '_' already exists")
 		test("_: _: while (true) {]", "(anonymous): Line 1:4 Label '_' already exists")
 
 
 		test("_:\n_:\nwhile (true) {]", "(anonymous): Line 2:1 Label '_' already exists")
 		test("_:\n_:\nwhile (true) {]", "(anonymous): Line 2:1 Label '_' already exists")
@@ -438,6 +433,16 @@ func TestParserErr(t *testing.T) {
 			test("super", "(anonymous): Line 1:1 Unexpected reserved word")
 			test("super", "(anonymous): Line 1:1 Unexpected reserved word")
 			test("abc.super = 1", nil)
 			test("abc.super = 1", nil)
 			test("var super;", "(anonymous): Line 1:5 Unexpected reserved word")
 			test("var super;", "(anonymous): Line 1:5 Unexpected reserved word")
+			test(`
+			obj = {
+			  aaa: 1
+			  bbb: "string"
+			};`, "(anonymous): Line 4:6 Unexpected identifier")
+			test("{}", nil)
+			test("{a: 1}", nil)
+			test("{a: 1,}", nil)
+			test("{a: 1, b: 2}", nil)
+			test("{a: 1, b: 2,}", nil)
 		}
 		}
 
 
 		{ // Reserved words (strict)
 		{ // Reserved words (strict)