From e0b630264948cd3071c1447cdfaad062d83862a6 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 14 Dec 2014 13:19:22 +1100 Subject: [PATCH] add support for private declarations --- acorn.js | 24 +++++++-- test/tests-6to5.js | 128 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 4 deletions(-) diff --git a/acorn.js b/acorn.js index 4557bc7efe..56cc6cfc0c 100644 --- a/acorn.js +++ b/acorn.js @@ -2008,7 +2008,9 @@ return parseLabeledStatement(node, maybeName, expr); } - if (expr.name === "declare") { + if (options.ecmaVersion >= 7 && expr.name === "private" && tokType === _name) { + return parsePrivate(node); + } else if (expr.name === "declare") { if (tokType === _class || tokType === _name || tokType === _function || tokType === _var) { return parseDeclare(node); } @@ -2988,6 +2990,15 @@ } } + function parsePrivate(node) { + node.declarations = []; + do { + node.declarations.push(parseIdent()); + } while (eat(_comma)); + semicolon(); + return finishNode(node, "PrivateDeclaration"); + } + // Parse a class declaration or literal (depending on the // `isStatement` parameter). @@ -3010,15 +3021,20 @@ expect(_braceL); while (!eat(_braceR)) { var method = startNode(); + if (options.ecmaVersion >= 7 && tokType === _name && tokVal === "private") { + next(); + classBody.body.push(parsePrivate(method)); + continue; + } if (tokType === _name && tokVal === "static") { next(); method['static'] = true; - } else { + } else { method['static'] = false; - } + } var isAsync = false; var isGenerator = eat(_star); - if (options.ecmaVersion >= 7 && !isGenerator && tokType === _name && tokVal === "async") { + if (options.ecmaVersion >= 7 && !isGenerator && tokType === _name && tokVal === "async") { var asyncId = parseIdent(); if (tokType === _colon || tokType === _parenL) { method.key = asyncId; diff --git a/test/tests-6to5.js b/test/tests-6to5.js index 87416eca36..a0a24fe647 100644 --- a/test/tests-6to5.js +++ b/test/tests-6to5.js @@ -2088,3 +2088,131 @@ test('delete foo::bar;', { }, { ecmaVersion: 7 }); + +test("private A;", { + type: "Program", + start: 0, + end: 10, + body: [{ + type: "PrivateDeclaration", + start: 0, + end: 10, + declarations: [{ + type: "Identifier", + start: 8, + end: 9, + name: "A" + }] + }] +}, { + ecmaVersion: 7 +}); + +test("private A, B;", { + type: "Program", + start: 0, + end: 13, + body: [{ + type: "PrivateDeclaration", + start: 0, + end: 13, + declarations: [ + { + type: "Identifier", + start: 8, + end: 9, + name: "A" + }, + { + type: "Identifier", + start: 11, + end: 12, + name: "B" + } + ] + }] +}, { + ecmaVersion: 7 +}); + +test("class A { private A; }", { + type: "Program", + start: 0, + end: 22, + body: [{ + type: "ClassDeclaration", + start: 0, + end: 22, + id: { + type: "Identifier", + start: 6, + end: 7, + name: "A" + }, + superClass: null, + body: { + type: "ClassBody", + start: 8, + end: 22, + body: [{ + type: "PrivateDeclaration", + start: 10, + end: 20, + declarations: [ + { + type: "Identifier", + start: 18, + end: 19, + name: "A" + } + ] + }] + } + }] +}, { + ecmaVersion: 7 +}); + +test("class A { private A, B; }", { + type: "Program", + start: 0, + end: 25, + body: [{ + type: "ClassDeclaration", + start: 0, + end: 25, + id: { + type: "Identifier", + start: 6, + end: 7, + name: "A" + }, + superClass: null, + body: { + type: "ClassBody", + start: 8, + end: 25, + body: [{ + type: "PrivateDeclaration", + start: 10, + end: 23, + declarations: [ + { + type: "Identifier", + start: 18, + end: 19, + name: "A" + }, + { + type: "Identifier", + start: 21, + end: 22, + name: "B" + } + ] + }] + } + }] +}, { + ecmaVersion: 7 +});