Move more class state out of replaceSupers (#7750)

Yes, the output is uglier. But, this is necessary for me to refactor
`replaceSupers` for #7733, which is necessary for both #7555 and
https://github.com/babel/babel/pull/7553#issuecomment-381434519.

I'm still in the middle of cleaning up all this code. Don't expect
`transformClass` to survive much longer as it's written currently.
This commit is contained in:
Justin Ridgewell
2018-04-21 22:56:14 -04:00
committed by GitHub
parent 3616137864
commit 0a257e8972
35 changed files with 103 additions and 107 deletions

View File

@@ -218,8 +218,34 @@ export default function transformClass(
replaceSupers.replace();
// TODO this needs to be cleaned up. But, one step at a time.
const state = {
returns: [],
bareSupers: new Set(),
};
path.traverse(
traverse.visitors.merge([
environmentVisitor,
{
ReturnStatement(path, state) {
if (!path.getFunctionParent().isArrowFunctionExpression()) {
state.returns.push(path);
}
},
Super(path, state) {
const { node, parentPath } = path;
if (parentPath.isCallExpression({ callee: node })) {
state.bareSupers.add(parentPath);
}
},
},
]),
state,
);
if (isConstructor) {
pushConstructor(replaceSupers, node, path);
pushConstructor(state, node, path);
} else {
pushMethod(node, path);
}
@@ -382,7 +408,16 @@ export default function transformClass(
}
for (const thisPath of classState.superThises) {
thisPath.replaceWith(thisRef());
const { node, parentPath } = thisPath;
if (parentPath.isMemberExpression({ object: node })) {
thisPath.replaceWith(thisRef());
continue;
}
thisPath.replaceWith(
t.callExpression(classState.file.addHelper("assertThisInitialized"), [
thisRef(),
]),
);
}
let wrapReturn;