Allow new.target in class properties (#759)

This commit is contained in:
Brian Ng 2017-10-14 15:50:21 -05:00 committed by Henry Zhu
parent 60ea39aa70
commit b5e6ba62db
11 changed files with 2333 additions and 7 deletions

View File

@ -1049,11 +1049,14 @@ export default class ExpressionParser extends LValParser {
if (this.eat(tt.dot)) {
const metaProp = this.parseMetaProperty(node, meta, "target");
if (!this.state.inFunction) {
this.raise(
metaProp.property.start,
"new.target can only be used in functions",
);
if (!this.state.inFunction && !this.state.inClassProperty) {
let error = "new.target can only be used in functions";
if (this.hasPlugin("classProperties")) {
error += " or class properties";
}
this.raise(metaProp.start, error);
}
return metaProp;

View File

@ -1,3 +1,3 @@
{
"throws": "new.target can only be used in functions (1:4)"
"throws": "new.target can only be used in functions (1:0)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "new.target can only be used in functions (1:12)"
"throws": "new.target can only be used in functions (1:8)"
}

View File

@ -0,0 +1 @@
var x = new.target;

View File

@ -0,0 +1,4 @@
{
"plugins": ["classProperties"],
"throws": "new.target can only be used in functions or class properties (1:8)"
}

View File

@ -0,0 +1,10 @@
class X {
static a = new.target;
static b = (foo = 1 + bar(new.target));
static c = () => new.target;
static d = (foo = new.target) => {};
e = new.target;
f = (foo = 1 + bar(new.target));
g = () => new.target;
h = (foo = new.target) => {};
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "flow"]
}

View File

@ -0,0 +1,10 @@
class X {
static a = new.target;
static b = (foo = 1 + bar(new.target));
static c = () => new.target;
static d = (foo = new.target) => {};
e = new.target;
f = (foo = 1 + bar(new.target));
g = () => new.target;
h = (foo = new.target) => {};
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties"]
}