Run prettier

This commit is contained in:
Brian Ng
2017-06-27 12:15:00 -05:00
parent 93cc22dae1
commit e4b35f680d
307 changed files with 6742 additions and 4080 deletions

View File

@@ -2,7 +2,7 @@ import LooseTransformer from "./loose";
import VanillaTransformer from "./vanilla";
import nameFunction from "babel-helper-function-name";
export default function ({ types: t }) {
export default function({ types: t }) {
// todo: investigate traversal requeueing
const VISITED = Symbol();
@@ -12,15 +12,17 @@ export default function ({ types: t }) {
if (!path.get("declaration").isClassDeclaration()) return;
const { node } = path;
const ref = node.declaration.id || path.scope.generateUidIdentifier("class");
const ref =
node.declaration.id || path.scope.generateUidIdentifier("class");
node.declaration.id = ref;
// Split the class declaration and the export into two separate statements.
path.replaceWith(node.declaration);
path.insertAfter(t.exportNamedDeclaration(
null,
[t.exportSpecifier(ref, t.identifier("default"))]
));
path.insertAfter(
t.exportNamedDeclaration(null, [
t.exportSpecifier(ref, t.identifier("default")),
]),
);
},
ClassDeclaration(path) {
@@ -28,9 +30,11 @@ export default function ({ types: t }) {
const ref = node.id || path.scope.generateUidIdentifier("class");
path.replaceWith(t.variableDeclaration("let", [
t.variableDeclarator(ref, t.toExpression(node)),
]));
path.replaceWith(
t.variableDeclaration("let", [
t.variableDeclarator(ref, t.toExpression(node)),
]),
);
},
ClassExpression(path, state) {
@@ -47,7 +51,10 @@ export default function ({ types: t }) {
path.replaceWith(new Constructor(path, state.file).run());
if (path.isCallExpression() && path.get("callee").isArrowFunctionExpression()) {
if (
path.isCallExpression() &&
path.get("callee").isArrowFunctionExpression()
) {
path.get("callee").arrowFunctionToExpression();
}
},

View File

@@ -1,7 +1,10 @@
import type { Scope } from "babel-traverse";
import * as t from "babel-types";
export default function (decorators: Array<Object>, scope: Scope): Array<Object> {
export default function(
decorators: Array<Object>,
scope: Scope,
): Array<Object> {
for (const decorator of decorators) {
const expression = decorator.expression;
if (!t.isMemberExpression(expression)) continue;
@@ -18,13 +21,15 @@ export default function (decorators: Array<Object>, scope: Scope): Array<Object>
ref = expression.object;
}
nodes.push(t.callExpression(
t.memberExpression(
t.memberExpression(ref, expression.property, expression.computed),
t.identifier("bind")
nodes.push(
t.callExpression(
t.memberExpression(
t.memberExpression(ref, expression.property, expression.computed),
t.identifier("bind"),
),
[ref],
),
[ref]
));
);
if (nodes.length === 1) {
decorator.expression = nodes[0];

View File

@@ -13,10 +13,22 @@ export default class LooseClassTransformer extends VanillaTransformer {
// use assignments instead of define properties for loose classes
let classRef = this.classRef;
if (!node.static) classRef = t.memberExpression(classRef, t.identifier("prototype"));
const methodName = t.memberExpression(classRef, node.key, node.computed || t.isLiteral(node.key));
if (!node.static) {
classRef = t.memberExpression(classRef, t.identifier("prototype"));
}
const methodName = t.memberExpression(
classRef,
node.key,
node.computed || t.isLiteral(node.key),
);
let func = t.functionExpression(null, node.params, node.body, node.generator, node.async);
let func = t.functionExpression(
null,
node.params,
node.body,
node.generator,
node.async,
);
func.returnType = node.returnType;
const key = t.toComputedKey(node, node.key);
if (t.isStringLiteral(key)) {
@@ -27,7 +39,9 @@ export default class LooseClassTransformer extends VanillaTransformer {
});
}
const expr = t.expressionStatement(t.assignmentExpression("=", methodName, func));
const expr = t.expressionStatement(
t.assignmentExpression("=", methodName, func),
);
t.inheritsComments(expr, node);
this.body.push(expr);
return true;

View File

@@ -22,47 +22,61 @@ const noMethodVisitor = {
},
};
const verifyConstructorVisitor = visitors.merge([noMethodVisitor, {
MemberExpression: {
exit(path) {
const objectPath = path.get("object");
if (this.isDerived && !this.hasBareSuper && objectPath.isSuper()) {
const hasArrowFunctionParent = path.findParent((p) => p.isArrowFunctionExpression());
if (!hasArrowFunctionParent) {
throw objectPath.buildCodeFrameError("'super.*' is not allowed before super()");
const verifyConstructorVisitor = visitors.merge([
noMethodVisitor,
{
MemberExpression: {
exit(path) {
const objectPath = path.get("object");
if (this.isDerived && !this.hasBareSuper && objectPath.isSuper()) {
const hasArrowFunctionParent = path.findParent(p =>
p.isArrowFunctionExpression(),
);
if (!hasArrowFunctionParent) {
throw objectPath.buildCodeFrameError(
"'super.*' is not allowed before super()",
);
}
}
},
},
CallExpression: {
exit(path) {
if (path.get("callee").isSuper()) {
this.hasBareSuper = true;
if (!this.isDerived) {
throw path.buildCodeFrameError(
"super() is only allowed in a derived constructor",
);
}
}
},
},
ThisExpression(path) {
if (this.isDerived && !this.hasBareSuper) {
const fn = path.find(p => p.isFunction());
if (!fn || !fn.isArrowFunctionExpression()) {
throw path.buildCodeFrameError(
"'this' is not allowed before super()",
);
}
}
},
},
]);
CallExpression: {
exit(path) {
if (path.get("callee").isSuper()) {
this.hasBareSuper = true;
if (!this.isDerived) {
throw path.buildCodeFrameError("super() is only allowed in a derived constructor");
}
}
const findThisesVisitor = visitors.merge([
noMethodVisitor,
{
ThisExpression(path) {
this.superThises.push(path);
},
},
ThisExpression(path) {
if (this.isDerived && !this.hasBareSuper) {
const fn = path.find((p) => p.isFunction());
if (!fn || !fn.isArrowFunctionExpression()) {
throw path.buildCodeFrameError("'this' is not allowed before super()");
}
}
},
}]);
const findThisesVisitor = visitors.merge([noMethodVisitor, {
ThisExpression(path) {
this.superThises.push(path);
},
}]);
]);
export default class ClassTransformer {
constructor(path: NodePath, file) {
@@ -92,8 +106,9 @@ export default class ClassTransformer {
this.classId = this.node.id;
// this is the name of the binding that will **always** reference the class we've constructed
this.classRef = this.node.id ? t.identifier(this.node.id.name) :
this.scope.generateUidIdentifier("class");
this.classRef = this.node.id
? t.identifier(this.node.id.name)
: this.scope.generateUidIdentifier("class");
this.superName = this.node.superClass || t.identifier("Function");
this.isDerived = !!this.node.superClass;
@@ -106,7 +121,7 @@ export default class ClassTransformer {
//
const constructorBody = this.constructorBody = t.blockStatement([]);
const constructorBody = (this.constructorBody = t.blockStatement([]));
this.constructor = this.buildConstructor();
//
@@ -129,15 +144,17 @@ export default class ClassTransformer {
// make sure this class isn't directly called (with A() instead new A())
if (!this.isLoose) {
constructorBody.body.unshift(t.expressionStatement(t.callExpression(
file.addHelper("classCallCheck"), [
t.thisExpression(),
this.classRef,
]
)));
constructorBody.body.unshift(
t.expressionStatement(
t.callExpression(file.addHelper("classCallCheck"), [
t.thisExpression(),
this.classRef,
]),
),
);
}
body = body.concat(this.staticPropBody.map((fn) => fn(this.classRef)));
body = body.concat(this.staticPropBody.map(fn => fn(this.classRef)));
if (this.classId) {
// named class with only a constructor
@@ -147,7 +164,10 @@ export default class ClassTransformer {
//
body.push(t.returnStatement(this.classRef));
const container = t.arrowFunctionExpression(closureParams, t.blockStatement(body));
const container = t.arrowFunctionExpression(
closureParams,
t.blockStatement(body),
);
return t.callExpression(container, closureArgs);
}
@@ -201,12 +221,12 @@ export default class ClassTransformer {
body = t.blockStatement([]);
}
this.path.get("body").unshiftContainer("body", t.classMethod(
"constructor",
t.identifier("constructor"),
params,
body
));
this.path
.get("body")
.unshiftContainer(
"body",
t.classMethod("constructor", t.identifier("constructor"), params, body),
);
}
buildBody() {
@@ -216,7 +236,9 @@ export default class ClassTransformer {
if (this.userConstructor) {
const constructorBody = this.constructorBody;
constructorBody.body = constructorBody.body.concat(this.userConstructor.body.body);
constructorBody.body = constructorBody.body.concat(
this.userConstructor.body.body,
);
t.inherits(this.constructor, this.userConstructor);
t.inherits(constructorBody, this.userConstructor.body);
}
@@ -236,7 +258,8 @@ export default class ClassTransformer {
if (node.decorators) {
throw path.buildCodeFrameError(
"Method has decorators, put the decorator plugin before the classes one.");
"Method has decorators, put the decorator plugin before the classes one.",
);
}
if (t.isClassMethod(node)) {
@@ -246,21 +269,26 @@ export default class ClassTransformer {
path.traverse(verifyConstructorVisitor, this);
if (!this.hasBareSuper && this.isDerived) {
throw path.buildCodeFrameError("missing super() call in constructor");
throw path.buildCodeFrameError(
"missing super() call in constructor",
);
}
}
const replaceSupers = new ReplaceSupers({
forceSuperMemoisation: isConstructor,
methodPath: path,
methodNode: node,
objectRef: this.classRef,
superRef: this.superName,
isStatic: node.static,
isLoose: this.isLoose,
scope: this.scope,
file: this.file,
}, true);
const replaceSupers = new ReplaceSupers(
{
forceSuperMemoisation: isConstructor,
methodPath: path,
methodNode: node,
objectRef: this.classRef,
superRef: this.superName,
isStatic: node.static,
isLoose: this.isLoose,
scope: this.scope,
file: this.file,
},
true,
);
replaceSupers.replace();
@@ -298,8 +326,12 @@ export default class ClassTransformer {
}
if (instanceProps || staticProps) {
if (instanceProps) instanceProps = defineMap.toComputedObjectFromClass(instanceProps);
if (staticProps) staticProps = defineMap.toComputedObjectFromClass(staticProps);
if (instanceProps) {
instanceProps = defineMap.toComputedObjectFromClass(instanceProps);
}
if (staticProps) {
staticProps = defineMap.toComputedObjectFromClass(staticProps);
}
const nullNode = t.nullLiteral();
@@ -330,10 +362,11 @@ export default class ClassTransformer {
}
args = args.slice(0, lastNonNullIndex + 1);
body.push(t.expressionStatement(
t.callExpression(this.file.addHelper("createClass"), args)
));
body.push(
t.expressionStatement(
t.callExpression(this.file.addHelper("createClass"), args),
),
);
}
this.clearDescriptors();
@@ -353,13 +386,21 @@ export default class ClassTransformer {
if (
bareSuperNode.arguments.length === 2 &&
t.isSpreadElement(bareSuperNode.arguments[1]) &&
t.isIdentifier(bareSuperNode.arguments[1].argument, { name: "arguments" })
t.isIdentifier(bareSuperNode.arguments[1].argument, {
name: "arguments",
})
) {
// special case single arguments spread
bareSuperNode.arguments[1] = bareSuperNode.arguments[1].argument;
bareSuperNode.callee = t.memberExpression(superRef, t.identifier("apply"));
bareSuperNode.callee = t.memberExpression(
superRef,
t.identifier("apply"),
);
} else {
bareSuperNode.callee = t.memberExpression(superRef, t.identifier("call"));
bareSuperNode.callee = t.memberExpression(
superRef,
t.identifier("call"),
);
}
} else {
bareSuperNode = optimiseCall(
@@ -367,12 +408,15 @@ export default class ClassTransformer {
"||",
t.memberExpression(this.classRef, t.identifier("__proto__")),
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[this.classRef]
)
t.memberExpression(
t.identifier("Object"),
t.identifier("getPrototypeOf"),
),
[this.classRef],
),
),
t.thisExpression(),
bareSuperNode.arguments
bareSuperNode.arguments,
);
}
@@ -383,11 +427,11 @@ export default class ClassTransformer {
} else {
call = t.callExpression(
this.file.addHelper("possibleConstructorReturn"),
[t.thisExpression(), bareSuperNode]
[t.thisExpression(), bareSuperNode],
);
}
const bareSuperAfter = this.bareSuperAfter.map((fn) => fn(thisRef));
const bareSuperAfter = this.bareSuperAfter.map(fn => fn(thisRef));
if (
bareSuper.parentPath.isExpressionStatement() &&
@@ -409,14 +453,11 @@ export default class ClassTransformer {
bareSuper.parentPath.replaceWith(t.returnStatement(call));
} else {
bareSuper.replaceWithMultiple([
t.variableDeclaration("var", [
t.variableDeclarator(thisRef, call),
]),
t.variableDeclaration("var", [t.variableDeclarator(thisRef, call)]),
...bareSuperAfter,
t.expressionStatement(thisRef),
]);
}
}
verifyConstructor() {
@@ -436,7 +477,7 @@ export default class ClassTransformer {
this.wrapSuperCall(bareSuper, superRef, thisRef, body);
if (guaranteedSuperBeforeFinish) {
bareSuper.find(function (parentPath) {
bareSuper.find(function(parentPath) {
// hit top so short circuit
if (parentPath === path) {
return true;
@@ -457,31 +498,38 @@ export default class ClassTransformer {
let wrapReturn;
if (this.isLoose) {
wrapReturn = (returnArg) => {
return returnArg ? t.logicalExpression("||", returnArg, thisRef) : thisRef;
wrapReturn = returnArg => {
return returnArg
? t.logicalExpression("||", returnArg, thisRef)
: thisRef;
};
} else {
wrapReturn = (returnArg) => t.callExpression(
this.file.addHelper("possibleConstructorReturn"),
[thisRef].concat(returnArg || [])
);
wrapReturn = returnArg =>
t.callExpression(
this.file.addHelper("possibleConstructorReturn"),
[thisRef].concat(returnArg || []),
);
}
// if we have a return as the last node in the body then we've already caught that
// return
const bodyPaths = body.get("body");
if (bodyPaths.length && !bodyPaths.pop().isReturnStatement()) {
body.pushContainer("body", t.returnStatement(
guaranteedSuperBeforeFinish ? thisRef : wrapReturn()));
body.pushContainer(
"body",
t.returnStatement(guaranteedSuperBeforeFinish ? thisRef : wrapReturn()),
);
}
for (const returnPath of this.superReturns) {
if (returnPath.node.argument) {
const ref = returnPath.scope.generateDeclaredUidIdentifier("ret");
returnPath.get("argument").replaceWithMultiple([
t.assignmentExpression("=", ref, returnPath.node.argument),
wrapReturn(ref),
]);
returnPath
.get("argument")
.replaceWithMultiple([
t.assignmentExpression("=", ref, returnPath.node.argument),
wrapReturn(ref),
]);
} else {
returnPath.get("argument").replaceWith(wrapReturn());
}
@@ -510,7 +558,11 @@ export default class ClassTransformer {
* Replace the constructor body of our class.
*/
pushConstructor(replaceSupers, method: { type: "ClassMethod" }, path: NodePath) {
pushConstructor(
replaceSupers,
method: { type: "ClassMethod" },
path: NodePath,
) {
this.bareSupers = replaceSupers.bareSupers;
this.superReturns = replaceSupers.returns;
@@ -561,9 +613,15 @@ export default class ClassTransformer {
// Unshift to ensure that the constructor inheritance is set up before
// any properties can be assigned to the prototype.
this.pushedInherits = true;
this.body.unshift(t.expressionStatement(t.callExpression(
this.isLoose ? this.file.addHelper("inheritsLoose") : this.file.addHelper("inherits"),
[this.classRef, this.superName]
)));
this.body.unshift(
t.expressionStatement(
t.callExpression(
this.isLoose
? this.file.addHelper("inheritsLoose")
: this.file.addHelper("inherits"),
[this.classRef, this.superName],
),
),
);
}
}