static properties

This commit is contained in:
Fábio Santos
2015-10-27 00:51:47 +00:00
parent 5f7cb6939c
commit 9ffed2bea6
4 changed files with 50 additions and 4 deletions

View File

@@ -473,7 +473,10 @@ var AST_Arrow = DEFNODE("Arrow", null, {
$documentation: "An ES6 Arrow function ((a) => b)"
}, AST_Lambda);
var AST_ConciseMethod = DEFNODE("ConciseMethod", null, {
var AST_ConciseMethod = DEFNODE("ConciseMethod", "static", {
$propdoc: {
static: "[boolean] whether this method is static (classes only)",
},
$documentation: "An ES6 concise method inside an object or class"
}, AST_Lambda);
@@ -947,11 +950,17 @@ var AST_ObjectSymbol = DEFNODE("ObjectSymbol", "symbol", {
}
}, AST_ObjectProperty);
var AST_ObjectSetter = DEFNODE("ObjectSetter", null, {
var AST_ObjectSetter = DEFNODE("ObjectSetter", "static", {
$propdoc: {
static: "[boolean] whether this is a static setter (classes only)"
},
$documentation: "An object setter property",
}, AST_ObjectProperty);
var AST_ObjectGetter = DEFNODE("ObjectGetter", null, {
var AST_ObjectGetter = DEFNODE("ObjectGetter", "static", {
$propdoc: {
static: "[boolean] whether this is a static getter (classes only)"
},
$documentation: "An object getter property",
}, AST_ObjectProperty);

View File

@@ -775,9 +775,11 @@ function OutputStream(options) {
var self = this;
if (!nokeyword) {
output.print("function");
}
if (self.name) {
output.space();
}
}
if (self.name) {
self.name.print(output);
}
output.with_parens(function(){
@@ -839,6 +841,10 @@ function OutputStream(options) {
if (needs_parens) { output.print(")") }
});
DEFPRINT(AST_ConciseMethod, function(self, output){
if (self.static) {
output.print("static");
output.space();
}
self._do_print(output, true /* do not print "function" */);
});
@@ -1238,12 +1244,20 @@ function OutputStream(options) {
self.value.print(output);
});
DEFPRINT(AST_ObjectSetter, function(self, output){
if (self.static) {
output.print("static");
output.space();
}
output.print("set");
output.space();
self.key.print(output);
self.value._do_print(output, true);
});
DEFPRINT(AST_ObjectGetter, function(self, output){
if (self.static) {
output.print("static");
output.space();
}
output.print("get");
output.space();
self.key.print(output);

View File

@@ -1533,9 +1533,16 @@ function parse($TEXT, options) {
}
function concise_method_or_getset(name, start) {
var is_static = false;
if (name === "static" && !is("punc", "(")) {
is_static = true;
name = S.token.value;
next();
}
if (is("punc", "(")) {
return new AST_ConciseMethod({
start : start,
static : is_static,
name : new AST_SymbolMethod({ name: name }),
argnames : params_or_seq_().as_params(croak),
body : _function_body(true),
@@ -1545,6 +1552,7 @@ function parse($TEXT, options) {
if (name == "get") {
return new AST_ObjectGetter({
start : start,
static: is_static,
key : as_atom_node(),
value : function_(AST_Accessor),
end : prev()
@@ -1553,6 +1561,7 @@ function parse($TEXT, options) {
if (name == "set") {
return new AST_ObjectSetter({
start : start,
static: is_static,
key : as_atom_node(),
value : function_(AST_Accessor),
end : prev()

View File

@@ -200,6 +200,20 @@ classes: {
expect_exact: "class SomeClass{constructor(){}foo(){}}class NoSemi{constructor(...args){}foo(){}}class ChildClass extends SomeClass{}var asExpression=class AsExpression{};var nameless=class{};"
}
class_statics: {
input: {
x = class {
static staticMethod() {}
static get foo() {}
static set bar() {}
static() { /* "static" can be a method name! */ }
get() { /* "get" can be a method name! */ }
set() { /* "set" can be a method name! */ }
}
}
expect_exact: "x=class{static staticMethod(){}static get foo(){}static set bar(){}static(){}get(){}set(){}};"
}
number_literals: {
input: {
0b1001;