delay this assignment when referencing this inside an arrow function pre-bare super in derived class constructors - fixes #1920
This commit is contained in:
@@ -52,7 +52,20 @@ var verifyConstructorVisitor = {
|
||||
|
||||
ThisExpression(node, parent, scope, state) {
|
||||
if (state.isDerived && !state.hasBareSuper) {
|
||||
throw this.errorWithNode("'this' is not allowed before super()");
|
||||
if (this.inShadow()) {
|
||||
var thisAlias = state.constructorPath.getData("this");
|
||||
|
||||
if (!thisAlias) {
|
||||
thisAlias = state.constructorPath.setData(
|
||||
"this",
|
||||
state.constructorPath.scope.generateUidIdentifier("this")
|
||||
);
|
||||
}
|
||||
|
||||
return thisAlias;
|
||||
} else {
|
||||
throw this.errorWithNode("'this' is not allowed before super()");
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -429,13 +442,21 @@ export default class ClassTransformer {
|
||||
|
||||
verifyConstructor(path: NodePath) {
|
||||
var state = {
|
||||
hasBareSuper: false,
|
||||
bareSuper: null,
|
||||
isDerived: this.isDerived,
|
||||
file: this.file
|
||||
constructorPath: path.get("value"),
|
||||
hasBareSuper: false,
|
||||
bareSuper: null,
|
||||
isDerived: this.isDerived,
|
||||
file: this.file,
|
||||
};
|
||||
|
||||
path.get("value").traverse(verifyConstructorVisitor, state);
|
||||
state.constructorPath.traverse(verifyConstructorVisitor, state);
|
||||
|
||||
var thisAlias = state.constructorPath.getData("this");
|
||||
if (thisAlias && state.bareSuper) {
|
||||
state.bareSuper.insertAfter(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(thisAlias, t.thisExpression())
|
||||
]));
|
||||
}
|
||||
|
||||
this.bareSuper = state.bareSuper;
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ export var metadata = {
|
||||
|
||||
function remap(path, key, create) {
|
||||
// ensure that we're shadowed
|
||||
if (!path.inShadow()) return;
|
||||
var shadowPath = path.inShadow();
|
||||
if (!shadowPath || shadowPath.isArrowFunctionExpression()) return;
|
||||
|
||||
var shadowFunction = path.node._shadowedFunctionLiteral;
|
||||
var currentFunction;
|
||||
|
||||
@@ -178,7 +178,7 @@ export function inShadow() {
|
||||
var path = this;
|
||||
while (path) {
|
||||
if (path.isFunction()) {
|
||||
if (path.node.shadow) {
|
||||
if (path.node.shadow || path.isArrowFunctionExpression()) {
|
||||
return path;
|
||||
} else {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user