add support for private declarations

This commit is contained in:
Sebastian McKenzie 2014-12-14 13:19:22 +11:00
parent c30abbb52b
commit e0b6302649
2 changed files with 148 additions and 4 deletions

View File

@ -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;

View File

@ -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
});