Merge branch 'master' into 7.0

* master:
  Upgrade flow to 0.41
  Throw error if new.target is used outside of a function (#402)
  Fix watch command (#403)
  Update yarn lock
  Fix parsing of class properties (#351)

# Conflicts:
#	src/parser/statement.js
#	test/fixtures/experimental/class-constructor-call/illegal-key/options.json
This commit is contained in:
Daniel Tschinder
2017-03-10 13:47:45 +01:00
42 changed files with 3794 additions and 81 deletions

View File

@@ -624,11 +624,18 @@ pp.parseClass = function (node, isStatement, optionalId) {
};
pp.isClassProperty = function () {
return this.match(tt.eq) || this.isLineTerminator();
return this.match(tt.eq) || this.match(tt.semi) || this.match(tt.braceR);
};
pp.isClassMutatorStarter = function () {
return false;
pp.isClassMethod = function () {
return this.match(tt.parenL);
};
pp.isNonstaticConstructor = function (method) {
return !method.computed && !method.static && (
(method.key.name === "constructor") || // Identifier
(method.key.value === "constructor") // Literal
);
};
pp.parseClassBody = function (node) {
@@ -665,79 +672,91 @@ pp.parseClassBody = function (node) {
decorators = [];
}
const isMaybeStatic = this.match(tt.name) && this.state.value === "static";
let isGenerator = this.eat(tt.star);
let isGetSet = false;
let isAsync = false;
this.parsePropertyName(method);
method.static = isMaybeStatic && !this.match(tt.parenL);
if (method.static) {
isGenerator = this.eat(tt.star);
this.parsePropertyName(method);
}
if (!isGenerator) {
if (this.isClassProperty()) {
method.static = false;
if (this.match(tt.name) && this.state.value === "static") {
const key = this.parseIdentifier(true); // eats 'static'
if (this.isClassMethod()) {
// a method named 'static'
method.kind = "method";
method.computed = false;
method.key = key;
this.parseClassMethod(classBody, method, false, false);
continue;
} else if (this.isClassProperty()) {
// a property named 'static'
method.computed = false;
method.key = key;
classBody.body.push(this.parseClassProperty(method));
continue;
}
// otherwise something static
method.static = true;
}
const isAsyncMethod = !this.match(tt.parenL) && !method.computed && method.key.type === "Identifier" && method.key.name === "async";
if (isAsyncMethod) {
if (this.hasPlugin("asyncGenerators") && this.eat(tt.star)) isGenerator = true;
isAsync = true;
if (this.eat(tt.star)) {
// a generator
method.kind = "method";
this.parsePropertyName(method);
}
method.kind = "method";
if (!method.computed) {
let { key } = method;
// handle get/set methods
// eg. class Foo { get bar() {} set bar() {} }
if (!isAsync && !isGenerator && !this.isClassMutatorStarter() && key.type === "Identifier" && !this.match(tt.parenL) && (key.name === "get" || key.name === "set")) {
isGetSet = true;
if (this.isNonstaticConstructor(method)) {
this.raise(method.key.start, "Constructor can't be a generator");
}
if (!method.computed && method.static && (method.key.name === "prototype" || method.key.value === "prototype")) {
this.raise(method.key.start, "Classes may not have static property named prototype");
}
this.parseClassMethod(classBody, method, true, false);
} else {
const isSimple = this.match(tt.name);
const key = this.parsePropertyName(method);
if (!method.computed && method.static && (method.key.name === "prototype" || method.key.value === "prototype")) {
this.raise(method.key.start, "Classes may not have static property named prototype");
}
if (this.isClassMethod()) {
// a normal method
if (this.isNonstaticConstructor(method)) {
if (hadConstructor) {
this.raise(key.start, "Duplicate constructor in the same class");
} else if (method.decorators) {
this.raise(method.start, "You can't attach decorators to a class constructor");
}
hadConstructor = true;
method.kind = "constructor";
} else {
method.kind = "method";
}
this.parseClassMethod(classBody, method, false, false);
} else if (this.isClassProperty()) {
// a normal property
if (this.isNonstaticConstructor(method)) {
this.raise(method.key.start, "Classes may not have a non-static field named 'constructor'");
}
classBody.body.push(this.parseClassProperty(method));
} else if (isSimple && key.name === "async" && !this.isLineTerminator()) {
// an async method
const isGenerator = this.hasPlugin("asyncGenerators") && this.eat(tt.star);
method.kind = "method";
this.parsePropertyName(method);
if (this.isNonstaticConstructor(method)) {
this.raise(method.key.start, "Constructor can't be an async function");
}
this.parseClassMethod(classBody, method, isGenerator, true);
} else if (isSimple && (key.name === "get" || key.name === "set") && !(this.isLineTerminator() && this.match(tt.star))) { // `get\n*` is an uninitialized property named 'get' followed by a generator.
// a getter or setter
method.kind = key.name;
key = this.parsePropertyName(method);
this.parsePropertyName(method);
if (this.isNonstaticConstructor(method)) {
this.raise(method.key.start, "Constructor can't have get/set modifier");
}
this.parseClassMethod(classBody, method, false, false);
this.checkGetterSetterParamCount(method);
} else if (this.isLineTerminator()) {
// an uninitialized class property (due to ASI, since we don't otherwise recognize the next token)
if (this.isNonstaticConstructor(method)) {
this.raise(method.key.start, "Classes may not have a non-static field named 'constructor'");
}
classBody.body.push(this.parseClassProperty(method));
} else {
this.unexpected();
}
// disallow invalid constructors
const isConstructor = !method.static && (
(key.name === "constructor") || // Identifier
(key.value === "constructor") // Literal
);
if (isConstructor) {
if (hadConstructor) this.raise(key.start, "Duplicate constructor in the same class");
if (isGetSet) this.raise(key.start, "Constructor can't have get/set modifier");
if (isGenerator) this.raise(key.start, "Constructor can't be a generator");
if (isAsync) this.raise(key.start, "Constructor can't be an async function");
method.kind = "constructor";
hadConstructor = true;
}
// disallow static prototype method
const isStaticPrototype = method.static && (
(key.name === "prototype") || // Identifier
(key.value === "prototype") // Literal
);
if (isStaticPrototype) {
this.raise(key.start, "Classes may not have static property named prototype");
}
}
// disallow decorators on class constructors
if (method.kind === "constructor" && method.decorators) {
this.raise(method.start, "You can't attach decorators to a class constructor");
}
this.parseClassMethod(classBody, method, isGenerator, isAsync);
if (isGetSet) {
this.checkGetterSetterParamCount(method);
}
}

View File

@@ -1112,6 +1112,13 @@ export default function (instance) {
};
});
// determine whether or not we're currently in the position where a class method would appear
instance.extend("isClassMethod", function (inner) {
return function () {
return this.isRelational("<") || inner.call(this);
};
});
// determine whether or not we're currently in the position where a class property would appear
instance.extend("isClassProperty", function (inner) {
return function () {
@@ -1437,14 +1444,4 @@ export default function (instance) {
return this.match(tt.colon) || inner.call(this);
};
});
instance.extend("isClassMutatorStarter", function (inner) {
return function () {
if (this.isRelational("<")) {
return true;
} else {
return inner.call(this);
}
};
});
}