diff --git a/acorn.js b/acorn.js index 2a7c89ef0f..90eee0664b 100644 --- a/acorn.js +++ b/acorn.js @@ -2510,14 +2510,17 @@ expect(_braceL); while (!eat(_braceR)) { var method = startNode(); - if (tokType === _name && tokVal === "static") { - next(); + var isGenerator = eat(_star); + parsePropertyName(method); + if (tokType !== _parenL && !method.computed && method.key.type === "Identifier" && + method.key.name === "static") { + if (isGenerator) unexpected(); method['static'] = true; + isGenerator = eat(_star); + parsePropertyName(method); } else { method['static'] = false; } - var isGenerator = eat(_star); - parsePropertyName(method); if (tokType !== _parenL && !method.computed && method.key.type === "Identifier" && (method.key.name === "get" || method.key.name === "set")) { if (isGenerator) unexpected(); diff --git a/acorn_loose.js b/acorn_loose.js index 33a3cfe9d4..010a2d7fdf 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -862,7 +862,7 @@ var prop = startNode(), isGenerator; if (options.ecmaVersion >= 6) { if (isClass) { - if (prop['static'] = (token.type === tt.name && token.value === "static")) next(); + prop['static'] = false; } else { prop.method = false; prop.shorthand = false; @@ -871,6 +871,16 @@ } parsePropertyName(prop); if (isDummy(prop.key)) { if (isDummy(parseExpression(true))) next(); eat(tt.comma); continue; } + if (isClass) { + if (prop.key.type === "Identifier" && !prop.computed && prop.key.name === "static" && + (token.type != tt.parenL && token.type != tt.braceL)) { + prop['static'] = true; + isGenerator = eat(tt.star); + parsePropertyName(prop); + } else { + prop['static'] = false; + } + } if (!isClass && eat(tt.colon)) { prop.kind = "init"; prop.value = parseExpression(true); @@ -883,7 +893,7 @@ } prop.value = parseMethod(isGenerator); } else if (options.ecmaVersion >= 5 && prop.key.type === "Identifier" && - (prop.key.name === "get" || prop.key.name === "set") && + !prop.computed && (prop.key.name === "get" || prop.key.name === "set") && (token.type != tt.comma && token.type != tt.braceR)) { prop.kind = prop.key.name; parsePropertyName(prop); diff --git a/test/tests-harmony.js b/test/tests-harmony.js index bb8406582b..e38275483e 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -14598,3 +14598,160 @@ test("var [localVar = defaultValue] = obj", { locations: true, loose: false }); + +// https://github.com/marijnh/acorn/issues/191 + +test("try {} catch ({message}) {}", { + type: "Program", + range: [0, 27], + body: [{ + type: "TryStatement", + range: [0, 27], + block: { + type: "BlockStatement", + range: [4, 6], + body: [] + }, + handler: { + type: "CatchClause", + range: [7, 27], + param: { + type: "ObjectPattern", + range: [14, 23], + properties: [{ + type: "Property", + range: [15, 22], + method: false, + shorthand: true, + computed: false, + key: { + type: "Identifier", + range: [15, 22], + name: "message" + }, + kind: "init", + value: { + type: "Identifier", + range: [15, 22], + name: "message" + } + }] + }, + guard: null, + body: { + type: "BlockStatement", + range: [25, 27], + body: [] + } + }, + guardedHandlers: [], + finalizer: null + }] +}, { + ecmaVersion: 6, + ranges: true, + locations: true, + loose: false +}); + +// https://github.com/marijnh/acorn/issues/192 + +test("class A { static() {} }", { + type: "Program", + range: [0, 23], + body: [{ + type: "ClassDeclaration", + range: [0, 23], + id: { + type: "Identifier", + range: [6, 7], + name: "A" + }, + superClass: null, + body: { + type: "ClassBody", + range: [8, 23], + body: [{ + type: "MethodDefinition", + range: [10, 21], + computed: false, + key: { + type: "Identifier", + range: [10, 16], + name: "static" + }, + static: false, + kind: "", + value: { + type: "FunctionExpression", + range: [16, 21], + id: null, + params: [], + defaults: [], + rest: null, + generator: false, + body: { + type: "BlockStatement", + range: [19, 21], + body: [] + }, + expression: false + } + }] + } + }] +}, { + ecmaVersion: 6, + ranges: true, + locations: true +}); + +test("class A { *static() {} }", { + type: "Program", + range: [0, 24], + body: [{ + type: "ClassDeclaration", + range: [0, 24], + id: { + type: "Identifier", + range: [6, 7], + name: "A" + }, + superClass: null, + body: { + type: "ClassBody", + range: [8, 24], + body: [{ + type: "MethodDefinition", + range: [10, 22], + computed: false, + key: { + type: "Identifier", + range: [11, 17], + name: "static" + }, + static: false, + kind: "", + value: { + type: "FunctionExpression", + range: [17, 22], + id: null, + params: [], + defaults: [], + rest: null, + generator: true, + body: { + type: "BlockStatement", + range: [20, 22], + body: [] + }, + expression: false + } + }] + } + }] +}, { + ecmaVersion: 6, + ranges: true, + locations: true +});