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

@@ -15,7 +15,6 @@ const buildGetDescriptor = template(`
Object.getOwnPropertyDescriptor(TARGET, PROPERTY);
`);
const buildGetObjectInitializer = template(`
(TEMP = Object.getOwnPropertyDescriptor(TARGET, PROPERTY), (TEMP = TEMP ? TEMP.value : undefined), {
enumerable: true,
@@ -88,7 +87,9 @@ export default function({ types: t }) {
*/
function ensureApplyDecoratedDescriptorHelper(path, state) {
if (!state.applyDecoratedDescriptor) {
state.applyDecoratedDescriptor = path.scope.generateUidIdentifier("applyDecoratedDescriptor");
state.applyDecoratedDescriptor = path.scope.generateUidIdentifier(
"applyDecoratedDescriptor",
);
const helper = buildApplyDecoratedDescriptor({
NAME: state.applyDecoratedDescriptor,
});
@@ -103,7 +104,9 @@ export default function({ types: t }) {
*/
function ensureInitializerDefineProp(path, state) {
if (!state.initializerDefineProp) {
state.initializerDefineProp = path.scope.generateUidIdentifier("initDefineProp");
state.initializerDefineProp = path.scope.generateUidIdentifier(
"initDefineProp",
);
const helper = buildInitializerDefineProperty({
NAME: state.initializerDefineProp,
});
@@ -119,7 +122,9 @@ export default function({ types: t }) {
*/
function ensureInitializerWarning(path, state) {
if (!state.initializerWarningHelper) {
state.initializerWarningHelper = path.scope.generateUidIdentifier("initializerWarningHelper");
state.initializerWarningHelper = path.scope.generateUidIdentifier(
"initializerWarningHelper",
);
const helper = buildInitializerWarningHelper({
NAME: state.initializerWarningHelper,
});
@@ -135,20 +140,29 @@ export default function({ types: t }) {
*/
function applyEnsureOrdering(path) {
// TODO: This should probably also hoist computed properties.
const decorators = (
path.isClass()
const decorators = (path.isClass()
? [path].concat(path.get("body.body"))
: path.get("properties")
).reduce((acc, prop) => acc.concat(prop.node.decorators || []), []);
: path.get("properties")).reduce(
(acc, prop) => acc.concat(prop.node.decorators || []),
[],
);
const identDecorators = decorators.filter((decorator) => !t.isIdentifier(decorator.expression));
const identDecorators = decorators.filter(
decorator => !t.isIdentifier(decorator.expression),
);
if (identDecorators.length === 0) return;
return t.sequenceExpression(identDecorators.map((decorator) => {
const expression = decorator.expression;
const id = decorator.expression = path.scope.generateDeclaredUidIdentifier("dec");
return t.assignmentExpression("=", id, expression);
}).concat([path.node]));
return t.sequenceExpression(
identDecorators
.map(decorator => {
const expression = decorator.expression;
const id = (decorator.expression = path.scope.generateDeclaredUidIdentifier(
"dec",
));
return t.assignmentExpression("=", id, expression);
})
.concat([path.node]),
);
}
/**
@@ -164,15 +178,15 @@ export default function({ types: t }) {
const name = classPath.scope.generateDeclaredUidIdentifier("class");
return decorators
.map((dec) => dec.expression)
.reverse()
.reduce(function(acc, decorator) {
return buildClassDecorator({
CLASS_REF: name,
DECORATOR: decorator,
INNER: acc,
}).expression;
}, classPath.node);
.map(dec => dec.expression)
.reverse()
.reduce(function(acc, decorator) {
return buildClassDecorator({
CLASS_REF: name,
DECORATOR: decorator,
INNER: acc,
}).expression;
}, classPath.node);
}
/**
@@ -207,7 +221,9 @@ export default function({ types: t }) {
* A helper to pull out property decorators into a sequence expression.
*/
function applyTargetDecorators(path, state, decoratedProps) {
const name = path.scope.generateDeclaredUidIdentifier(path.isClass() ? "class" : "obj");
const name = path.scope.generateDeclaredUidIdentifier(
path.isClass() ? "class" : "obj",
);
const exprs = decoratedProps.reduce(function(acc, node) {
const decorators = node.decorators || [];
@@ -216,58 +232,79 @@ export default function({ types: t }) {
if (decorators.length === 0) return acc;
if (node.computed) {
throw path.buildCodeFrameError("Computed method/property decorators are not yet supported.");
throw path.buildCodeFrameError(
"Computed method/property decorators are not yet supported.",
);
}
const property = t.isLiteral(node.key) ? node.key : t.stringLiteral(node.key.name);
const property = t.isLiteral(node.key)
? node.key
: t.stringLiteral(node.key.name);
const target = (path.isClass() && !node.static) ? buildClassPrototype({
CLASS_REF: name,
}).expression : name;
const target =
path.isClass() && !node.static
? buildClassPrototype({
CLASS_REF: name,
}).expression
: name;
if (t.isClassProperty(node, { static: false })) {
const descriptor = path.scope.generateDeclaredUidIdentifier("descriptor");
const initializer = node.value ?
t.functionExpression(null, [], t.blockStatement([t.returnStatement(node.value)])) :
t.nullLiteral();
node.value = t.callExpression(
ensureInitializerWarning(path, state), [descriptor, t.thisExpression()]
const descriptor = path.scope.generateDeclaredUidIdentifier(
"descriptor",
);
const initializer = node.value
? t.functionExpression(
null,
[],
t.blockStatement([t.returnStatement(node.value)]),
)
: t.nullLiteral();
node.value = t.callExpression(ensureInitializerWarning(path, state), [
descriptor,
t.thisExpression(),
]);
acc = acc.concat([
t.assignmentExpression(
"=", descriptor, t.callExpression(ensureApplyDecoratedDescriptorHelper(path, state), [
target,
property,
t.arrayExpression(decorators.map((dec) => dec.expression)),
t.objectExpression([
t.objectProperty(t.identifier("enumerable"), t.booleanLiteral(true)),
t.objectProperty(t.identifier("initializer"), initializer),
]),
])
"=",
descriptor,
t.callExpression(
ensureApplyDecoratedDescriptorHelper(path, state),
[
target,
property,
t.arrayExpression(decorators.map(dec => dec.expression)),
t.objectExpression([
t.objectProperty(
t.identifier("enumerable"),
t.booleanLiteral(true),
),
t.objectProperty(t.identifier("initializer"), initializer),
]),
],
),
),
]);
} else {
acc = acc.concat(
t.callExpression(ensureApplyDecoratedDescriptorHelper(path, state), [
target,
property,
t.arrayExpression(decorators.map((dec) => dec.expression)),
(
t.isObjectProperty(node) ||
t.isClassProperty(node, { static: true })) ?
buildGetObjectInitializer({
t.callExpression(ensureApplyDecoratedDescriptorHelper(path, state), [
target,
property,
t.arrayExpression(decorators.map(dec => dec.expression)),
t.isObjectProperty(node) ||
t.isClassProperty(node, { static: true })
? buildGetObjectInitializer({
TEMP: path.scope.generateDeclaredUidIdentifier("init"),
TARGET: target,
PROPERTY: property,
}).expression : buildGetDescriptor({
}).expression
: buildGetDescriptor({
TARGET: target,
PROPERTY: property,
}
).expression,
target,
])
}).expression,
target,
]),
);
}
@@ -289,35 +326,42 @@ export default function({ types: t }) {
if (!path.get("declaration").isClassDeclaration()) return;
const { node } = path;
const ref = node.declaration.id || path.scope.generateUidIdentifier("default");
const ref =
node.declaration.id || path.scope.generateUidIdentifier("default");
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) {
const { node } = path;
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) {
// Create a replacement for the class node if there is one. We do one pass to replace classes with
// class decorators, and a second pass to process method decorators.
const decoratedClass = (
const decoratedClass =
applyEnsureOrdering(path) ||
applyClassDecorators(path, state) ||
applyMethodDecorators(path, state)
);
applyMethodDecorators(path, state);
if (decoratedClass) path.replaceWith(decoratedClass);
},
ObjectExpression(path, state) {
const decoratedObject = applyEnsureOrdering(path) || applyObjectDecorators(path, state);
const decoratedObject =
applyEnsureOrdering(path) || applyObjectDecorators(path, state);
if (decoratedObject) path.replaceWith(decoratedObject);
},
@@ -328,14 +372,22 @@ export default function({ types: t }) {
if (!path.get("left").isMemberExpression()) return;
if (!path.get("left.property").isIdentifier()) return;
if (!path.get("right").isCallExpression()) return;
if (!path.get("right.callee").isIdentifier({ name: state.initializerWarningHelper.name })) return;
if (
!path
.get("right.callee")
.isIdentifier({ name: state.initializerWarningHelper.name })
) {
return;
}
path.replaceWith(t.callExpression(ensureInitializerDefineProp(path, state), [
path.get("left.object").node,
t.stringLiteral(path.get("left.property").node.name),
path.get("right.arguments")[0].node,
path.get("right.arguments")[1].node,
]));
path.replaceWith(
t.callExpression(ensureInitializerDefineProp(path, state), [
path.get("left.object").node,
t.stringLiteral(path.get("left.property").node.name),
path.get("right.arguments")[0].node,
path.get("right.arguments")[1].node,
]),
);
},
},
};