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

@@ -20,10 +20,9 @@ const buildExportAll = template(`
}
`);
const TYPE_IMPORT = "Import";
export default function ({ types: t }) {
export default function({ types: t }) {
const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
const reassignmentVisitor = {
@@ -49,20 +48,22 @@ export default function ({ types: t }) {
// in order to ensure the same update expression value
let isPostUpdateExpression = path.isUpdateExpression() && !node.prefix;
if (isPostUpdateExpression) {
if (node.operator === "++")
{node = t.binaryExpression("+", node.argument, t.numericLiteral(1));}
else if (node.operator === "--")
{node = t.binaryExpression("-", node.argument, t.numericLiteral(1));}
else
{isPostUpdateExpression = false;}
if (node.operator === "++") {
node = t.binaryExpression("+", node.argument, t.numericLiteral(1));
} else if (node.operator === "--") {
node = t.binaryExpression("-", node.argument, t.numericLiteral(1));
} else {
isPostUpdateExpression = false;
}
}
for (const exportedName of exportedNames) {
node = this.buildCall(exportedName, node).expression;
}
if (isPostUpdateExpression)
{node = t.sequenceExpression([node, path.node]);}
if (isPostUpdateExpression) {
node = t.sequenceExpression([node, path.node]);
}
path.replaceWith(node);
},
@@ -70,18 +71,26 @@ export default function ({ types: t }) {
return {
visitor: {
CallExpression(path, state) {
if (path.node.callee.type === TYPE_IMPORT) {
const contextIdent = state.contextIdent;
path.replaceWith(t.callExpression(t.memberExpression(contextIdent, t.identifier("import")),
path.node.arguments));
path.replaceWith(
t.callExpression(
t.memberExpression(contextIdent, t.identifier("import")),
path.node.arguments,
),
);
}
},
ReferencedIdentifier(path, state) {
if (path.node.name == "__moduleName" && !path.scope.hasBinding("__moduleName")) {
path.replaceWith(t.memberExpression(state.contextIdent, t.identifier("id")));
if (
path.node.name == "__moduleName" &&
!path.scope.hasBinding("__moduleName")
) {
path.replaceWith(
t.memberExpression(state.contextIdent, t.identifier("id")),
);
}
},
@@ -109,20 +118,22 @@ export default function ({ types: t }) {
function pushModule(source, key, specifiers) {
let module;
modules.forEach(function (m) {
modules.forEach(function(m) {
if (m.key === source) {
module = m;
}
});
if (!module) {
modules.push(module = { key: source, imports: [], exports: [] });
modules.push(
(module = { key: source, imports: [], exports: [] }),
);
}
module[key] = module[key].concat(specifiers);
}
function buildExportCall(name, val) {
return t.expressionStatement(
t.callExpression(exportIdent, [t.stringLiteral(name), val])
t.callExpression(exportIdent, [t.stringLiteral(name), val]),
);
}
@@ -154,7 +165,10 @@ export default function ({ types: t }) {
path.remove();
} else if (path.isExportDefaultDeclaration()) {
const declar = path.get("declaration");
if (declar.isClassDeclaration() || declar.isFunctionDeclaration()) {
if (
declar.isClassDeclaration() ||
declar.isFunctionDeclaration()
) {
const id = declar.node.id;
const nodes = [];
@@ -163,7 +177,9 @@ export default function ({ types: t }) {
nodes.push(buildExportCall("default", id));
addExportName(id.name, "default");
} else {
nodes.push(buildExportCall("default", t.toExpression(declar.node)));
nodes.push(
buildExportCall("default", t.toExpression(declar.node)),
);
}
if (!canHoist || declar.isClassDeclaration()) {
@@ -212,8 +228,16 @@ export default function ({ types: t }) {
const nodes = [];
for (const specifier of specifiers) {
nodes.push(buildExportCall(specifier.exported.name, specifier.local));
addExportName(specifier.local.name, specifier.exported.name);
nodes.push(
buildExportCall(
specifier.exported.name,
specifier.local,
),
);
addExportName(
specifier.local.name,
specifier.exported.name,
);
}
path.replaceWithMultiple(nodes);
@@ -223,65 +247,103 @@ export default function ({ types: t }) {
}
}
modules.forEach(function (specifiers) {
modules.forEach(function(specifiers) {
const setterBody = [];
const target = path.scope.generateUidIdentifier(specifiers.key);
for (let specifier of specifiers.imports) {
if (t.isImportNamespaceSpecifier(specifier)) {
setterBody.push(t.expressionStatement(t.assignmentExpression("=", specifier.local, target)));
setterBody.push(
t.expressionStatement(
t.assignmentExpression("=", specifier.local, target),
),
);
} else if (t.isImportDefaultSpecifier(specifier)) {
specifier = t.importSpecifier(specifier.local, t.identifier("default"));
specifier = t.importSpecifier(
specifier.local,
t.identifier("default"),
);
}
if (t.isImportSpecifier(specifier)) {
setterBody.push(t.expressionStatement(t.assignmentExpression("=", specifier.local,
t.memberExpression(target, specifier.imported))));
setterBody.push(
t.expressionStatement(
t.assignmentExpression(
"=",
specifier.local,
t.memberExpression(target, specifier.imported),
),
),
);
}
}
if (specifiers.exports.length) {
const exportObjRef = path.scope.generateUidIdentifier("exportObj");
const exportObjRef = path.scope.generateUidIdentifier(
"exportObj",
);
setterBody.push(t.variableDeclaration("var", [
t.variableDeclarator(exportObjRef, t.objectExpression([])),
]));
setterBody.push(
t.variableDeclaration("var", [
t.variableDeclarator(exportObjRef, t.objectExpression([])),
]),
);
for (const node of specifiers.exports) {
if (t.isExportAllDeclaration(node)) {
setterBody.push(buildExportAll({
KEY: path.scope.generateUidIdentifier("key"),
EXPORT_OBJ: exportObjRef,
TARGET: target,
}));
setterBody.push(
buildExportAll({
KEY: path.scope.generateUidIdentifier("key"),
EXPORT_OBJ: exportObjRef,
TARGET: target,
}),
);
} else if (t.isExportSpecifier(node)) {
setterBody.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(exportObjRef, node.exported),
t.memberExpression(target, node.local))
));
setterBody.push(
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(exportObjRef, node.exported),
t.memberExpression(target, node.local),
),
),
);
} else {
// todo
}
}
setterBody.push(t.expressionStatement(t.callExpression(exportIdent, [exportObjRef])));
setterBody.push(
t.expressionStatement(
t.callExpression(exportIdent, [exportObjRef]),
),
);
}
sources.push(t.stringLiteral(specifiers.key));
setters.push(t.functionExpression(null, [target], t.blockStatement(setterBody)));
setters.push(
t.functionExpression(
null,
[target],
t.blockStatement(setterBody),
),
);
});
let moduleName = this.getModuleName();
if (moduleName) moduleName = t.stringLiteral(moduleName);
if (canHoist) {
hoistVariables(path, (id) => variableIds.push(id));
hoistVariables(path, id => variableIds.push(id));
}
if (variableIds.length) {
beforeBody.unshift(t.variableDeclaration("var",
variableIds.map((id) => t.variableDeclarator(id))));
beforeBody.unshift(
t.variableDeclaration(
"var",
variableIds.map(id => t.variableDeclarator(id)),
),
);
}
path.traverse(reassignmentVisitor, {
@@ -297,7 +359,9 @@ export default function ({ types: t }) {
path.node.body = [
buildTemplate({
SYSTEM_REGISTER: t.memberExpression(
t.identifier(state.opts.systemGlobal || "System"), t.identifier("register")),
t.identifier(state.opts.systemGlobal || "System"),
t.identifier("register"),
),
BEFORE_BODY: beforeBody,
MODULE_NAME: moduleName,
SETTERS: setters,