2017-01-10 20:18:22 +03:00
|
|
|
"use strict";
|
2017-01-28 18:50:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
var assert = require("assert");
|
2017-01-19 22:46:37 +03:00
|
|
|
var comments = require("./comments");
|
2016-12-23 21:38:10 +03:00
|
|
|
var FastPath = require("./fast-path");
|
|
|
|
var util = require("./util");
|
2017-01-11 08:48:49 +03:00
|
|
|
var isIdentifierName = require("esutils").keyword.isIdentifierNameES6;
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-19 23:46:57 +03:00
|
|
|
var docBuilders = require("./doc-builders");
|
|
|
|
var concat = docBuilders.concat;
|
|
|
|
var join = docBuilders.join;
|
|
|
|
var line = docBuilders.line;
|
|
|
|
var hardline = docBuilders.hardline;
|
|
|
|
var softline = docBuilders.softline;
|
|
|
|
var literalline = docBuilders.literalline;
|
|
|
|
var group = docBuilders.group;
|
|
|
|
var indent = docBuilders.indent;
|
|
|
|
var conditionalGroup = docBuilders.conditionalGroup;
|
|
|
|
var ifBreak = docBuilders.ifBreak;
|
2017-02-04 00:15:21 +03:00
|
|
|
var breakParent = docBuilders.breakParent;
|
2017-02-23 20:26:26 +03:00
|
|
|
var lineSuffixBoundary = docBuilders.lineSuffixBoundary;
|
2017-01-19 23:46:57 +03:00
|
|
|
|
|
|
|
var docUtils = require("./doc-utils");
|
2017-01-24 02:47:11 +03:00
|
|
|
var willBreak = docUtils.willBreak;
|
|
|
|
var isLineNext = docUtils.isLineNext;
|
2017-01-19 23:46:57 +03:00
|
|
|
var getFirstString = docUtils.getFirstString;
|
|
|
|
var isEmpty = docUtils.isEmpty;
|
|
|
|
|
|
|
|
var types = require("ast-types");
|
|
|
|
var namedTypes = types.namedTypes;
|
|
|
|
var isString = types.builtInTypes.string;
|
|
|
|
var isObject = types.builtInTypes.object;
|
|
|
|
|
2017-02-18 06:44:55 +03:00
|
|
|
function shouldPrintComma(options, level) {
|
|
|
|
level = level || "es5";
|
|
|
|
|
2017-02-23 20:57:51 +03:00
|
|
|
switch (options.trailingComma) {
|
2017-02-18 06:44:55 +03:00
|
|
|
case "all":
|
2017-02-23 20:57:51 +03:00
|
|
|
if (level === "all") {
|
2017-02-18 06:44:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "es5":
|
2017-02-23 20:57:51 +03:00
|
|
|
if (level === "es5") {
|
2017-02-18 06:44:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "none":
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
function genericPrint(path, options, printPath) {
|
|
|
|
assert.ok(path instanceof FastPath);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
var node = path.getValue();
|
|
|
|
var parts = [];
|
|
|
|
var needsParens = false;
|
2016-12-31 07:03:22 +03:00
|
|
|
var linesWithoutParens = genericPrintNoParens(path, options, printPath);
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-31 01:00:11 +03:00
|
|
|
if (!node || isEmpty(linesWithoutParens)) {
|
2016-12-23 21:38:10 +03:00
|
|
|
return linesWithoutParens;
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-14 05:58:50 +03:00
|
|
|
// Escape hatch
|
2017-02-16 06:56:11 +03:00
|
|
|
if (
|
|
|
|
node.comments &&
|
|
|
|
node.comments.length > 0 &&
|
|
|
|
node.comments[0].value.trim() === "prettier-ignore"
|
|
|
|
) {
|
|
|
|
return options.originalText.slice(util.locStart(node), util.locEnd(node));
|
2017-02-14 05:58:50 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 07:03:22 +03:00
|
|
|
if (
|
2017-01-19 02:31:46 +03:00
|
|
|
node.decorators &&
|
2017-02-16 06:56:11 +03:00
|
|
|
node.decorators.length > 0 &&
|
|
|
|
// If the parent node is an export declaration, it will be
|
|
|
|
// responsible for printing node.decorators.
|
|
|
|
!util.getParentExportDeclaration(path)
|
2016-12-31 07:03:22 +03:00
|
|
|
) {
|
2017-01-26 00:30:09 +03:00
|
|
|
const separator = node.decorators.length === 1 &&
|
|
|
|
node.decorators[0].expression.type === "Identifier"
|
2017-01-28 18:50:22 +03:00
|
|
|
? " "
|
|
|
|
: hardline;
|
2016-12-31 07:03:22 +03:00
|
|
|
path.each(
|
|
|
|
function(decoratorPath) {
|
2017-01-26 00:30:09 +03:00
|
|
|
parts.push(printPath(decoratorPath), separator);
|
2016-12-31 07:03:22 +03:00
|
|
|
},
|
|
|
|
"decorators"
|
|
|
|
);
|
2017-01-09 20:09:04 +03:00
|
|
|
} else if (
|
2017-01-19 02:31:46 +03:00
|
|
|
util.isExportDeclaration(node) &&
|
2017-02-16 06:56:11 +03:00
|
|
|
node.declaration &&
|
|
|
|
node.declaration.decorators
|
2017-01-09 20:09:04 +03:00
|
|
|
) {
|
|
|
|
// Export declarations are responsible for printing any decorators
|
|
|
|
// that logically apply to node.declaration.
|
|
|
|
path.each(
|
|
|
|
function(decoratorPath) {
|
|
|
|
parts.push(printPath(decoratorPath), line);
|
|
|
|
},
|
|
|
|
"declaration",
|
|
|
|
"decorators"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Nodes with decorators can't have parentheses, so we can avoid
|
|
|
|
// computing path.needsParens() except in this case.
|
|
|
|
needsParens = path.needsParens();
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (needsParens) {
|
|
|
|
parts.unshift("(");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
parts.push(linesWithoutParens);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (needsParens) {
|
|
|
|
parts.push(")");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
return concat(parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
function genericPrintNoParens(path, options, print) {
|
|
|
|
var n = path.getValue();
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (!n) {
|
2017-01-20 00:20:03 +03:00
|
|
|
return "";
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (typeof n === "string") {
|
2017-01-20 00:20:03 +03:00
|
|
|
return n;
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-27 21:28:04 +03:00
|
|
|
// TODO: For some reason NumericLiteralTypeAnnotation is not
|
|
|
|
// printable so this throws, but I think that's a bug in ast-types.
|
|
|
|
// This assert isn't very useful though.
|
|
|
|
// namedTypes.Printable.assert(n);
|
2016-12-23 21:38:10 +03:00
|
|
|
var parts = [];
|
|
|
|
switch (n.type) {
|
2017-01-13 23:03:53 +03:00
|
|
|
case "File":
|
|
|
|
return path.call(print, "program");
|
|
|
|
case "Program":
|
|
|
|
// Babel 6
|
|
|
|
if (n.directives) {
|
|
|
|
path.each(
|
|
|
|
function(childPath) {
|
|
|
|
parts.push(print(childPath), ";", hardline);
|
2017-02-03 19:50:51 +03:00
|
|
|
if (
|
|
|
|
util.isNextLineEmpty(options.originalText, childPath.getValue())
|
|
|
|
) {
|
2017-01-26 00:26:09 +03:00
|
|
|
parts.push(hardline);
|
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
},
|
|
|
|
"directives"
|
|
|
|
);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(
|
|
|
|
path.call(
|
|
|
|
function(bodyPath) {
|
|
|
|
return printStatementSequence(bodyPath, options, print);
|
|
|
|
},
|
|
|
|
"body"
|
|
|
|
)
|
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-28 00:09:15 +03:00
|
|
|
parts.push(
|
2017-02-14 04:08:58 +03:00
|
|
|
comments.printDanglingComments(path, options, /* sameIndent */ true)
|
2017-01-28 00:09:15 +03:00
|
|
|
);
|
2017-02-07 18:39:04 +03:00
|
|
|
|
|
|
|
// Only force a trailing newline if there were any contents.
|
|
|
|
if (n.body.length || n.comments) {
|
|
|
|
parts.push(hardline);
|
|
|
|
}
|
2017-01-11 17:39:32 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
// Babel extension.
|
|
|
|
case "Noop":
|
|
|
|
case "EmptyStatement":
|
2017-01-20 00:20:03 +03:00
|
|
|
return "";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ExpressionStatement":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat([path.call(print, "expression"), ";"]); // Babel extension.
|
|
|
|
case "ParenthesizedExpression":
|
|
|
|
return concat(["(", path.call(print, "expression"), ")"]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "AssignmentExpression":
|
|
|
|
return group(
|
|
|
|
concat([
|
|
|
|
path.call(print, "left"),
|
|
|
|
" ",
|
|
|
|
n.operator,
|
2017-03-01 20:37:02 +03:00
|
|
|
hasLeadingOwnLineComment(options.originalText, n.right)
|
|
|
|
? indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([hardline, path.call(print, "right")])
|
|
|
|
)
|
|
|
|
: concat([" ", path.call(print, "right")])
|
2017-01-13 23:03:53 +03:00
|
|
|
])
|
|
|
|
);
|
|
|
|
case "BinaryExpression":
|
2017-01-19 01:01:17 +03:00
|
|
|
case "LogicalExpression": {
|
|
|
|
const parts = [];
|
|
|
|
printBinaryishExpressions(path, parts, print, options);
|
2017-02-09 18:36:05 +03:00
|
|
|
const parent = path.getParentNode();
|
|
|
|
|
|
|
|
// Avoid indenting sub-expressions in if/etc statements.
|
2017-02-15 17:41:06 +03:00
|
|
|
if (
|
2017-03-01 20:37:02 +03:00
|
|
|
(hasLeadingOwnLineComment(options.originalText, n) &&
|
2017-02-28 18:00:28 +03:00
|
|
|
(parent.type === "AssignmentExpression" ||
|
2017-03-01 20:37:02 +03:00
|
|
|
parent.type === "VariableDeclarator")) ||
|
2017-02-15 17:41:06 +03:00
|
|
|
shouldInlineLogicalExpression(n) ||
|
2017-02-23 20:57:51 +03:00
|
|
|
(n !== parent.body &&
|
|
|
|
(parent.type === "IfStatement" ||
|
|
|
|
parent.type === "WhileStatement" ||
|
|
|
|
parent.type === "DoStatement" ||
|
|
|
|
parent.type === "ForStatement")) ||
|
|
|
|
(n === parent.body && parent.type === "ArrowFunctionExpression")
|
2017-02-09 18:36:05 +03:00
|
|
|
) {
|
|
|
|
return group(concat(parts));
|
|
|
|
}
|
|
|
|
|
|
|
|
const rest = concat(parts.slice(1));
|
2017-01-19 01:01:17 +03:00
|
|
|
|
2017-02-03 19:50:51 +03:00
|
|
|
return group(
|
|
|
|
concat([
|
2017-01-19 02:31:46 +03:00
|
|
|
// Don't include the initial expression in the indentation
|
|
|
|
// level. The first item is guaranteed to be the first
|
|
|
|
// left-most expression.
|
|
|
|
parts.length > 0 ? parts[0] : "",
|
2017-02-09 18:36:05 +03:00
|
|
|
indent(options.tabWidth, rest)
|
2017-02-03 19:50:51 +03:00
|
|
|
])
|
|
|
|
);
|
2017-01-19 01:01:17 +03:00
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "AssignmentPattern":
|
|
|
|
return concat([
|
2017-01-09 20:09:04 +03:00
|
|
|
path.call(print, "left"),
|
2017-01-13 23:03:53 +03:00
|
|
|
" = ",
|
2017-01-09 20:09:04 +03:00
|
|
|
path.call(print, "right")
|
2017-01-13 23:03:53 +03:00
|
|
|
]);
|
|
|
|
case "MemberExpression": {
|
2017-01-09 20:09:04 +03:00
|
|
|
return concat([
|
|
|
|
path.call(print, "object"),
|
2017-01-26 22:58:47 +03:00
|
|
|
printMemberLookup(path, options, print)
|
2017-01-09 20:09:04 +03:00
|
|
|
]);
|
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "MetaProperty":
|
|
|
|
return concat([
|
|
|
|
path.call(print, "meta"),
|
|
|
|
".",
|
|
|
|
path.call(print, "property")
|
|
|
|
]);
|
|
|
|
case "BindExpression":
|
|
|
|
if (n.object) {
|
|
|
|
parts.push(path.call(print, "object"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push("::", path.call(print, "callee"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "Path":
|
|
|
|
return join(".", n.body);
|
|
|
|
case "Identifier":
|
|
|
|
return concat([
|
|
|
|
n.name,
|
|
|
|
n.optional ? "?" : "",
|
|
|
|
path.call(print, "typeAnnotation")
|
|
|
|
]);
|
|
|
|
case "SpreadElement":
|
|
|
|
case "SpreadElementPattern":
|
|
|
|
// Babel 6 for ObjectPattern
|
|
|
|
case "RestProperty":
|
|
|
|
case "SpreadProperty":
|
|
|
|
case "SpreadPropertyPattern":
|
|
|
|
case "RestElement":
|
|
|
|
return concat([
|
|
|
|
"...",
|
|
|
|
path.call(print, "argument"),
|
|
|
|
path.call(print, "typeAnnotation")
|
|
|
|
]);
|
|
|
|
case "FunctionDeclaration":
|
|
|
|
case "FunctionExpression":
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.async) parts.push("async ");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push("function");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.generator) parts.push("*");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.id) {
|
|
|
|
parts.push(" ", path.call(print, "id"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(
|
|
|
|
path.call(print, "typeParameters"),
|
2017-01-24 02:47:11 +03:00
|
|
|
group(
|
2017-01-13 23:03:53 +03:00
|
|
|
concat([
|
|
|
|
printFunctionParams(path, print, options),
|
|
|
|
printReturnType(path, print)
|
|
|
|
])
|
|
|
|
),
|
|
|
|
" ",
|
|
|
|
path.call(print, "body")
|
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ArrowFunctionExpression":
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.async) parts.push("async ");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.typeParameters) {
|
|
|
|
parts.push(path.call(print, "typeParameters"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (
|
2017-01-19 02:31:46 +03:00
|
|
|
n.params.length === 1 &&
|
2017-02-16 06:56:11 +03:00
|
|
|
!n.rest &&
|
|
|
|
n.params[0].type === "Identifier" &&
|
|
|
|
!n.params[0].typeAnnotation &&
|
|
|
|
!n.params[0].leadingComments &&
|
|
|
|
!n.params[0].trailingComments &&
|
|
|
|
!n.predicate &&
|
|
|
|
!n.returnType
|
2017-01-13 23:03:53 +03:00
|
|
|
) {
|
|
|
|
parts.push(path.call(print, "params", 0));
|
|
|
|
} else {
|
|
|
|
parts.push(
|
2017-01-24 02:47:11 +03:00
|
|
|
group(
|
2017-01-13 23:03:53 +03:00
|
|
|
concat([
|
|
|
|
printFunctionParams(path, print, options),
|
|
|
|
printReturnType(path, print)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(" =>");
|
2017-01-11 00:46:33 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
const body = path.call(print, "body");
|
2017-01-28 18:50:22 +03:00
|
|
|
const collapsed = concat([concat(parts), " ", body]);
|
2017-01-12 02:15:12 +03:00
|
|
|
|
2017-01-24 21:54:01 +03:00
|
|
|
// We want to always keep these types of nodes on the same line
|
|
|
|
// as the arrow.
|
2017-01-15 08:04:50 +03:00
|
|
|
if (
|
2017-01-19 02:31:46 +03:00
|
|
|
n.body.type === "ArrayExpression" ||
|
2017-02-16 06:56:11 +03:00
|
|
|
n.body.type === "ObjectExpression" ||
|
|
|
|
n.body.type === "JSXElement" ||
|
2017-03-09 20:08:12 +03:00
|
|
|
n.body.type === "BlockStatement" ||
|
|
|
|
n.body.type === "TaggedTemplateExpression" ||
|
|
|
|
n.body.type === "TemplateElement"
|
2017-01-15 08:04:50 +03:00
|
|
|
) {
|
2017-01-13 23:03:53 +03:00
|
|
|
return group(collapsed);
|
|
|
|
}
|
2017-01-12 02:15:12 +03:00
|
|
|
|
2017-01-24 21:54:01 +03:00
|
|
|
return group(
|
2017-03-09 20:08:12 +03:00
|
|
|
concat([
|
|
|
|
concat(parts),
|
|
|
|
group(indent(options.tabWidth, concat([line, body])))
|
|
|
|
])
|
2017-01-24 21:54:01 +03:00
|
|
|
);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "MethodDefinition":
|
|
|
|
if (n.static) {
|
|
|
|
parts.push("static ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(printMethod(path, options, print));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "YieldExpression":
|
|
|
|
parts.push("yield");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.delegate) parts.push("*");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.argument) parts.push(" ", path.call(print, "argument"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "AwaitExpression":
|
|
|
|
parts.push("await");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.all) parts.push("*");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.argument) parts.push(" ", path.call(print, "argument"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ModuleDeclaration":
|
|
|
|
parts.push("module", path.call(print, "id"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.source) {
|
|
|
|
assert.ok(!n.body);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push("from", path.call(print, "source"));
|
|
|
|
} else {
|
|
|
|
parts.push(path.call(print, "body"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return join(" ", parts);
|
|
|
|
case "ImportSpecifier":
|
|
|
|
if (n.imported) {
|
2017-02-15 23:56:34 +03:00
|
|
|
if (n.importKind) {
|
|
|
|
parts.push(path.call(print, "importKind"), " ");
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(path.call(print, "imported"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.local && n.local.name !== n.imported.name) {
|
|
|
|
parts.push(" as ", path.call(print, "local"));
|
|
|
|
}
|
|
|
|
} else if (n.id) {
|
|
|
|
parts.push(path.call(print, "id"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.name) {
|
|
|
|
parts.push(" as ", path.call(print, "name"));
|
|
|
|
}
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ExportSpecifier":
|
|
|
|
if (n.local) {
|
|
|
|
parts.push(path.call(print, "local"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.exported && n.exported.name !== n.local.name) {
|
|
|
|
parts.push(" as ", path.call(print, "exported"));
|
|
|
|
}
|
|
|
|
} else if (n.id) {
|
|
|
|
parts.push(path.call(print, "id"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.name) {
|
|
|
|
parts.push(" as ", path.call(print, "name"));
|
|
|
|
}
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ExportBatchSpecifier":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "*";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ImportNamespaceSpecifier":
|
|
|
|
parts.push("* as ");
|
|
|
|
|
|
|
|
if (n.local) {
|
|
|
|
parts.push(path.call(print, "local"));
|
|
|
|
} else if (n.id) {
|
|
|
|
parts.push(path.call(print, "id"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ImportDefaultSpecifier":
|
|
|
|
if (n.local) {
|
|
|
|
return path.call(print, "local");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return path.call(print, "id");
|
|
|
|
case "ExportDeclaration":
|
|
|
|
case "ExportDefaultDeclaration":
|
|
|
|
case "ExportNamedDeclaration":
|
|
|
|
return printExportDeclaration(path, options, print);
|
|
|
|
case "ExportAllDeclaration":
|
|
|
|
parts.push("export *");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.exported) {
|
|
|
|
parts.push(" as ", path.call(print, "exported"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(" from ", path.call(print, "source"), ";");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ExportNamespaceSpecifier":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["* as ", path.call(print, "exported")]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ExportDefaultSpecifier":
|
|
|
|
return path.call(print, "exported");
|
|
|
|
case "ImportDeclaration":
|
|
|
|
parts.push("import ");
|
|
|
|
|
2017-02-04 00:19:14 +03:00
|
|
|
const fromParts = [];
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.importKind && n.importKind !== "value") {
|
|
|
|
parts.push(n.importKind + " ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-04 00:19:14 +03:00
|
|
|
var standalones = [];
|
|
|
|
var grouped = [];
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.specifiers && n.specifiers.length > 0) {
|
|
|
|
path.each(
|
|
|
|
function(specifierPath) {
|
|
|
|
var value = specifierPath.getValue();
|
|
|
|
if (
|
|
|
|
namedTypes.ImportDefaultSpecifier.check(value) ||
|
2017-02-16 06:56:11 +03:00
|
|
|
namedTypes.ImportNamespaceSpecifier.check(value)
|
2017-01-13 23:03:53 +03:00
|
|
|
) {
|
|
|
|
standalones.push(print(specifierPath));
|
|
|
|
} else {
|
|
|
|
grouped.push(print(specifierPath));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"specifiers"
|
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (standalones.length > 0) {
|
2017-01-22 23:34:00 +03:00
|
|
|
parts.push(join(", ", standalones));
|
Break multiline imports (#167)
Follows the same pattern as https://github.com/jlongster/prettier/pull/156/files
```js
echo "import { aaaaaaaa, bbbbbb, cccccc, dddddddd, eeeeeee, fffffffff, ggggggggg } from 'vjeux';" | ./bin/prettier.js --stdin
import {
aaaaaaaa,
bbbbbb,
cccccc,
dddddddd,
eeeeeee,
fffffffff,
ggggggggg
} from "vjeux";
```
2017-01-13 20:59:33 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (standalones.length > 0 && grouped.length > 0) {
|
|
|
|
parts.push(", ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (grouped.length > 0) {
|
|
|
|
parts.push(
|
2017-01-24 21:54:01 +03:00
|
|
|
group(
|
Break multiline imports (#167)
Follows the same pattern as https://github.com/jlongster/prettier/pull/156/files
```js
echo "import { aaaaaaaa, bbbbbb, cccccc, dddddddd, eeeeeee, fffffffff, ggggggggg } from 'vjeux';" | ./bin/prettier.js --stdin
import {
aaaaaaaa,
bbbbbb,
cccccc,
dddddddd,
eeeeeee,
fffffffff,
ggggggggg
} from "vjeux";
```
2017-01-13 20:59:33 +03:00
|
|
|
concat([
|
2017-01-13 23:03:53 +03:00
|
|
|
"{",
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([
|
|
|
|
options.bracketSpacing ? line : softline,
|
2017-01-28 18:50:22 +03:00
|
|
|
join(concat([",", line]), grouped)
|
2017-01-13 23:03:53 +03:00
|
|
|
])
|
|
|
|
),
|
2017-02-18 06:44:55 +03:00
|
|
|
ifBreak(shouldPrintComma(options) ? "," : ""),
|
Break multiline imports (#167)
Follows the same pattern as https://github.com/jlongster/prettier/pull/156/files
```js
echo "import { aaaaaaaa, bbbbbb, cccccc, dddddddd, eeeeeee, fffffffff, ggggggggg } from 'vjeux';" | ./bin/prettier.js --stdin
import {
aaaaaaaa,
bbbbbb,
cccccc,
dddddddd,
eeeeeee,
fffffffff,
ggggggggg
} from "vjeux";
```
2017-01-13 20:59:33 +03:00
|
|
|
options.bracketSpacing ? line : softline,
|
2017-01-13 23:03:53 +03:00
|
|
|
"}"
|
Break multiline imports (#167)
Follows the same pattern as https://github.com/jlongster/prettier/pull/156/files
```js
echo "import { aaaaaaaa, bbbbbb, cccccc, dddddddd, eeeeeee, fffffffff, ggggggggg } from 'vjeux';" | ./bin/prettier.js --stdin
import {
aaaaaaaa,
bbbbbb,
cccccc,
dddddddd,
eeeeeee,
fffffffff,
ggggggggg
} from "vjeux";
```
2017-01-13 20:59:33 +03:00
|
|
|
])
|
2017-01-13 23:03:53 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-02-04 00:19:14 +03:00
|
|
|
fromParts.push(grouped.length === 0 ? line : " ", "from ");
|
2017-03-06 06:08:42 +03:00
|
|
|
} else if (n.importKind && n.importKind === "type") {
|
|
|
|
parts.push("{} from ");
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-04 00:19:14 +03:00
|
|
|
fromParts.push(path.call(print, "source"), ";");
|
|
|
|
|
|
|
|
// If there's a very long import, break the following way:
|
|
|
|
//
|
|
|
|
// import veryLong
|
|
|
|
// from 'verylong'
|
|
|
|
//
|
|
|
|
// In case there are grouped elements, they will already break the way
|
|
|
|
// we want and this break would take precedence instead.
|
|
|
|
if (grouped.length === 0) {
|
|
|
|
return group(
|
2017-02-09 18:44:56 +03:00
|
|
|
concat([concat(parts), indent(options.tabWidth, concat(fromParts))])
|
2017-02-04 00:19:14 +03:00
|
|
|
);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-04 00:19:14 +03:00
|
|
|
return concat([concat(parts), concat(fromParts)]);
|
2017-01-25 18:33:48 +03:00
|
|
|
|
|
|
|
case "Import": {
|
|
|
|
return "import";
|
|
|
|
}
|
2017-01-24 20:37:01 +03:00
|
|
|
case "BlockStatement": {
|
2017-01-13 23:03:53 +03:00
|
|
|
var naked = path.call(
|
|
|
|
function(bodyPath) {
|
|
|
|
return printStatementSequence(bodyPath, options, print);
|
2016-12-31 07:03:22 +03:00
|
|
|
},
|
2017-01-13 23:03:53 +03:00
|
|
|
"body"
|
2016-12-31 07:03:22 +03:00
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-15 07:39:41 +03:00
|
|
|
const hasContent = getFirstString(naked);
|
|
|
|
const hasDirectives = n.directives && n.directives.length > 0;
|
|
|
|
|
2017-01-24 20:37:01 +03:00
|
|
|
var parent = path.getParentNode();
|
2017-02-23 18:29:05 +03:00
|
|
|
const parentParent = path.getParentNode(1);
|
2017-01-28 18:50:22 +03:00
|
|
|
if (
|
|
|
|
!hasContent &&
|
2017-02-16 06:56:11 +03:00
|
|
|
!hasDirectives &&
|
|
|
|
!n.comments &&
|
|
|
|
(parent.type === "ArrowFunctionExpression" ||
|
|
|
|
parent.type === "FunctionExpression" ||
|
|
|
|
parent.type === "FunctionDeclaration" ||
|
|
|
|
parent.type === "ObjectMethod" ||
|
2017-02-23 18:29:05 +03:00
|
|
|
parent.type === "ClassMethod" ||
|
|
|
|
(parent.type === "CatchClause" && !parentParent.finalizer))
|
2017-01-28 18:50:22 +03:00
|
|
|
) {
|
2017-01-24 20:37:01 +03:00
|
|
|
return "{}";
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push("{");
|
|
|
|
|
|
|
|
// Babel 6
|
2017-01-15 07:39:41 +03:00
|
|
|
if (hasDirectives) {
|
2017-01-13 23:03:53 +03:00
|
|
|
path.each(
|
|
|
|
function(childPath) {
|
|
|
|
parts.push(
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-01-28 18:50:22 +03:00
|
|
|
concat([hardline, print(childPath), ";"])
|
2017-01-13 23:03:53 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
"directives"
|
|
|
|
);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-15 07:39:41 +03:00
|
|
|
if (hasContent) {
|
2017-01-28 18:50:22 +03:00
|
|
|
parts.push(indent(options.tabWidth, concat([hardline, naked])));
|
2017-01-19 22:46:37 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-28 00:09:15 +03:00
|
|
|
parts.push(comments.printDanglingComments(path, options));
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(hardline, "}");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
2017-01-24 20:37:01 +03:00
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ReturnStatement":
|
|
|
|
parts.push("return");
|
|
|
|
|
2017-03-09 04:06:18 +03:00
|
|
|
if (n.argument) {
|
|
|
|
if (returnArgumentHasLeadingComment(options, n.argument)) {
|
|
|
|
parts.push(
|
|
|
|
concat([
|
|
|
|
" (",
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([softline, path.call(print, "argument")])
|
|
|
|
),
|
|
|
|
line,
|
|
|
|
")"
|
|
|
|
])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
parts.push(" ", path.call(print, "argument"));
|
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(";");
|
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
case "CallExpression": {
|
2017-03-04 00:48:29 +03:00
|
|
|
if (
|
|
|
|
// We want to keep require calls as a unit
|
2017-03-04 02:39:37 +03:00
|
|
|
(n.callee.type === "Identifier" && n.callee.name === "require") ||
|
2017-03-09 00:24:28 +03:00
|
|
|
// Keep test declarations on a single line
|
|
|
|
// e.g. `it('long name', () => {`
|
2017-03-04 02:39:37 +03:00
|
|
|
(n.callee.type === "Identifier" &&
|
2017-03-09 00:24:28 +03:00
|
|
|
(n.callee.name === "it" ||
|
|
|
|
n.callee.name === "test" ||
|
|
|
|
n.callee.name === "describe") &&
|
2017-03-04 00:48:29 +03:00
|
|
|
n.arguments.length === 2 &&
|
2017-03-04 02:39:37 +03:00
|
|
|
(n.arguments[0].type === "StringLiteral" ||
|
2017-03-07 19:36:03 +03:00
|
|
|
n.arguments[0].type === "TemplateLiteral" ||
|
2017-03-04 02:39:37 +03:00
|
|
|
(n.arguments[0].type === "Literal" &&
|
|
|
|
typeof n.arguments[0].value === "string")) &&
|
|
|
|
(n.arguments[1].type === "FunctionExpression" ||
|
|
|
|
n.arguments[1].type === "ArrowFunctionExpression") &&
|
2017-03-04 00:48:29 +03:00
|
|
|
n.arguments[1].params.length <= 1)
|
|
|
|
) {
|
2017-03-03 06:21:02 +03:00
|
|
|
return concat([
|
|
|
|
path.call(print, "callee"),
|
|
|
|
concat(["(", join(", ", path.map(print, "arguments")), ")"])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-04 00:48:29 +03:00
|
|
|
// We detect calls on member lookups and possibly print them in a
|
|
|
|
// special chain format. See `printMemberChain` for more info.
|
|
|
|
if (n.callee.type === "MemberExpression") {
|
|
|
|
return printMemberChain(path, options, print);
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:09:04 +03:00
|
|
|
return concat([
|
|
|
|
path.call(print, "callee"),
|
|
|
|
printArgumentsList(path, options, print)
|
|
|
|
]);
|
|
|
|
}
|
2017-01-05 23:26:45 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ObjectExpression":
|
|
|
|
case "ObjectPattern":
|
|
|
|
case "ObjectTypeAnnotation":
|
|
|
|
var allowBreak = false;
|
|
|
|
var isTypeAnnotation = n.type === "ObjectTypeAnnotation";
|
|
|
|
// Leave this here because we *might* want to make this
|
|
|
|
// configurable later -- flow accepts ";" for type separators
|
|
|
|
var separator = isTypeAnnotation ? "," : ",";
|
|
|
|
var fields = [];
|
|
|
|
var leftBrace = n.exact ? "{|" : "{";
|
|
|
|
var rightBrace = n.exact ? "|}" : "}";
|
2017-02-13 20:03:56 +03:00
|
|
|
var parent = path.getParentNode(0);
|
2017-02-16 06:56:11 +03:00
|
|
|
var parentIsUnionTypeAnnotation = parent.type === "UnionTypeAnnotation";
|
2017-01-13 23:03:53 +03:00
|
|
|
|
|
|
|
if (isTypeAnnotation) {
|
|
|
|
fields.push("indexers", "callProperties");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
fields.push("properties");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
var props = [];
|
2017-02-09 19:08:58 +03:00
|
|
|
let separatorParts = [];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
fields.forEach(function(field) {
|
|
|
|
path.each(
|
|
|
|
function(childPath) {
|
2017-02-09 19:08:58 +03:00
|
|
|
props.push(concat(separatorParts));
|
2017-01-13 23:03:53 +03:00
|
|
|
props.push(group(print(childPath)));
|
2017-02-09 19:08:58 +03:00
|
|
|
|
|
|
|
separatorParts = [separator, line];
|
2017-02-16 06:56:11 +03:00
|
|
|
if (
|
|
|
|
util.isNextLineEmpty(options.originalText, childPath.getValue())
|
|
|
|
) {
|
2017-02-09 19:08:58 +03:00
|
|
|
separatorParts.push(hardline);
|
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
},
|
|
|
|
field
|
|
|
|
);
|
|
|
|
});
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-30 18:07:03 +03:00
|
|
|
const lastElem = util.getLast(n.properties);
|
|
|
|
const canHaveTrailingComma = !(lastElem &&
|
|
|
|
lastElem.type === "RestProperty");
|
|
|
|
|
2017-03-15 04:36:35 +03:00
|
|
|
const shouldBreak = n.type !== "ObjectPattern" &&
|
|
|
|
util.hasNewlineInRange(
|
|
|
|
options.originalText,
|
|
|
|
util.locStart(n),
|
|
|
|
util.locEnd(n)
|
|
|
|
);
|
2017-01-30 20:08:55 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (props.length === 0) {
|
2017-01-31 17:52:05 +03:00
|
|
|
return group(
|
|
|
|
concat([
|
|
|
|
"{",
|
|
|
|
comments.printDanglingComments(path, options),
|
|
|
|
softline,
|
|
|
|
"}"
|
|
|
|
])
|
|
|
|
);
|
2017-01-13 23:03:53 +03:00
|
|
|
} else {
|
2017-01-24 21:54:01 +03:00
|
|
|
return group(
|
2017-01-13 23:03:53 +03:00
|
|
|
concat([
|
|
|
|
leftBrace,
|
|
|
|
indent(
|
2017-02-13 20:03:56 +03:00
|
|
|
options.tabWidth + (parentIsUnionTypeAnnotation ? 2 : 0),
|
2017-02-16 06:56:11 +03:00
|
|
|
concat([options.bracketSpacing ? line : softline, concat(props)])
|
2017-01-13 23:03:53 +03:00
|
|
|
),
|
2017-02-23 20:57:51 +03:00
|
|
|
ifBreak(
|
|
|
|
canHaveTrailingComma && shouldPrintComma(options) ? "," : ""
|
|
|
|
),
|
2017-02-13 20:03:56 +03:00
|
|
|
indent(
|
|
|
|
parentIsUnionTypeAnnotation ? 2 : 0,
|
|
|
|
concat([options.bracketSpacing ? line : softline, rightBrace])
|
|
|
|
),
|
2017-01-13 23:03:53 +03:00
|
|
|
path.call(print, "typeAnnotation")
|
2017-01-30 20:08:55 +03:00
|
|
|
]),
|
|
|
|
{ shouldBreak }
|
2017-01-13 23:03:53 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
case "PropertyPattern":
|
|
|
|
return concat([
|
|
|
|
path.call(print, "key"),
|
|
|
|
": ",
|
|
|
|
path.call(print, "pattern")
|
|
|
|
]);
|
|
|
|
// Babel 6
|
2017-01-28 18:50:22 +03:00
|
|
|
case "ObjectProperty": // Non-standard AST node type.
|
|
|
|
case "Property":
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.method || n.kind === "get" || n.kind === "set") {
|
|
|
|
return printMethod(path, options, print);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.shorthand) {
|
|
|
|
parts.push(path.call(print, "value"));
|
|
|
|
} else {
|
|
|
|
if (n.computed) {
|
|
|
|
parts.push("[", path.call(print, "key"), "]");
|
|
|
|
} else {
|
2017-01-19 20:37:24 +03:00
|
|
|
parts.push(printPropertyKey(path, options, print));
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2017-02-15 18:20:37 +03:00
|
|
|
parts.push(concat([": ", path.call(print, "value")]));
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
|
|
|
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(parts); // Babel 6
|
|
|
|
case "ClassMethod":
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.static) {
|
|
|
|
parts.push("static ");
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = parts.concat(printObjectMethod(path, options, print));
|
|
|
|
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(parts); // Babel 6
|
|
|
|
case "ObjectMethod":
|
2017-01-13 23:03:53 +03:00
|
|
|
return printObjectMethod(path, options, print);
|
|
|
|
case "Decorator":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["@", path.call(print, "expression")]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ArrayExpression":
|
2017-01-23 07:52:30 +03:00
|
|
|
case "ArrayPattern":
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.elements.length === 0) {
|
2017-01-28 18:50:22 +03:00
|
|
|
parts.push(
|
2017-02-13 07:09:39 +03:00
|
|
|
group(
|
|
|
|
concat([
|
|
|
|
"[",
|
|
|
|
comments.printDanglingComments(path, options),
|
|
|
|
softline,
|
|
|
|
"]"
|
|
|
|
])
|
|
|
|
)
|
2017-01-28 18:50:22 +03:00
|
|
|
);
|
2017-01-13 23:03:53 +03:00
|
|
|
} else {
|
2017-01-23 02:20:47 +03:00
|
|
|
const lastElem = util.getLast(n.elements);
|
2017-01-23 20:49:46 +03:00
|
|
|
const canHaveTrailingComma = !(lastElem &&
|
|
|
|
lastElem.type === "RestElement");
|
2017-01-23 02:20:47 +03:00
|
|
|
|
2017-01-16 20:53:39 +03:00
|
|
|
// JavaScript allows you to have empty elements in an array which
|
|
|
|
// changes its length based on the number of commas. The algorithm
|
|
|
|
// is that if the last argument is null, we need to force insert
|
|
|
|
// a comma to ensure JavaScript recognizes it.
|
|
|
|
// [,].length === 1
|
|
|
|
// [1,].length === 1
|
|
|
|
// [1,,].length === 2
|
|
|
|
//
|
|
|
|
// Note that util.getLast returns null if the array is empty, but
|
|
|
|
// we already check for an empty array just above so we are safe
|
2017-01-23 20:49:46 +03:00
|
|
|
const needsForcedTrailingComma = canHaveTrailingComma &&
|
|
|
|
lastElem === null;
|
2017-01-16 20:53:39 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(
|
2017-01-24 21:54:01 +03:00
|
|
|
group(
|
2017-01-13 23:03:53 +03:00
|
|
|
concat([
|
|
|
|
"[",
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-03-15 19:35:22 +03:00
|
|
|
concat([
|
|
|
|
softline,
|
|
|
|
printArrayItems(path, options, "elements", print)
|
|
|
|
])
|
2017-01-13 23:03:53 +03:00
|
|
|
),
|
2017-01-16 20:53:39 +03:00
|
|
|
needsForcedTrailingComma ? "," : "",
|
2017-01-23 02:20:47 +03:00
|
|
|
ifBreak(
|
|
|
|
canHaveTrailingComma &&
|
2017-01-23 20:49:46 +03:00
|
|
|
!needsForcedTrailingComma &&
|
2017-02-18 06:44:55 +03:00
|
|
|
shouldPrintComma(options)
|
2017-01-23 20:49:46 +03:00
|
|
|
? ","
|
|
|
|
: ""
|
2017-01-23 02:20:47 +03:00
|
|
|
),
|
2017-02-23 20:57:51 +03:00
|
|
|
comments.printDanglingComments(
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
/* sameIndent */ true
|
|
|
|
),
|
2017-01-24 22:38:12 +03:00
|
|
|
softline,
|
2017-01-13 23:03:53 +03:00
|
|
|
"]"
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.typeAnnotation) parts.push(path.call(print, "typeAnnotation"));
|
2017-01-13 23:03:53 +03:00
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
case "SequenceExpression":
|
|
|
|
return join(", ", path.map(print, "expressions"));
|
|
|
|
case "ThisExpression":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "this";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "Super":
|
2017-01-28 18:50:22 +03:00
|
|
|
return "super"; // Babel 6 Literal split
|
|
|
|
case "NullLiteral":
|
|
|
|
return "null"; // Babel 6 Literal split
|
|
|
|
case "RegExpLiteral":
|
2017-02-02 20:54:10 +03:00
|
|
|
return n.extra.raw;
|
2017-01-13 23:03:53 +03:00
|
|
|
// Babel 6 Literal split
|
|
|
|
case "NumericLiteral":
|
2017-01-30 20:36:23 +03:00
|
|
|
return printNumber(n.extra.raw);
|
2017-01-17 07:01:51 +03:00
|
|
|
// Babel 6 Literal split
|
|
|
|
case "BooleanLiteral":
|
2017-01-13 23:03:53 +03:00
|
|
|
// Babel 6 Literal split
|
|
|
|
case "StringLiteral":
|
|
|
|
case "Literal":
|
2017-01-30 20:36:23 +03:00
|
|
|
if (typeof n.value === "number") return printNumber(n.raw);
|
2017-01-20 00:20:03 +03:00
|
|
|
if (typeof n.value !== "string") return "" + n.value;
|
2017-01-13 23:03:53 +03:00
|
|
|
|
2017-01-28 18:50:22 +03:00
|
|
|
return nodeStr(n, options); // Babel 6
|
|
|
|
case "Directive":
|
|
|
|
return path.call(print, "value"); // Babel 6
|
|
|
|
case "DirectiveLiteral":
|
2017-01-20 00:20:03 +03:00
|
|
|
return nodeStr(n, options);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ModuleSpecifier":
|
|
|
|
if (n.local) {
|
|
|
|
throw new Error("The ESTree ModuleSpecifier type should be abstract");
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Esprima ModuleSpecifier type is just a string-valued
|
|
|
|
// Literal identifying the imported-from module.
|
2017-01-20 00:20:03 +03:00
|
|
|
return nodeStr(n, options);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "UnaryExpression":
|
|
|
|
parts.push(n.operator);
|
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (/[a-z]$/.test(n.operator)) parts.push(" ");
|
2017-01-13 23:03:53 +03:00
|
|
|
|
|
|
|
parts.push(path.call(print, "argument"));
|
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
case "UpdateExpression":
|
|
|
|
parts.push(path.call(print, "argument"), n.operator);
|
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.prefix) parts.reverse();
|
2017-01-13 23:03:53 +03:00
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
case "ConditionalExpression":
|
|
|
|
return group(
|
2017-01-09 20:09:04 +03:00
|
|
|
concat([
|
2017-01-13 23:03:53 +03:00
|
|
|
path.call(print, "test"),
|
2017-01-09 20:09:04 +03:00
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([
|
2017-01-13 23:03:53 +03:00
|
|
|
line,
|
|
|
|
"? ",
|
2017-02-02 21:37:59 +03:00
|
|
|
indent(2, path.call(print, "consequent")),
|
2017-01-13 23:03:53 +03:00
|
|
|
line,
|
|
|
|
": ",
|
2017-02-02 21:37:59 +03:00
|
|
|
indent(2, path.call(print, "alternate"))
|
2017-01-09 20:09:04 +03:00
|
|
|
])
|
2017-01-13 23:03:53 +03:00
|
|
|
)
|
2017-01-09 20:09:04 +03:00
|
|
|
])
|
|
|
|
);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "NewExpression":
|
|
|
|
parts.push("new ", path.call(print, "callee"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
var args = n.arguments;
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (args) {
|
|
|
|
parts.push(printArgumentsList(path, options, print));
|
2017-01-11 08:48:49 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "VariableDeclaration":
|
|
|
|
var printed = path.map(
|
|
|
|
function(childPath) {
|
|
|
|
return print(childPath);
|
|
|
|
},
|
|
|
|
"declarations"
|
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts = [
|
|
|
|
n.kind,
|
|
|
|
" ",
|
2017-01-19 02:31:46 +03:00
|
|
|
printed[0],
|
2017-01-13 23:03:53 +03:00
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-01-28 18:50:22 +03:00
|
|
|
concat(printed.slice(1).map(p => concat([",", line, p])))
|
2017-01-13 23:03:53 +03:00
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
// We generally want to terminate all variable declarations with a
|
2017-01-22 23:34:14 +03:00
|
|
|
// semicolon, except when they in the () part of for loops.
|
2017-01-13 23:03:53 +03:00
|
|
|
var parentNode = path.getParentNode();
|
|
|
|
|
2017-01-22 23:34:14 +03:00
|
|
|
var isParentForLoop = namedTypes.ForStatement.check(parentNode) ||
|
2017-01-23 20:49:46 +03:00
|
|
|
namedTypes.ForInStatement.check(parentNode) ||
|
2017-02-23 20:57:51 +03:00
|
|
|
(namedTypes.ForOfStatement &&
|
|
|
|
namedTypes.ForOfStatement.check(parentNode)) ||
|
|
|
|
(namedTypes.ForAwaitStatement &&
|
|
|
|
namedTypes.ForAwaitStatement.check(parentNode));
|
2017-01-22 23:34:14 +03:00
|
|
|
|
|
|
|
if (!(isParentForLoop && parentNode.body !== n)) {
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(";");
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:54:01 +03:00
|
|
|
return group(concat(parts));
|
2017-01-13 23:03:53 +03:00
|
|
|
case "VariableDeclarator":
|
|
|
|
return n.init
|
2017-02-28 18:00:28 +03:00
|
|
|
? concat([
|
|
|
|
path.call(print, "id"),
|
|
|
|
" =",
|
2017-03-01 20:37:02 +03:00
|
|
|
hasLeadingOwnLineComment(options.originalText, n.init)
|
|
|
|
? indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([hardline, path.call(print, "init")])
|
|
|
|
)
|
|
|
|
: concat([" ", path.call(print, "init")])
|
2017-02-28 18:00:28 +03:00
|
|
|
])
|
2017-01-13 23:03:53 +03:00
|
|
|
: path.call(print, "id");
|
|
|
|
case "WithStatement":
|
|
|
|
return concat([
|
|
|
|
"with (",
|
|
|
|
path.call(print, "object"),
|
2017-01-16 20:54:39 +03:00
|
|
|
")",
|
|
|
|
adjustClause(path.call(print, "body"), options)
|
2017-01-13 23:03:53 +03:00
|
|
|
]);
|
|
|
|
case "IfStatement":
|
|
|
|
const con = adjustClause(path.call(print, "consequent"), options);
|
|
|
|
|
|
|
|
parts = [
|
|
|
|
"if (",
|
|
|
|
group(
|
2017-01-09 20:09:04 +03:00
|
|
|
concat([
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-01-28 18:50:22 +03:00
|
|
|
concat([softline, path.call(print, "test")])
|
2017-01-09 20:09:04 +03:00
|
|
|
),
|
2017-01-13 23:03:53 +03:00
|
|
|
softline
|
2017-01-09 20:09:04 +03:00
|
|
|
])
|
2017-01-13 23:03:53 +03:00
|
|
|
),
|
|
|
|
")",
|
|
|
|
con
|
|
|
|
];
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.alternate) {
|
|
|
|
const hasBraces = isCurlyBracket(con);
|
|
|
|
const isEmpty = isEmptyBlock(con);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (hasBraces && !isEmpty) {
|
|
|
|
parts.push(" else");
|
|
|
|
} else {
|
2017-01-24 02:47:11 +03:00
|
|
|
// We use `conditionalGroup` to suppress break propagation.
|
|
|
|
// This allows us to provide a hardline without forcing the
|
|
|
|
// entire `if` clause to break up.
|
2017-01-28 18:50:22 +03:00
|
|
|
parts.push(conditionalGroup([concat([hardline, "else"])]));
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(
|
|
|
|
adjustClause(
|
|
|
|
path.call(print, "alternate"),
|
|
|
|
options,
|
|
|
|
n.alternate.type === "IfStatement"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return group(concat(parts));
|
|
|
|
case "ForStatement": {
|
|
|
|
const body = adjustClause(path.call(print, "body"), options);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-20 18:15:46 +03:00
|
|
|
// We want to keep dangling comments above the loop to stay consistent.
|
|
|
|
// Any comment positioned between the for statement and the parentheses
|
|
|
|
// is going to be printed before the statement.
|
2017-02-23 20:57:51 +03:00
|
|
|
const dangling = comments.printDanglingComments(
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
/* sameLine */ true
|
|
|
|
);
|
2017-02-20 17:17:10 +03:00
|
|
|
const printedComments = dangling ? concat([dangling, softline]) : "";
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (!n.init && !n.test && !n.update) {
|
2017-02-20 17:17:10 +03:00
|
|
|
return concat([printedComments, "for (;;)", body]);
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat([
|
2017-02-20 17:17:10 +03:00
|
|
|
printedComments,
|
2017-01-13 23:03:53 +03:00
|
|
|
"for (",
|
|
|
|
group(
|
2017-01-09 20:09:04 +03:00
|
|
|
concat([
|
2017-01-13 23:03:53 +03:00
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([
|
|
|
|
softline,
|
|
|
|
path.call(print, "init"),
|
|
|
|
";",
|
|
|
|
line,
|
|
|
|
path.call(print, "test"),
|
|
|
|
";",
|
|
|
|
line,
|
|
|
|
path.call(print, "update")
|
|
|
|
])
|
|
|
|
),
|
|
|
|
softline
|
2017-01-09 20:09:04 +03:00
|
|
|
])
|
2017-01-13 23:03:53 +03:00
|
|
|
),
|
|
|
|
")",
|
|
|
|
body
|
|
|
|
]);
|
2016-12-31 07:03:22 +03:00
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "WhileStatement":
|
|
|
|
return concat([
|
|
|
|
"while (",
|
2017-01-20 20:42:16 +03:00
|
|
|
group(
|
|
|
|
concat([
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-01-28 18:50:22 +03:00
|
|
|
concat([softline, path.call(print, "test")])
|
2017-01-20 20:42:16 +03:00
|
|
|
),
|
|
|
|
softline
|
|
|
|
])
|
|
|
|
),
|
2017-01-13 23:03:53 +03:00
|
|
|
")",
|
|
|
|
adjustClause(path.call(print, "body"), options)
|
|
|
|
]);
|
|
|
|
case "ForInStatement":
|
|
|
|
// Note: esprima can't actually parse "for each (".
|
|
|
|
return concat([
|
|
|
|
n.each ? "for each (" : "for (",
|
|
|
|
path.call(print, "left"),
|
|
|
|
" in ",
|
|
|
|
path.call(print, "right"),
|
|
|
|
")",
|
|
|
|
adjustClause(path.call(print, "body"), options)
|
|
|
|
]);
|
|
|
|
case "ForOfStatement":
|
|
|
|
return concat([
|
|
|
|
"for (",
|
|
|
|
path.call(print, "left"),
|
|
|
|
" of ",
|
|
|
|
path.call(print, "right"),
|
|
|
|
")",
|
|
|
|
adjustClause(path.call(print, "body"), options)
|
|
|
|
]);
|
|
|
|
case "ForAwaitStatement":
|
|
|
|
return concat([
|
|
|
|
"for await (",
|
|
|
|
path.call(print, "left"),
|
|
|
|
" of ",
|
|
|
|
path.call(print, "right"),
|
|
|
|
")",
|
|
|
|
adjustClause(path.call(print, "body"), options)
|
|
|
|
]);
|
|
|
|
case "DoWhileStatement":
|
|
|
|
var clause = adjustClause(path.call(print, "body"), options);
|
2017-01-28 18:50:22 +03:00
|
|
|
var doBody = concat(["do", clause]);
|
|
|
|
var parts = [doBody];
|
2017-01-13 23:03:53 +03:00
|
|
|
const hasBraces = isCurlyBracket(clause);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (hasBraces) parts.push(" while");
|
2017-01-28 18:50:22 +03:00
|
|
|
else parts.push(concat([line, "while"]));
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(" (", path.call(print, "test"), ");");
|
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
case "DoExpression":
|
2017-02-27 18:49:08 +03:00
|
|
|
return concat(["do ", path.call(print, "body")]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "BreakStatement":
|
|
|
|
parts.push("break");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.label) parts.push(" ", path.call(print, "label"));
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-31 07:03:22 +03:00
|
|
|
parts.push(";");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ContinueStatement":
|
|
|
|
parts.push("continue");
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.label) parts.push(" ", path.call(print, "label"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(";");
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "LabeledStatement":
|
2017-01-22 23:34:27 +03:00
|
|
|
if (n.body.type === "EmptyStatement") {
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat([path.call(print, "label"), ":;"]);
|
2017-01-22 23:34:27 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat([
|
|
|
|
path.call(print, "label"),
|
2017-03-04 00:46:40 +03:00
|
|
|
": ",
|
2017-01-13 23:03:53 +03:00
|
|
|
path.call(print, "body")
|
|
|
|
]);
|
|
|
|
case "TryStatement":
|
|
|
|
parts.push("try ", path.call(print, "block"));
|
|
|
|
|
|
|
|
if (n.handler) {
|
|
|
|
parts.push(" ", path.call(print, "handler"));
|
|
|
|
} else if (n.handlers) {
|
|
|
|
path.each(
|
|
|
|
function(handlerPath) {
|
|
|
|
parts.push(" ", print(handlerPath));
|
|
|
|
},
|
|
|
|
"handlers"
|
|
|
|
);
|
2017-01-05 20:10:14 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.finalizer) {
|
|
|
|
parts.push(" finally ", path.call(print, "finalizer"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
case "CatchClause":
|
|
|
|
parts.push("catch (", path.call(print, "param"));
|
|
|
|
|
|
|
|
if (n.guard)
|
|
|
|
// Note: esprima does not recognize conditional catch clauses.
|
|
|
|
parts.push(" if ", path.call(print, "guard"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(") ", path.call(print, "body"));
|
2017-01-13 21:00:32 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ThrowStatement":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["throw ", path.call(print, "argument"), ";"]);
|
2017-01-13 23:03:53 +03:00
|
|
|
// Note: ignoring n.lexical because it has no printing consequences.
|
|
|
|
case "SwitchStatement":
|
2017-01-13 21:00:32 +03:00
|
|
|
return concat([
|
2017-01-13 23:03:53 +03:00
|
|
|
"switch (",
|
|
|
|
path.call(print, "discriminant"),
|
|
|
|
") {",
|
2017-01-23 20:49:46 +03:00
|
|
|
n.cases.length > 0
|
|
|
|
? indent(
|
2017-01-28 18:50:22 +03:00
|
|
|
options.tabWidth,
|
|
|
|
concat([hardline, join(hardline, path.map(print, "cases"))])
|
|
|
|
)
|
2017-01-23 20:49:46 +03:00
|
|
|
: "",
|
2017-01-13 23:03:53 +03:00
|
|
|
hardline,
|
|
|
|
"}"
|
2017-01-13 21:00:32 +03:00
|
|
|
]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "SwitchCase":
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.test) parts.push("case ", path.call(print, "test"), ":");
|
|
|
|
else parts.push("default:");
|
2017-01-13 23:03:53 +03:00
|
|
|
|
2017-01-22 23:33:21 +03:00
|
|
|
if (n.consequent.find(node => node.type !== "EmptyStatement")) {
|
2017-01-13 23:03:53 +03:00
|
|
|
const cons = path.call(
|
|
|
|
function(consequentPath) {
|
|
|
|
return printStatementSequence(consequentPath, options, print);
|
|
|
|
},
|
|
|
|
"consequent"
|
|
|
|
);
|
|
|
|
|
|
|
|
parts.push(
|
|
|
|
isCurlyBracket(cons)
|
2017-01-28 18:50:22 +03:00
|
|
|
? concat([" ", cons])
|
|
|
|
: indent(options.tabWidth, concat([hardline, cons]))
|
2017-01-13 23:03:53 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
// JSX extensions below.
|
|
|
|
case "DebuggerStatement":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "debugger;";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "JSXAttribute":
|
|
|
|
parts.push(path.call(print, "name"));
|
|
|
|
|
|
|
|
if (n.value) {
|
|
|
|
let res;
|
|
|
|
if (
|
|
|
|
(n.value.type === "StringLiteral" || n.value.type === "Literal") &&
|
2017-02-16 06:56:11 +03:00
|
|
|
typeof n.value.value === "string"
|
2017-01-13 23:03:53 +03:00
|
|
|
) {
|
|
|
|
res = '"' + util.htmlEscapeInsideDoubleQuote(n.value.value) + '"';
|
|
|
|
} else {
|
|
|
|
res = path.call(print, "value");
|
|
|
|
}
|
|
|
|
parts.push("=", res);
|
|
|
|
}
|
2017-01-13 21:00:32 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "JSXIdentifier":
|
2017-01-20 00:20:03 +03:00
|
|
|
return "" + n.name;
|
2017-01-13 23:03:53 +03:00
|
|
|
case "JSXNamespacedName":
|
|
|
|
return join(":", [
|
|
|
|
path.call(print, "namespace"),
|
|
|
|
path.call(print, "name")
|
|
|
|
]);
|
|
|
|
case "JSXMemberExpression":
|
|
|
|
return join(".", [
|
|
|
|
path.call(print, "object"),
|
|
|
|
path.call(print, "property")
|
|
|
|
]);
|
|
|
|
case "JSXSpreadAttribute":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["{...", path.call(print, "argument"), "}"]);
|
2017-01-26 22:51:08 +03:00
|
|
|
case "JSXExpressionContainer": {
|
|
|
|
const parent = path.getParentNode(0);
|
|
|
|
|
|
|
|
const shouldInline = n.expression.type === "ArrayExpression" ||
|
|
|
|
n.expression.type === "ObjectExpression" ||
|
|
|
|
n.expression.type === "ArrowFunctionExpression" ||
|
|
|
|
n.expression.type === "CallExpression" ||
|
|
|
|
n.expression.type === "FunctionExpression" ||
|
2017-02-05 05:37:18 +03:00
|
|
|
n.expression.type === "JSXEmptyExpression" ||
|
2017-02-23 20:57:51 +03:00
|
|
|
(parent.type === "JSXElement" &&
|
2017-01-28 18:50:22 +03:00
|
|
|
(n.expression.type === "ConditionalExpression" ||
|
2017-03-09 18:59:53 +03:00
|
|
|
isBinaryish(n.expression)));
|
2017-01-26 22:51:08 +03:00
|
|
|
|
|
|
|
if (shouldInline) {
|
2017-02-23 20:57:51 +03:00
|
|
|
return group(
|
|
|
|
concat(["{", path.call(print, "expression"), lineSuffixBoundary, "}"])
|
|
|
|
);
|
2017-01-19 20:37:02 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 02:47:11 +03:00
|
|
|
return group(
|
2017-01-13 23:03:53 +03:00
|
|
|
concat([
|
|
|
|
"{",
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-01-28 18:50:22 +03:00
|
|
|
concat([softline, path.call(print, "expression")])
|
2017-01-13 23:03:53 +03:00
|
|
|
),
|
|
|
|
softline,
|
2017-02-23 20:26:26 +03:00
|
|
|
lineSuffixBoundary,
|
2017-01-13 23:03:53 +03:00
|
|
|
"}"
|
|
|
|
])
|
|
|
|
);
|
2017-01-26 22:51:08 +03:00
|
|
|
}
|
2017-01-19 00:25:20 +03:00
|
|
|
case "JSXElement": {
|
|
|
|
const elem = printJSXElement(path, options, print);
|
|
|
|
return maybeWrapJSXElementInParens(path, elem, options);
|
|
|
|
}
|
2017-01-27 01:22:56 +03:00
|
|
|
case "JSXOpeningElement": {
|
|
|
|
const n = path.getValue();
|
|
|
|
|
|
|
|
// don't break up opening elements with a single long text attribute
|
2017-01-28 18:50:22 +03:00
|
|
|
if (
|
|
|
|
n.attributes.length === 1 &&
|
2017-02-16 06:56:11 +03:00
|
|
|
n.attributes[0].value &&
|
|
|
|
n.attributes[0].value.type === "Literal" &&
|
|
|
|
typeof n.attributes[0].value.value === "string"
|
2017-01-27 01:22:56 +03:00
|
|
|
) {
|
2017-01-28 18:50:22 +03:00
|
|
|
return group(
|
|
|
|
concat([
|
|
|
|
"<",
|
|
|
|
path.call(print, "name"),
|
|
|
|
" ",
|
|
|
|
concat(path.map(print, "attributes")),
|
|
|
|
n.selfClosing ? " />" : ">"
|
|
|
|
])
|
|
|
|
);
|
2017-01-27 01:22:56 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return group(
|
2017-01-09 20:09:04 +03:00
|
|
|
concat([
|
2017-01-13 23:03:53 +03:00
|
|
|
"<",
|
|
|
|
path.call(print, "name"),
|
2017-01-24 21:54:01 +03:00
|
|
|
concat([
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat(
|
2017-01-28 18:50:22 +03:00
|
|
|
path.map(attr => concat([line, print(attr)]), "attributes")
|
2017-01-24 21:54:01 +03:00
|
|
|
)
|
|
|
|
),
|
2017-02-16 06:56:11 +03:00
|
|
|
n.selfClosing ? line : options.jsxBracketSameLine ? ">" : softline
|
2017-01-24 21:54:01 +03:00
|
|
|
]),
|
2017-02-16 06:56:11 +03:00
|
|
|
n.selfClosing ? "/>" : options.jsxBracketSameLine ? "" : ">"
|
2017-01-09 20:09:04 +03:00
|
|
|
])
|
|
|
|
);
|
2017-01-27 01:22:56 +03:00
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "JSXClosingElement":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["</", path.call(print, "name"), ">"]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "JSXText":
|
|
|
|
throw new Error("JSXTest should be handled by JSXElement");
|
|
|
|
case "JSXEmptyExpression":
|
2017-03-11 05:23:11 +03:00
|
|
|
const requiresHardline = n.comments.some(
|
|
|
|
comment => comment.type === "Line" || comment.type === "CommentLine"
|
2017-03-04 02:39:37 +03:00
|
|
|
);
|
2017-03-11 05:23:11 +03:00
|
|
|
|
|
|
|
return concat([
|
|
|
|
comments.printDanglingComments(
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
/* sameIndent */ requiresHardline ? false : true
|
|
|
|
),
|
|
|
|
requiresHardline ? hardline : ""
|
|
|
|
]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "TypeAnnotatedIdentifier":
|
|
|
|
return concat([
|
|
|
|
path.call(print, "annotation"),
|
|
|
|
" ",
|
|
|
|
path.call(print, "identifier")
|
|
|
|
]);
|
|
|
|
case "ClassBody":
|
2017-02-04 00:56:06 +03:00
|
|
|
if (!n.comments && n.body.length === 0) {
|
|
|
|
return "{}";
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-03 19:50:51 +03:00
|
|
|
return concat([
|
|
|
|
"{",
|
2017-02-09 18:44:56 +03:00
|
|
|
n.body.length > 0
|
|
|
|
? indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([
|
|
|
|
hardline,
|
|
|
|
path.call(
|
|
|
|
function(bodyPath) {
|
|
|
|
return printStatementSequence(bodyPath, options, print);
|
|
|
|
},
|
|
|
|
"body"
|
|
|
|
)
|
|
|
|
])
|
2017-01-13 23:03:53 +03:00
|
|
|
)
|
2017-02-09 18:44:56 +03:00
|
|
|
: comments.printDanglingComments(path, options),
|
2017-02-03 19:50:51 +03:00
|
|
|
hardline,
|
|
|
|
"}"
|
|
|
|
]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ClassPropertyDefinition":
|
|
|
|
parts.push("static ", path.call(print, "definition"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (!namedTypes.MethodDefinition.check(n.definition)) parts.push(";");
|
2017-01-09 20:07:39 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ClassProperty":
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.static) parts.push("static ");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
var key;
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.computed) {
|
2017-01-28 18:50:22 +03:00
|
|
|
key = concat(["[", path.call(print, "key"), "]"]);
|
2017-01-11 22:37:53 +03:00
|
|
|
} else {
|
2017-01-19 20:37:24 +03:00
|
|
|
key = printPropertyKey(path, options, print);
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.variance === "plus") {
|
2017-01-28 18:50:22 +03:00
|
|
|
key = concat(["+", key]);
|
2017-01-13 23:03:53 +03:00
|
|
|
} else if (n.variance === "minus") {
|
2017-01-28 18:50:22 +03:00
|
|
|
key = concat(["-", key]);
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2017-01-11 22:37:53 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(key);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.typeAnnotation) parts.push(path.call(print, "typeAnnotation"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (n.value) parts.push(" = ", path.call(print, "value"));
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(";");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "ClassDeclaration":
|
|
|
|
case "ClassExpression":
|
2017-01-26 00:36:55 +03:00
|
|
|
return concat(printClass(path, options, print));
|
2017-01-13 23:03:53 +03:00
|
|
|
case "TemplateElement":
|
2017-02-02 20:54:10 +03:00
|
|
|
return join(literalline, n.value.raw.split("\n"));
|
2017-01-13 23:03:53 +03:00
|
|
|
case "TemplateLiteral":
|
|
|
|
var expressions = path.map(print, "expressions");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-23 20:00:29 +03:00
|
|
|
function removeLines(doc) {
|
|
|
|
// Force this doc into flat mode by statically converting all
|
|
|
|
// lines into spaces (or soft lines into nothing). Hard lines
|
|
|
|
// should still output because there's too great of a chance
|
|
|
|
// of breaking existing assumptions otherwise.
|
|
|
|
return docUtils.mapDoc(doc, d => {
|
|
|
|
if (d.type === "line" && !d.hard) {
|
|
|
|
return d.soft ? "" : " ";
|
2017-03-01 20:37:02 +03:00
|
|
|
} else if (d.type === "if-break") {
|
2017-02-24 08:21:13 +03:00
|
|
|
return d.flatContents || "";
|
|
|
|
}
|
2017-02-23 20:00:29 +03:00
|
|
|
return d;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push("`");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
path.each(
|
|
|
|
function(childPath) {
|
|
|
|
var i = childPath.getName();
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(print(childPath));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (i < expressions.length) {
|
2017-02-23 20:26:26 +03:00
|
|
|
parts.push(
|
|
|
|
"${",
|
2017-02-23 20:57:51 +03:00
|
|
|
removeLines(expressions[i]),
|
|
|
|
lineSuffixBoundary,
|
2017-02-23 20:26:26 +03:00
|
|
|
"}"
|
|
|
|
);
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"quasis"
|
|
|
|
);
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push("`");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
// These types are unprintable because they serve as abstract
|
|
|
|
// supertypes for other (printable) types.
|
|
|
|
case "TaggedTemplateExpression":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat([path.call(print, "tag"), path.call(print, "quasi")]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "Node":
|
|
|
|
case "Printable":
|
|
|
|
case "SourceLocation":
|
|
|
|
case "Position":
|
|
|
|
case "Statement":
|
|
|
|
case "Function":
|
|
|
|
case "Pattern":
|
|
|
|
case "Expression":
|
|
|
|
case "Declaration":
|
|
|
|
case "Specifier":
|
|
|
|
case "NamedSpecifier":
|
|
|
|
// Supertype of Block and Line.
|
|
|
|
case "Comment":
|
|
|
|
// Flow
|
2017-01-28 18:50:22 +03:00
|
|
|
case "MemberTypeAnnotation": // Flow
|
|
|
|
case "Type":
|
2017-01-13 23:03:53 +03:00
|
|
|
throw new Error("unprintable type: " + JSON.stringify(n.type));
|
2017-01-26 22:57:43 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
// Type Annotations for Facebook Flow, typically stripped out or
|
|
|
|
// transformed away before printing.
|
|
|
|
case "TypeAnnotation":
|
|
|
|
if (n.typeAnnotation) {
|
|
|
|
if (n.typeAnnotation.type !== "FunctionTypeAnnotation") {
|
|
|
|
parts.push(": ");
|
|
|
|
}
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(path.call(print, "typeAnnotation"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return "";
|
|
|
|
case "TupleTypeAnnotation":
|
2017-03-15 19:35:22 +03:00
|
|
|
return group(
|
|
|
|
concat([
|
|
|
|
"[",
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([
|
|
|
|
softline,
|
|
|
|
printArrayItems(path, options, "types", print)
|
|
|
|
])
|
|
|
|
),
|
|
|
|
ifBreak(shouldPrintComma(options) ? "," : ""),
|
|
|
|
comments.printDanglingComments(
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
/* sameIndent */ true
|
|
|
|
),
|
|
|
|
softline,
|
|
|
|
"]"
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ExistentialTypeParam":
|
|
|
|
case "ExistsTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "*";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "EmptyTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "empty";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "AnyTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "any";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "MixedTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "mixed";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ArrayTypeAnnotation":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat([path.call(print, "elementType"), "[]"]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "BooleanTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "boolean";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "NumericLiteralTypeAnnotation":
|
|
|
|
case "BooleanLiteralTypeAnnotation":
|
|
|
|
return "" + n.value;
|
|
|
|
case "DeclareClass":
|
2017-01-26 00:36:55 +03:00
|
|
|
return printFlowDeclaration(path, printClass(path, options, print));
|
2017-01-13 23:03:53 +03:00
|
|
|
case "DeclareFunction":
|
|
|
|
return printFlowDeclaration(path, [
|
|
|
|
"function ",
|
|
|
|
path.call(print, "id"),
|
|
|
|
n.predicate ? " " : "",
|
|
|
|
path.call(print, "predicate"),
|
|
|
|
";"
|
|
|
|
]);
|
|
|
|
case "DeclareModule":
|
|
|
|
return printFlowDeclaration(path, [
|
|
|
|
"module ",
|
|
|
|
path.call(print, "id"),
|
|
|
|
" ",
|
|
|
|
path.call(print, "body")
|
|
|
|
]);
|
|
|
|
case "DeclareModuleExports":
|
|
|
|
return printFlowDeclaration(path, [
|
|
|
|
"module.exports",
|
|
|
|
path.call(print, "typeAnnotation"),
|
|
|
|
";"
|
|
|
|
]);
|
|
|
|
case "DeclareVariable":
|
2017-01-28 18:50:22 +03:00
|
|
|
return printFlowDeclaration(path, ["var ", path.call(print, "id"), ";"]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "DeclareExportAllDeclaration":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["declare export * from ", path.call(print, "source")]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "DeclareExportDeclaration":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["declare ", printExportDeclaration(path, options, print)]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "FunctionTypeAnnotation":
|
|
|
|
// FunctionTypeAnnotation is ambiguous:
|
|
|
|
// declare function foo(a: B): void; OR
|
|
|
|
// var A: (a: B) => void;
|
|
|
|
var parent = path.getParentNode(0);
|
2017-02-23 20:57:51 +03:00
|
|
|
var isArrowFunctionTypeAnnotation = !((!parent.variance &&
|
2017-01-13 23:03:53 +03:00
|
|
|
!parent.optional &&
|
2017-02-23 20:57:51 +03:00
|
|
|
namedTypes.ObjectTypeProperty.check(parent)) ||
|
2017-01-13 23:03:53 +03:00
|
|
|
namedTypes.ObjectTypeCallProperty.check(parent) ||
|
|
|
|
namedTypes.DeclareFunction.check(path.getParentNode(2)));
|
2017-02-05 05:23:37 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
var needsColon = isArrowFunctionTypeAnnotation &&
|
|
|
|
namedTypes.TypeAnnotation.check(parent);
|
|
|
|
|
2017-02-05 05:23:37 +03:00
|
|
|
if (isObjectTypePropertyAFunction(parent)) {
|
|
|
|
isArrowFunctionTypeAnnotation = true;
|
|
|
|
needsColon = true;
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (needsColon) {
|
2016-12-31 07:03:22 +03:00
|
|
|
parts.push(": ");
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(path.call(print, "typeParameters"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-24 21:54:01 +03:00
|
|
|
parts.push(group(printFunctionParams(path, print, options)));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
// The returnType is not wrapped in a TypeAnnotation, so the colon
|
|
|
|
// needs to be added separately.
|
|
|
|
if (n.returnType || n.predicate) {
|
|
|
|
parts.push(
|
|
|
|
isArrowFunctionTypeAnnotation ? " => " : ": ",
|
|
|
|
path.call(print, "returnType"),
|
|
|
|
path.call(print, "predicate")
|
|
|
|
);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "FunctionTypeParam":
|
|
|
|
return concat([
|
|
|
|
path.call(print, "name"),
|
|
|
|
n.optional ? "?" : "",
|
|
|
|
n.name ? ": " : "",
|
|
|
|
path.call(print, "typeAnnotation")
|
|
|
|
]);
|
|
|
|
case "GenericTypeAnnotation":
|
|
|
|
return concat([
|
|
|
|
path.call(print, "id"),
|
|
|
|
path.call(print, "typeParameters")
|
|
|
|
]);
|
|
|
|
case "DeclareInterface":
|
2017-01-14 07:15:30 +03:00
|
|
|
case "InterfaceDeclaration": {
|
2017-02-13 20:17:20 +03:00
|
|
|
if (
|
|
|
|
n.type === "DeclareInterface" ||
|
2017-02-16 06:56:11 +03:00
|
|
|
isFlowNodeStartingWithDeclare(n, options)
|
2017-02-13 20:17:20 +03:00
|
|
|
) {
|
2017-01-14 07:15:30 +03:00
|
|
|
parts.push("declare ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
parts.push(
|
2017-01-20 00:20:03 +03:00
|
|
|
"interface ",
|
2017-01-13 23:03:53 +03:00
|
|
|
path.call(print, "id"),
|
|
|
|
path.call(print, "typeParameters"),
|
|
|
|
" "
|
2016-12-23 21:38:10 +03:00
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n["extends"].length > 0) {
|
2017-01-19 02:31:46 +03:00
|
|
|
parts.push("extends ", join(", ", path.map(print, "extends")), " ");
|
2017-01-13 23:03:53 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-14 07:15:30 +03:00
|
|
|
parts.push(path.call(print, "body"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
2017-01-14 07:15:30 +03:00
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ClassImplements":
|
|
|
|
case "InterfaceExtends":
|
|
|
|
return concat([
|
|
|
|
path.call(print, "id"),
|
|
|
|
path.call(print, "typeParameters")
|
|
|
|
]);
|
|
|
|
case "IntersectionTypeAnnotation":
|
|
|
|
case "UnionTypeAnnotation": {
|
2017-01-09 20:09:04 +03:00
|
|
|
const types = path.map(print, "types");
|
2017-02-23 20:57:51 +03:00
|
|
|
const isIntersection = n.type === "IntersectionTypeAnnotation";
|
2017-02-20 18:18:39 +03:00
|
|
|
const shouldInline = isIntersection &&
|
2017-02-23 20:57:51 +03:00
|
|
|
!(n.types.length > 1 && n.types[0].type === "ObjectTypeAnnotation");
|
2017-01-11 00:28:23 +03:00
|
|
|
|
2017-01-27 05:41:50 +03:00
|
|
|
// single-line variation
|
|
|
|
// A | B | C
|
|
|
|
|
|
|
|
// multi-line variation
|
|
|
|
// | A
|
|
|
|
// | B
|
|
|
|
// | C
|
2017-02-20 18:18:39 +03:00
|
|
|
|
|
|
|
// We want & operators to be inlined.
|
|
|
|
if (shouldInline) {
|
|
|
|
return join(" & ", types);
|
|
|
|
}
|
|
|
|
|
2017-03-04 00:54:45 +03:00
|
|
|
const parent = path.getParentNode();
|
|
|
|
// If there's a leading comment, the parent is doing the indentation
|
2017-03-04 02:39:37 +03:00
|
|
|
const shouldIndent = !(parent.type === "TypeAlias" &&
|
2017-03-04 00:54:45 +03:00
|
|
|
hasLeadingOwnLineComment(options.originalText, n));
|
|
|
|
|
|
|
|
const token = isIntersection ? "&" : "|";
|
|
|
|
|
2017-01-27 05:41:50 +03:00
|
|
|
return group(
|
|
|
|
indent(
|
2017-03-04 00:54:45 +03:00
|
|
|
shouldIndent ? options.tabWidth : 0,
|
2017-01-27 05:41:50 +03:00
|
|
|
concat([
|
2017-03-04 00:54:45 +03:00
|
|
|
ifBreak(concat([shouldIndent ? line : "", token, " "])),
|
|
|
|
join(concat([line, token, " "]), types)
|
2017-01-27 05:41:50 +03:00
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
2016-12-31 07:03:22 +03:00
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "NullableTypeAnnotation":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["?", path.call(print, "typeAnnotation")]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "NullLiteralTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "null";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ThisTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "this";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "NumberTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "number";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "ObjectTypeCallProperty":
|
|
|
|
if (n.static) {
|
|
|
|
parts.push("static ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(path.call(print, "value"));
|
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
case "ObjectTypeIndexer":
|
|
|
|
var variance = n.variance === "plus"
|
|
|
|
? "+"
|
|
|
|
: n.variance === "minus" ? "-" : "";
|
|
|
|
return concat([
|
|
|
|
variance,
|
|
|
|
"[",
|
|
|
|
path.call(print, "id"),
|
|
|
|
n.id ? ": " : "",
|
|
|
|
path.call(print, "key"),
|
|
|
|
"]: ",
|
|
|
|
path.call(print, "value")
|
|
|
|
]);
|
|
|
|
case "ObjectTypeProperty":
|
|
|
|
var variance = n.variance === "plus"
|
|
|
|
? "+"
|
|
|
|
: n.variance === "minus" ? "-" : "";
|
|
|
|
// TODO: This is a bad hack and we need a better way to know
|
|
|
|
// when to emit an arrow function or not.
|
2017-01-19 02:31:46 +03:00
|
|
|
var isFunction = !n.variance &&
|
|
|
|
!n.optional &&
|
2017-01-13 23:03:53 +03:00
|
|
|
n.value.type === "FunctionTypeAnnotation";
|
2017-02-05 05:23:37 +03:00
|
|
|
|
|
|
|
if (isObjectTypePropertyAFunction(n)) {
|
|
|
|
isFunction = true;
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat([
|
|
|
|
n.static ? "static " : "",
|
|
|
|
variance,
|
|
|
|
path.call(print, "key"),
|
|
|
|
n.optional ? "?" : "",
|
|
|
|
isFunction ? "" : ": ",
|
|
|
|
path.call(print, "value")
|
|
|
|
]);
|
|
|
|
case "QualifiedTypeIdentifier":
|
|
|
|
return concat([
|
|
|
|
path.call(print, "qualification"),
|
|
|
|
".",
|
|
|
|
path.call(print, "id")
|
|
|
|
]);
|
|
|
|
case "StringLiteralTypeAnnotation":
|
2017-01-20 00:20:03 +03:00
|
|
|
return nodeStr(n, options);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "NumberLiteralTypeAnnotation":
|
|
|
|
assert.strictEqual(typeof n.value, "number");
|
|
|
|
|
2017-01-20 00:20:03 +03:00
|
|
|
return "" + n.value;
|
2017-01-13 23:03:53 +03:00
|
|
|
case "StringTypeAnnotation":
|
2017-01-20 21:12:37 +03:00
|
|
|
return "string";
|
2017-01-13 23:03:53 +03:00
|
|
|
case "DeclareTypeAlias":
|
|
|
|
case "TypeAlias": {
|
2017-02-13 20:17:20 +03:00
|
|
|
if (
|
|
|
|
n.type === "DeclareTypeAlias" ||
|
2017-02-16 06:56:11 +03:00
|
|
|
isFlowNodeStartingWithDeclare(n, options)
|
2017-02-13 20:17:20 +03:00
|
|
|
) {
|
2016-12-30 08:01:44 +03:00
|
|
|
parts.push("declare ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-30 08:01:44 +03:00
|
|
|
parts.push(
|
2016-12-23 21:38:10 +03:00
|
|
|
"type ",
|
|
|
|
path.call(print, "id"),
|
|
|
|
path.call(print, "typeParameters"),
|
2017-03-04 00:54:45 +03:00
|
|
|
" =",
|
|
|
|
hasLeadingOwnLineComment(options.originalText, n.right)
|
|
|
|
? indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([hardline, path.call(print, "right")])
|
|
|
|
)
|
|
|
|
: concat([" ", path.call(print, "right")]),
|
2016-12-23 21:38:10 +03:00
|
|
|
";"
|
2016-12-30 08:01:44 +03:00
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-30 08:01:44 +03:00
|
|
|
return concat(parts);
|
|
|
|
}
|
2017-01-13 23:03:53 +03:00
|
|
|
case "TypeCastExpression":
|
|
|
|
return concat([
|
|
|
|
"(",
|
|
|
|
path.call(print, "expression"),
|
|
|
|
path.call(print, "typeAnnotation"),
|
|
|
|
")"
|
|
|
|
]);
|
|
|
|
case "TypeParameterDeclaration":
|
|
|
|
case "TypeParameterInstantiation":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["<", join(", ", path.map(print, "params")), ">"]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "TypeParameter":
|
|
|
|
switch (n.variance) {
|
|
|
|
case "plus":
|
|
|
|
parts.push("+");
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "minus":
|
|
|
|
parts.push("-");
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(path.call(print, "name"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n.bound) {
|
|
|
|
parts.push(path.call(print, "bound"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (n["default"]) {
|
|
|
|
parts.push("=", path.call(print, "default"));
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
return concat(parts);
|
|
|
|
case "TypeofTypeAnnotation":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["typeof ", path.call(print, "argument")]);
|
2017-01-13 23:03:53 +03:00
|
|
|
case "VoidTypeAnnotation":
|
|
|
|
return "void";
|
|
|
|
case "NullTypeAnnotation":
|
|
|
|
return "null";
|
|
|
|
case "InferredPredicate":
|
|
|
|
return "%checks";
|
|
|
|
// Unhandled types below. If encountered, nodes of these types should
|
|
|
|
// be either left alone or desugared into AST types that are fully
|
|
|
|
// supported by the pretty-printer.
|
|
|
|
case "DeclaredPredicate":
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat(["%checks(", path.call(print, "value"), ")"]);
|
2017-01-13 23:03:53 +03:00
|
|
|
// TODO
|
|
|
|
case "ClassHeritage":
|
|
|
|
// TODO
|
|
|
|
case "ComprehensionBlock":
|
|
|
|
// TODO
|
|
|
|
case "ComprehensionExpression":
|
|
|
|
// TODO
|
|
|
|
case "Glob":
|
|
|
|
// TODO
|
|
|
|
case "GeneratorExpression":
|
|
|
|
// TODO
|
|
|
|
case "LetStatement":
|
|
|
|
// TODO
|
|
|
|
case "LetExpression":
|
|
|
|
// TODO
|
|
|
|
case "GraphExpression":
|
|
|
|
// TODO
|
|
|
|
// XML types that nobody cares about or needs to print.
|
|
|
|
case "GraphIndexExpression":
|
|
|
|
case "XMLDefaultDeclaration":
|
|
|
|
case "XMLAnyName":
|
|
|
|
case "XMLQualifiedIdentifier":
|
|
|
|
case "XMLFunctionQualifiedIdentifier":
|
|
|
|
case "XMLAttributeSelector":
|
|
|
|
case "XMLFilterExpression":
|
|
|
|
case "XML":
|
|
|
|
case "XMLElement":
|
|
|
|
case "XMLList":
|
|
|
|
case "XMLEscape":
|
|
|
|
case "XMLText":
|
|
|
|
case "XMLStartTag":
|
|
|
|
case "XMLEndTag":
|
|
|
|
case "XMLPointTag":
|
|
|
|
case "XMLName":
|
|
|
|
case "XMLAttribute":
|
|
|
|
case "XMLCdata":
|
|
|
|
case "XMLComment":
|
|
|
|
case "XMLProcessingInstruction":
|
|
|
|
default:
|
|
|
|
debugger;
|
|
|
|
throw new Error("unknown type: " + JSON.stringify(n.type));
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
function printStatementSequence(path, options, print) {
|
2016-12-31 07:10:22 +03:00
|
|
|
let printed = [];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-05 06:27:25 +03:00
|
|
|
path.map(function(stmtPath, i) {
|
|
|
|
var stmt = stmtPath.getValue();
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-05 06:27:25 +03:00
|
|
|
// Just in case the AST has been modified to contain falsy
|
|
|
|
// "statements," it's safer simply to skip them.
|
|
|
|
if (!stmt) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-05 06:27:25 +03:00
|
|
|
// Skip printing EmptyStatement nodes to avoid leaving stray
|
|
|
|
// semicolons lying around.
|
|
|
|
if (stmt.type === "EmptyStatement") {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-05 06:27:25 +03:00
|
|
|
const stmtPrinted = print(stmtPath);
|
2017-01-10 05:49:06 +03:00
|
|
|
const text = options.originalText;
|
2017-01-05 06:27:25 +03:00
|
|
|
const parts = [];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-05 06:27:25 +03:00
|
|
|
parts.push(stmtPrinted);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-30 20:10:54 +03:00
|
|
|
if (util.isNextLineEmpty(text, stmt) && !isLastStatement(stmtPath)) {
|
2017-01-05 06:27:25 +03:00
|
|
|
parts.push(hardline);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-05 06:27:25 +03:00
|
|
|
printed.push(concat(parts));
|
|
|
|
});
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-31 07:01:07 +03:00
|
|
|
return join(hardline, printed);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-19 20:37:24 +03:00
|
|
|
function printPropertyKey(path, options, print) {
|
2017-01-23 05:41:31 +03:00
|
|
|
const node = path.getNode();
|
|
|
|
const key = node.key;
|
|
|
|
|
2017-01-15 07:40:26 +03:00
|
|
|
if (
|
2017-01-23 05:41:31 +03:00
|
|
|
(key.type === "StringLiteral" ||
|
2017-02-23 20:57:51 +03:00
|
|
|
(key.type === "Literal" && typeof key.value === "string")) &&
|
2017-02-16 06:56:11 +03:00
|
|
|
isIdentifierName(key.value) &&
|
|
|
|
!node.computed &&
|
|
|
|
// There's a bug in the flow parser where it throws if there are
|
|
|
|
// unquoted unicode literals as keys. Let's quote them for now.
|
|
|
|
(options.parser !== "flow" || key.value.match(/[a-zA-Z0-9$_]/))
|
2017-01-15 07:40:26 +03:00
|
|
|
) {
|
2017-01-11 20:31:34 +03:00
|
|
|
// 'a' -> a
|
2017-01-23 05:41:31 +03:00
|
|
|
return key.value;
|
2017-01-11 08:48:49 +03:00
|
|
|
}
|
|
|
|
return path.call(print, "key");
|
|
|
|
}
|
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
function printMethod(path, options, print) {
|
|
|
|
var node = path.getNode();
|
|
|
|
var kind = node.kind;
|
|
|
|
var parts = [];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (node.type === "ObjectMethod" || node.type === "ClassMethod") {
|
|
|
|
node.value = node;
|
|
|
|
} else {
|
|
|
|
namedTypes.FunctionExpression.assert(node.value);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (node.value.async) {
|
|
|
|
parts.push("async ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (!kind || kind === "init" || kind === "method" || kind === "constructor") {
|
|
|
|
if (node.value.generator) {
|
|
|
|
parts.push("*");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.ok(kind === "get" || kind === "set");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
parts.push(kind, " ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 20:37:24 +03:00
|
|
|
var key = printPropertyKey(path, options, print);
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (node.computed) {
|
2017-01-28 18:50:22 +03:00
|
|
|
key = concat(["[", key, "]"]);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-03 19:50:51 +03:00
|
|
|
parts.push(
|
|
|
|
key,
|
|
|
|
path.call(print, "value", "typeParameters"),
|
|
|
|
group(
|
|
|
|
concat([
|
2017-01-13 23:03:53 +03:00
|
|
|
path.call(
|
|
|
|
function(valuePath) {
|
|
|
|
return printFunctionParams(valuePath, print, options);
|
|
|
|
},
|
|
|
|
"value"
|
|
|
|
),
|
|
|
|
path.call(p => printReturnType(p, print), "value")
|
2017-02-03 19:50:51 +03:00
|
|
|
])
|
|
|
|
),
|
|
|
|
" ",
|
|
|
|
path.call(print, "value", "body")
|
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-09 21:47:02 +03:00
|
|
|
return concat(parts);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 06:29:53 +03:00
|
|
|
function shouldGroupLastArg(args) {
|
2017-02-25 20:56:13 +03:00
|
|
|
const lastArg = util.getLast(args);
|
2017-03-04 02:39:37 +03:00
|
|
|
const penultimateArg = util.getPenultimate(args);
|
2017-03-03 06:29:53 +03:00
|
|
|
return (!lastArg.comments || !lastArg.comments.length) &&
|
2017-01-31 17:52:53 +03:00
|
|
|
(lastArg.type === "ObjectExpression" ||
|
|
|
|
lastArg.type === "ArrayExpression" ||
|
|
|
|
lastArg.type === "FunctionExpression" ||
|
2017-02-23 20:57:51 +03:00
|
|
|
(lastArg.type === "ArrowFunctionExpression" &&
|
2017-01-31 17:52:53 +03:00
|
|
|
(lastArg.body.type === "BlockStatement" ||
|
|
|
|
lastArg.body.type === "ArrowFunctionExpression" ||
|
|
|
|
lastArg.body.type === "ObjectExpression" ||
|
|
|
|
lastArg.body.type === "ArrayExpression" ||
|
|
|
|
lastArg.body.type === "CallExpression" ||
|
2017-03-01 20:37:02 +03:00
|
|
|
lastArg.body.type === "JSXElement"))) &&
|
|
|
|
// If the last two arguments are of the same type,
|
|
|
|
// disable last element expansion.
|
|
|
|
(!penultimateArg || penultimateArg.type !== lastArg.type);
|
2017-03-03 06:29:53 +03:00
|
|
|
}
|
2017-01-04 23:24:10 +03:00
|
|
|
|
2017-03-03 06:29:53 +03:00
|
|
|
function printArgumentsList(path, options, print) {
|
|
|
|
var printed = path.map(print, "arguments");
|
|
|
|
|
|
|
|
if (printed.length === 0) {
|
2017-03-09 18:55:18 +03:00
|
|
|
return concat([
|
|
|
|
"(",
|
|
|
|
comments.printDanglingComments(path, options, /* sameIndent */ true),
|
|
|
|
")"
|
|
|
|
]);
|
2017-03-03 06:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const args = path.getValue().arguments;
|
|
|
|
// This is just an optimization; I think we could return the
|
|
|
|
// conditional group for all function calls, but it's more expensive
|
|
|
|
// so only do it for specific forms.
|
|
|
|
if (shouldGroupLastArg(args)) {
|
2017-01-24 02:47:11 +03:00
|
|
|
const shouldBreak = printed.slice(0, -1).some(willBreak);
|
2017-02-04 00:15:21 +03:00
|
|
|
return concat([
|
|
|
|
printed.some(willBreak) ? breakParent : "",
|
|
|
|
conditionalGroup(
|
|
|
|
[
|
|
|
|
concat(["(", join(concat([", "]), printed), ")"]),
|
2017-01-04 23:24:10 +03:00
|
|
|
concat([
|
|
|
|
"(",
|
2017-02-04 00:15:21 +03:00
|
|
|
join(concat([",", line]), printed.slice(0, -1)),
|
|
|
|
printed.length > 1 ? ", " : "",
|
|
|
|
group(util.getLast(printed), { shouldBreak: true }),
|
2017-01-04 23:24:10 +03:00
|
|
|
")"
|
|
|
|
]),
|
2017-02-04 00:15:21 +03:00
|
|
|
group(
|
|
|
|
concat([
|
|
|
|
"(",
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([line, join(concat([",", line]), printed)])
|
|
|
|
),
|
2017-02-18 06:44:55 +03:00
|
|
|
shouldPrintComma(options, "all") ? "," : "",
|
2017-02-04 00:15:21 +03:00
|
|
|
line,
|
|
|
|
")"
|
|
|
|
]),
|
|
|
|
{ shouldBreak: true }
|
|
|
|
)
|
|
|
|
],
|
|
|
|
{ shouldBreak }
|
|
|
|
)
|
|
|
|
]);
|
2017-01-04 23:24:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:54:01 +03:00
|
|
|
return group(
|
2017-01-04 23:24:10 +03:00
|
|
|
concat([
|
|
|
|
"(",
|
2017-01-09 20:09:04 +03:00
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-01-28 18:50:22 +03:00
|
|
|
concat([softline, join(concat([",", line]), printed)])
|
2017-01-09 20:09:04 +03:00
|
|
|
),
|
2017-02-18 06:44:55 +03:00
|
|
|
ifBreak(shouldPrintComma(options, "all") ? "," : ""),
|
2017-01-04 23:24:10 +03:00
|
|
|
softline,
|
|
|
|
")"
|
2017-01-31 19:45:47 +03:00
|
|
|
]),
|
|
|
|
{ shouldBreak: printed.some(willBreak) }
|
2017-01-04 23:24:10 +03:00
|
|
|
);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-05 00:52:49 +03:00
|
|
|
function printFunctionParams(path, print, options) {
|
2016-12-23 21:38:10 +03:00
|
|
|
var fun = path.getValue();
|
2016-12-30 08:01:44 +03:00
|
|
|
// namedTypes.Function.assert(fun);
|
2016-12-23 21:38:10 +03:00
|
|
|
var printed = path.map(print, "params");
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (fun.defaults) {
|
2016-12-31 07:03:22 +03:00
|
|
|
path.each(
|
|
|
|
function(defExprPath) {
|
|
|
|
var i = defExprPath.getName();
|
|
|
|
var p = printed[i];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-31 07:03:22 +03:00
|
|
|
if (p && defExprPath.getValue()) {
|
2017-01-28 18:50:22 +03:00
|
|
|
printed[i] = concat([p, " = ", print(defExprPath)]);
|
2016-12-31 07:03:22 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"defaults"
|
|
|
|
);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (fun.rest) {
|
2017-01-28 18:50:22 +03:00
|
|
|
printed.push(concat(["...", path.call(print, "rest")]));
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-15 07:55:38 +03:00
|
|
|
if (printed.length === 0) {
|
2017-02-24 18:26:30 +03:00
|
|
|
return concat([
|
|
|
|
"(",
|
|
|
|
comments.printDanglingComments(path, options, /* sameIndent */ true),
|
|
|
|
")"
|
2017-03-01 20:37:02 +03:00
|
|
|
]);
|
2017-01-15 07:55:38 +03:00
|
|
|
}
|
|
|
|
|
2017-01-18 20:42:20 +03:00
|
|
|
const lastParam = util.getLast(path.getValue().params);
|
2017-01-20 21:12:37 +03:00
|
|
|
const canHaveTrailingComma = !(lastParam &&
|
2017-03-04 02:39:37 +03:00
|
|
|
lastParam.type === "RestElement") && !fun.rest;
|
2017-01-18 20:42:20 +03:00
|
|
|
|
2017-03-03 06:29:53 +03:00
|
|
|
// If the parent is a call with the last argument expansion and this is the
|
|
|
|
// params of the last argument, we dont want the arguments to break and instead
|
|
|
|
// want the whole expression to be on a new line.
|
|
|
|
//
|
|
|
|
// Good: Bad:
|
|
|
|
// verylongcall( verylongcall((
|
|
|
|
// (a, b) => { a,
|
|
|
|
// } b,
|
|
|
|
// }) ) => {
|
|
|
|
// })
|
|
|
|
const parent = path.getParentNode();
|
2017-03-04 02:39:37 +03:00
|
|
|
if (
|
|
|
|
(parent.type === "CallExpression" || parent.type === "NewExpression") &&
|
2017-03-03 06:29:53 +03:00
|
|
|
util.getLast(parent.arguments) === path.getValue() &&
|
2017-03-04 02:39:37 +03:00
|
|
|
shouldGroupLastArg(parent.arguments)
|
|
|
|
) {
|
2017-03-03 06:29:53 +03:00
|
|
|
return concat(["(", join(", ", printed), ")"]);
|
|
|
|
}
|
|
|
|
|
2017-01-05 00:52:49 +03:00
|
|
|
return concat([
|
|
|
|
"(",
|
2017-01-09 20:09:04 +03:00
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
2017-01-28 18:50:22 +03:00
|
|
|
concat([softline, join(concat([",", line]), printed)])
|
2017-01-09 20:09:04 +03:00
|
|
|
),
|
2017-02-23 20:57:51 +03:00
|
|
|
ifBreak(
|
|
|
|
canHaveTrailingComma && shouldPrintComma(options, "all") ? "," : ""
|
|
|
|
),
|
2017-01-05 00:52:49 +03:00
|
|
|
softline,
|
|
|
|
")"
|
|
|
|
]);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function printObjectMethod(path, options, print) {
|
|
|
|
var objMethod = path.getValue();
|
|
|
|
var parts = [];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (objMethod.async) parts.push("async ");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (objMethod.generator) parts.push("*");
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-31 07:03:22 +03:00
|
|
|
if (
|
|
|
|
objMethod.method || objMethod.kind === "get" || objMethod.kind === "set"
|
|
|
|
) {
|
2016-12-23 21:38:10 +03:00
|
|
|
return printMethod(path, options, print);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-19 20:37:24 +03:00
|
|
|
var key = printPropertyKey(path, options, print);
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (objMethod.computed) {
|
|
|
|
parts.push("[", key, "]");
|
|
|
|
} else {
|
|
|
|
parts.push(key);
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-12 02:14:46 +03:00
|
|
|
if (objMethod.typeParameters) {
|
|
|
|
parts.push(path.call(print, "typeParameters"));
|
|
|
|
}
|
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
parts.push(
|
2017-01-24 21:54:01 +03:00
|
|
|
group(
|
2017-01-13 23:03:53 +03:00
|
|
|
concat([
|
|
|
|
printFunctionParams(path, print, options),
|
|
|
|
printReturnType(path, print)
|
|
|
|
])
|
|
|
|
),
|
2016-12-23 21:38:10 +03:00
|
|
|
" ",
|
|
|
|
path.call(print, "body")
|
|
|
|
);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-09 21:47:02 +03:00
|
|
|
return concat(parts);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2016-12-30 21:32:43 +03:00
|
|
|
function printReturnType(path, print) {
|
|
|
|
const n = path.getValue();
|
2017-01-28 18:50:22 +03:00
|
|
|
const parts = [path.call(print, "returnType")];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-31 07:03:22 +03:00
|
|
|
if (n.predicate) {
|
2016-12-31 07:10:22 +03:00
|
|
|
// The return type will already add the colon, but otherwise we
|
|
|
|
// need to do it ourselves
|
2017-01-09 20:09:04 +03:00
|
|
|
parts.push(n.returnType ? " " : ": ", path.call(print, "predicate"));
|
2016-12-30 21:32:43 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-30 21:32:43 +03:00
|
|
|
return concat(parts);
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:11:52 +03:00
|
|
|
function typeIsFunction(type) {
|
|
|
|
return type === "FunctionExpression" ||
|
|
|
|
type === "ArrowFunctionExpression" ||
|
|
|
|
type === "NewExpression";
|
|
|
|
}
|
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
function printExportDeclaration(path, options, print) {
|
|
|
|
var decl = path.getValue();
|
2017-01-28 18:50:22 +03:00
|
|
|
var parts = ["export "];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
namedTypes.Declaration.assert(decl);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-31 07:03:22 +03:00
|
|
|
if (decl["default"] || decl.type === "ExportDefaultDeclaration") {
|
2016-12-23 21:38:10 +03:00
|
|
|
parts.push("default ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-02-23 20:57:51 +03:00
|
|
|
parts.push(
|
|
|
|
comments.printDanglingComments(path, options, /* sameIndent */ true)
|
|
|
|
);
|
2017-02-20 01:39:09 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (decl.declaration) {
|
|
|
|
parts.push(path.call(print, "declaration"));
|
2017-01-11 15:43:27 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
if (
|
|
|
|
decl.type === "ExportDefaultDeclaration" &&
|
2017-02-16 06:56:11 +03:00
|
|
|
(decl.declaration.type !== "ClassDeclaration" &&
|
|
|
|
decl.declaration.type !== "FunctionDeclaration")
|
2017-01-13 23:03:53 +03:00
|
|
|
) {
|
2017-01-11 15:43:27 +03:00
|
|
|
parts.push(";");
|
|
|
|
}
|
2017-01-16 19:58:23 +03:00
|
|
|
} else {
|
|
|
|
if (decl.specifiers && decl.specifiers.length > 0) {
|
|
|
|
if (
|
|
|
|
decl.specifiers.length === 1 &&
|
2017-02-16 06:56:11 +03:00
|
|
|
decl.specifiers[0].type === "ExportBatchSpecifier"
|
2017-01-16 19:58:23 +03:00
|
|
|
) {
|
|
|
|
parts.push("*");
|
2017-01-21 06:20:31 +03:00
|
|
|
} else if (
|
2017-02-23 20:57:51 +03:00
|
|
|
(decl.specifiers.length === 1 &&
|
|
|
|
decl.specifiers[0].type === "ExportDefaultSpecifier") ||
|
2017-02-16 06:56:11 +03:00
|
|
|
decl.specifiers[0].type === "ExportNamespaceSpecifier"
|
2017-01-21 06:20:31 +03:00
|
|
|
) {
|
|
|
|
parts.push(path.map(print, "specifiers")[0]);
|
2017-01-16 19:58:23 +03:00
|
|
|
} else {
|
|
|
|
parts.push(
|
|
|
|
decl.exportKind === "type" ? "type " : "",
|
2017-01-24 21:54:01 +03:00
|
|
|
group(
|
2017-01-16 19:58:23 +03:00
|
|
|
concat([
|
|
|
|
"{",
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
concat([
|
|
|
|
options.bracketSpacing ? line : softline,
|
2017-01-28 18:50:22 +03:00
|
|
|
join(concat([",", line]), path.map(print, "specifiers"))
|
2017-01-16 19:58:23 +03:00
|
|
|
])
|
|
|
|
),
|
2017-02-18 06:44:55 +03:00
|
|
|
ifBreak(shouldPrintComma(options) ? "," : ""),
|
2017-01-16 19:58:23 +03:00
|
|
|
options.bracketSpacing ? line : softline,
|
|
|
|
"}"
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-01-09 20:09:04 +03:00
|
|
|
} else {
|
2017-01-16 19:58:23 +03:00
|
|
|
parts.push("{}");
|
2017-01-09 20:09:04 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-09 20:09:04 +03:00
|
|
|
if (decl.source) {
|
|
|
|
parts.push(" from ", path.call(print, "source"));
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2017-01-11 15:43:27 +03:00
|
|
|
|
2017-01-13 23:03:53 +03:00
|
|
|
parts.push(";");
|
2017-01-09 20:09:04 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
return concat(parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
function printFlowDeclaration(path, parts) {
|
|
|
|
var parentExportDecl = util.getParentExportDeclaration(path);
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
if (parentExportDecl) {
|
2016-12-31 07:03:22 +03:00
|
|
|
assert.strictEqual(parentExportDecl.type, "DeclareExportDeclaration");
|
2016-12-23 21:38:10 +03:00
|
|
|
} else {
|
|
|
|
// If the parent node has type DeclareExportDeclaration, then it
|
|
|
|
// will be responsible for printing the "declare" token. Otherwise
|
|
|
|
// it needs to be printed with this non-exported declaration node.
|
|
|
|
parts.unshift("declare ");
|
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
return concat(parts);
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:36:55 +03:00
|
|
|
function printClass(path, options, print) {
|
2016-12-30 08:01:44 +03:00
|
|
|
const n = path.getValue();
|
2017-01-28 18:50:22 +03:00
|
|
|
const parts = ["class"];
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2016-12-30 08:01:44 +03:00
|
|
|
if (n.id) {
|
2016-12-31 07:03:22 +03:00
|
|
|
parts.push(" ", path.call(print, "id"), path.call(print, "typeParameters"));
|
2016-12-30 08:01:44 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-26 00:36:55 +03:00
|
|
|
const partsGroup = [];
|
2016-12-30 08:01:44 +03:00
|
|
|
if (n.superClass) {
|
2017-01-26 00:36:55 +03:00
|
|
|
partsGroup.push(
|
|
|
|
line,
|
|
|
|
"extends ",
|
2017-01-09 20:09:04 +03:00
|
|
|
path.call(print, "superClass"),
|
|
|
|
path.call(print, "superTypeParameters")
|
|
|
|
);
|
|
|
|
} else if (n.extends && n.extends.length > 0) {
|
2017-01-26 00:36:55 +03:00
|
|
|
partsGroup.push(line, "extends ", join(", ", path.map(print, "extends")));
|
2017-01-09 20:09:04 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-31 07:03:22 +03:00
|
|
|
if (n["implements"] && n["implements"].length > 0) {
|
2017-01-28 18:50:22 +03:00
|
|
|
partsGroup.push(
|
|
|
|
line,
|
|
|
|
"implements ",
|
|
|
|
join(", ", path.map(print, "implements"))
|
|
|
|
);
|
2017-01-26 00:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (partsGroup.length > 0) {
|
|
|
|
parts.push(group(indent(options.tabWidth, concat(partsGroup))));
|
2016-12-30 08:01:44 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-30 08:01:44 +03:00
|
|
|
parts.push(" ", path.call(print, "body"));
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2016-12-30 08:01:44 +03:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2017-01-26 22:58:47 +03:00
|
|
|
function printMemberLookup(path, options, print) {
|
2017-01-05 23:26:45 +03:00
|
|
|
const property = path.call(print, "property");
|
|
|
|
const n = path.getValue();
|
2017-01-26 22:58:47 +03:00
|
|
|
|
|
|
|
return concat(
|
|
|
|
n.computed
|
2017-01-28 18:50:22 +03:00
|
|
|
? [
|
|
|
|
"[",
|
|
|
|
group(
|
|
|
|
concat([
|
|
|
|
indent(options.tabWidth, concat([softline, property])),
|
|
|
|
softline
|
|
|
|
])
|
|
|
|
),
|
|
|
|
"]"
|
|
|
|
]
|
|
|
|
: [".", property]
|
|
|
|
);
|
2017-01-05 23:26:45 +03:00
|
|
|
}
|
|
|
|
|
2017-01-08 22:44:44 +03:00
|
|
|
// We detect calls on member expressions specially to format a
|
|
|
|
// comman pattern better. The pattern we are looking for is this:
|
|
|
|
//
|
|
|
|
// arr
|
|
|
|
// .map(x => x + 1)
|
|
|
|
// .filter(x => x > 10)
|
|
|
|
// .some(x => x % 2)
|
|
|
|
//
|
2017-01-31 23:32:42 +03:00
|
|
|
// The way it is structured in the AST is via a nested sequence of
|
|
|
|
// MemberExpression and CallExpression. We need to traverse the AST
|
|
|
|
// and make groups out of it to print it in the desired way.
|
|
|
|
function printMemberChain(path, options, print) {
|
|
|
|
// The first phase is to linearize the AST by traversing it down.
|
|
|
|
//
|
|
|
|
// a().b()
|
|
|
|
// has the following AST structure:
|
|
|
|
// CallExpression(MemberExpression(CallExpression(Identifier)))
|
|
|
|
// and we transform it into
|
|
|
|
// [Identifier, CallExpression, MemberExpression, CallExpression]
|
|
|
|
const printedNodes = [];
|
|
|
|
|
|
|
|
function rec(path) {
|
|
|
|
const node = path.getValue();
|
|
|
|
if (node.type === "CallExpression") {
|
|
|
|
printedNodes.unshift({
|
|
|
|
node: node,
|
2017-02-05 05:31:54 +03:00
|
|
|
printed: comments.printComments(
|
|
|
|
path,
|
|
|
|
p => printArgumentsList(path, options, print),
|
|
|
|
options
|
|
|
|
)
|
2017-01-31 23:32:42 +03:00
|
|
|
});
|
|
|
|
path.call(callee => rec(callee), "callee");
|
|
|
|
} else if (node.type === "MemberExpression") {
|
|
|
|
printedNodes.unshift({
|
|
|
|
node: node,
|
2017-02-03 19:50:51 +03:00
|
|
|
printed: comments.printComments(
|
|
|
|
path,
|
|
|
|
p => printMemberLookup(path, options, print),
|
|
|
|
options
|
|
|
|
)
|
2017-01-31 23:32:42 +03:00
|
|
|
});
|
|
|
|
path.call(object => rec(object), "object");
|
|
|
|
} else {
|
|
|
|
printedNodes.unshift({
|
|
|
|
node: node,
|
|
|
|
printed: path.call(print)
|
|
|
|
});
|
2017-01-08 22:44:44 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-05 05:31:54 +03:00
|
|
|
// Note: the comments of the root node have already been printed, so we
|
|
|
|
// need to extract this first call without printing them as they would
|
|
|
|
// if handled inside of the recursive call.
|
|
|
|
printedNodes.unshift({
|
|
|
|
node: path.getValue(),
|
|
|
|
printed: printArgumentsList(path, options, print)
|
|
|
|
});
|
|
|
|
path.call(callee => rec(callee), "callee");
|
2017-01-31 23:32:42 +03:00
|
|
|
|
|
|
|
// Once we have a linear list of printed nodes, we want to create groups out
|
|
|
|
// of it.
|
|
|
|
//
|
|
|
|
// a().b.c().d().e
|
|
|
|
// will be grouped as
|
|
|
|
// [
|
|
|
|
// [Identifier, CallExpression],
|
|
|
|
// [MemberExpression, MemberExpression, CallExpression],
|
|
|
|
// [MemberExpression, CallExpression],
|
|
|
|
// [MemberExpression],
|
|
|
|
// ]
|
|
|
|
// so that we can print it as
|
|
|
|
// a()
|
|
|
|
// .b.c()
|
|
|
|
// .d()
|
|
|
|
// .e
|
|
|
|
|
2017-02-04 00:18:54 +03:00
|
|
|
// The first group is the first node followed by
|
|
|
|
// - as many CallExpression as possible
|
|
|
|
// < fn()()() >.something()
|
|
|
|
// - then, as many MemberExpression as possible but the last one
|
|
|
|
// < this.items >.something()
|
2017-01-31 23:32:42 +03:00
|
|
|
var groups = [];
|
|
|
|
var currentGroup = [printedNodes[0]];
|
|
|
|
var i = 1;
|
|
|
|
for (; i < printedNodes.length; ++i) {
|
|
|
|
if (printedNodes[i].node.type === "CallExpression") {
|
|
|
|
currentGroup.push(printedNodes[i]);
|
2017-01-09 20:09:04 +03:00
|
|
|
} else {
|
2017-01-31 23:32:42 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-04 00:18:54 +03:00
|
|
|
for (; i + 1 < printedNodes.length; ++i) {
|
2017-02-09 18:44:56 +03:00
|
|
|
if (
|
|
|
|
printedNodes[i].node.type === "MemberExpression" &&
|
2017-02-16 06:56:11 +03:00
|
|
|
printedNodes[i + 1].node.type === "MemberExpression"
|
2017-02-09 18:44:56 +03:00
|
|
|
) {
|
2017-02-04 00:18:54 +03:00
|
|
|
currentGroup.push(printedNodes[i]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 23:32:42 +03:00
|
|
|
groups.push(currentGroup);
|
|
|
|
currentGroup = [];
|
|
|
|
|
|
|
|
// Then, each following group is a sequence of MemberExpression followed by
|
|
|
|
// a sequence of CallExpression. To compute it, we keep adding things to the
|
|
|
|
// group until we has seen a CallExpression in the past and reach a
|
|
|
|
// MemberExpression
|
|
|
|
var hasSeenCallExpression = false;
|
|
|
|
for (; i < printedNodes.length; ++i) {
|
2017-02-03 19:50:51 +03:00
|
|
|
if (
|
|
|
|
hasSeenCallExpression && printedNodes[i].node.type === "MemberExpression"
|
|
|
|
) {
|
2017-02-23 18:34:52 +03:00
|
|
|
// [0] should be appended at the end of the group instead of the
|
|
|
|
// beginning of the next one
|
|
|
|
if (printedNodes[i].node.computed) {
|
|
|
|
currentGroup.push(printedNodes[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-31 23:32:42 +03:00
|
|
|
groups.push(currentGroup);
|
|
|
|
currentGroup = [];
|
|
|
|
hasSeenCallExpression = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (printedNodes[i].node.type === "CallExpression") {
|
|
|
|
hasSeenCallExpression = true;
|
2017-01-08 22:44:44 +03:00
|
|
|
}
|
2017-01-31 23:32:42 +03:00
|
|
|
currentGroup.push(printedNodes[i]);
|
|
|
|
}
|
|
|
|
if (currentGroup.length > 0) {
|
|
|
|
groups.push(currentGroup);
|
2017-01-08 22:44:44 +03:00
|
|
|
}
|
2017-01-31 23:32:42 +03:00
|
|
|
|
|
|
|
// There are cases like Object.keys(), Observable.of(), _.values() where
|
|
|
|
// they are the subject of all the chained calls and therefore should
|
|
|
|
// be kept on the same line:
|
|
|
|
//
|
|
|
|
// Object.keys(items)
|
|
|
|
// .filter(x => x)
|
|
|
|
// .map(x => x)
|
|
|
|
//
|
|
|
|
// In order to detect those cases, we use an heuristic: if the first
|
|
|
|
// node is just an identifier with the name starting with a capital
|
2017-02-23 17:41:44 +03:00
|
|
|
// letter, just a sequence of _$ or this. The rationale is that they are
|
2017-01-31 23:32:42 +03:00
|
|
|
// likely to be factories.
|
2017-02-16 06:56:11 +03:00
|
|
|
const shouldMerge = groups[0].length === 1 &&
|
2017-02-23 17:41:44 +03:00
|
|
|
(groups[0][0].node.type === "ThisExpression" ||
|
2017-02-23 20:57:51 +03:00
|
|
|
(groups[0][0].node.type === "Identifier" &&
|
|
|
|
groups[0][0].node.name.match(/(^[A-Z])|^[_$]+$/))) &&
|
2017-02-13 18:07:33 +03:00
|
|
|
groups.length >= 2;
|
2017-01-31 23:32:42 +03:00
|
|
|
|
|
|
|
function printGroup(printedGroup) {
|
|
|
|
return concat(printedGroup.map(tuple => tuple.printed));
|
|
|
|
}
|
|
|
|
|
2017-02-28 18:55:32 +03:00
|
|
|
function printIndentedGroup(groups) {
|
2017-02-13 18:07:33 +03:00
|
|
|
return indent(
|
|
|
|
options.tabWidth,
|
2017-02-28 18:55:32 +03:00
|
|
|
group(concat([hardline, join(hardline, groups.map(printGroup))]))
|
2017-02-13 18:07:33 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-31 23:32:42 +03:00
|
|
|
const printedGroups = groups.map(printGroup);
|
|
|
|
const oneLine = concat(printedGroups);
|
2017-02-13 20:05:18 +03:00
|
|
|
const hasComment = groups.length >= 2 && groups[1][0].node.comments;
|
2017-01-31 23:32:42 +03:00
|
|
|
|
|
|
|
// If we only have a single `.`, we shouldn't do anything fancy and just
|
|
|
|
// render everything concatenated together.
|
2017-03-01 20:37:02 +03:00
|
|
|
if (
|
|
|
|
groups.length <= (shouldMerge ? 3 : 2) &&
|
|
|
|
!hasComment &&
|
|
|
|
// (a || b).map() should be break before .map() instead of ||
|
|
|
|
groups[0][0].node.type !== "LogicalExpression"
|
|
|
|
) {
|
2017-01-31 23:32:42 +03:00
|
|
|
return group(oneLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
const expanded = concat([
|
|
|
|
printGroup(groups[0]),
|
2017-02-28 18:55:32 +03:00
|
|
|
shouldMerge ? concat(groups.slice(1, 2).map(printGroup)) : "",
|
|
|
|
printIndentedGroup(groups.slice(shouldMerge ? 2 : 1))
|
2017-01-31 23:32:42 +03:00
|
|
|
]);
|
|
|
|
|
2017-02-13 20:05:18 +03:00
|
|
|
// If there's a comment, we don't want to print in one line.
|
|
|
|
if (hasComment) {
|
|
|
|
return group(expanded);
|
|
|
|
}
|
|
|
|
|
2017-01-31 23:32:42 +03:00
|
|
|
// If any group but the last one has a hard line, we want to force expand
|
|
|
|
// it. If the last group is a function it's okay to inline if it fits.
|
|
|
|
if (printedGroups.slice(0, -1).some(willBreak)) {
|
|
|
|
return group(expanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
return conditionalGroup([oneLine, expanded]);
|
2017-01-08 22:44:44 +03:00
|
|
|
}
|
|
|
|
|
2017-01-19 00:25:20 +03:00
|
|
|
function isEmptyJSXElement(node) {
|
|
|
|
if (node.children.length === 0) return true;
|
|
|
|
if (node.children.length > 1) return false;
|
|
|
|
|
|
|
|
// if there is one child but it's just a newline, treat as empty
|
|
|
|
const value = node.children[0].value;
|
|
|
|
if (!/\S/.test(value) && /\n/.test(value)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSX Children are strange, mostly for two reasons:
|
|
|
|
// 1. JSX reads newlines into string values, instead of skipping them like JS
|
|
|
|
// 2. up to one whitespace between elements within a line is significant,
|
|
|
|
// but not between lines.
|
|
|
|
//
|
|
|
|
// So for one thing, '\n' needs to be parsed out of string literals
|
|
|
|
// and turned into hardlines (with string boundaries otherwise using softline)
|
|
|
|
//
|
|
|
|
// For another, leading, trailing, and lone whitespace all need to
|
|
|
|
// turn themselves into the rather ugly `{' '}` when breaking.
|
2017-02-07 21:47:34 +03:00
|
|
|
function printJSXChildren(path, options, print, jsxWhitespace) {
|
2017-01-19 00:25:20 +03:00
|
|
|
const n = path.getValue();
|
|
|
|
const children = [];
|
|
|
|
|
|
|
|
// using `map` instead of `each` because it provides `i`
|
2017-01-19 02:31:46 +03:00
|
|
|
path.map(
|
|
|
|
function(childPath, i) {
|
|
|
|
const child = childPath.getValue();
|
|
|
|
const isLiteral = namedTypes.Literal.check(child);
|
|
|
|
|
|
|
|
if (isLiteral && typeof child.value === "string") {
|
2017-01-19 19:30:35 +03:00
|
|
|
// There's a bug in the flow parser where it doesn't unescape the
|
|
|
|
// value field. To workaround this, we can use rawValue which is
|
|
|
|
// correctly escaped (since it parsed).
|
|
|
|
// We really want to use value and re-escape it ourself when possible
|
|
|
|
// though.
|
2017-02-02 02:29:42 +03:00
|
|
|
const partiallyEscapedValue = options.parser === "flow"
|
2017-01-20 21:12:37 +03:00
|
|
|
? child.raw
|
|
|
|
: util.htmlEscapeInsideAngleBracket(child.value);
|
2017-02-03 19:50:51 +03:00
|
|
|
const value = partiallyEscapedValue.replace(/\u00a0/g, " ");
|
2017-01-19 19:30:35 +03:00
|
|
|
|
|
|
|
if (/\S/.test(value)) {
|
2017-01-27 22:20:01 +03:00
|
|
|
// treat each line of text as its own entity
|
|
|
|
value.split(/(\n\s*)/).forEach(line => {
|
|
|
|
const newlines = line.match(/\n/g);
|
|
|
|
if (newlines) {
|
|
|
|
children.push(hardline);
|
|
|
|
|
|
|
|
// allow one extra newline
|
|
|
|
if (newlines.length > 1) {
|
2017-01-28 18:50:22 +03:00
|
|
|
children.push(hardline);
|
2017-01-27 22:20:01 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const beginSpace = /^\s+/.test(line);
|
|
|
|
if (beginSpace) {
|
|
|
|
children.push(jsxWhitespace);
|
2017-03-01 00:30:54 +03:00
|
|
|
children.push(softline);
|
2017-01-27 22:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const stripped = line.replace(/^\s+|\s+$/g, "");
|
|
|
|
if (stripped) {
|
|
|
|
children.push(stripped);
|
|
|
|
}
|
2017-01-19 00:25:20 +03:00
|
|
|
|
2017-01-27 22:20:01 +03:00
|
|
|
const endSpace = /\s+$/.test(line);
|
|
|
|
if (endSpace) {
|
2017-03-01 00:30:54 +03:00
|
|
|
children.push(softline);
|
2017-01-27 22:20:01 +03:00
|
|
|
children.push(jsxWhitespace);
|
|
|
|
}
|
|
|
|
});
|
2017-02-07 17:58:25 +03:00
|
|
|
|
2017-02-15 00:41:51 +03:00
|
|
|
if (!isLineNext(util.getLast(children))) {
|
|
|
|
children.push(softline);
|
|
|
|
}
|
2017-01-19 19:30:35 +03:00
|
|
|
} else if (/\n/.test(value)) {
|
2017-01-19 00:25:20 +03:00
|
|
|
children.push(hardline);
|
2017-01-27 22:20:01 +03:00
|
|
|
|
|
|
|
// allow one extra newline
|
|
|
|
if (value.match(/\n/g).length > 1) {
|
2017-01-28 18:50:22 +03:00
|
|
|
children.push(hardline);
|
2017-01-27 22:20:01 +03:00
|
|
|
}
|
2017-01-19 19:30:35 +03:00
|
|
|
} else if (/\s/.test(value)) {
|
2017-01-19 02:31:46 +03:00
|
|
|
// whitespace-only without newlines,
|
|
|
|
// eg; a single space separating two elements
|
|
|
|
children.push(jsxWhitespace);
|
2017-01-19 00:25:20 +03:00
|
|
|
children.push(softline);
|
|
|
|
}
|
2017-01-19 02:31:46 +03:00
|
|
|
} else {
|
|
|
|
children.push(print(childPath));
|
2017-01-19 00:25:20 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
// add a line unless it's followed by a JSX newline
|
|
|
|
let next = n.children[i + 1];
|
|
|
|
if (!(next && /^\s*\n/.test(next.value))) {
|
|
|
|
children.push(softline);
|
|
|
|
}
|
2017-01-19 00:25:20 +03:00
|
|
|
}
|
2017-01-19 02:31:46 +03:00
|
|
|
},
|
|
|
|
"children"
|
|
|
|
);
|
2017-01-19 00:25:20 +03:00
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSX expands children from the inside-out, instead of the outside-in.
|
|
|
|
// This is both to break children before attributes,
|
|
|
|
// and to ensure that when children break, their parents do as well.
|
|
|
|
//
|
|
|
|
// Any element that is written without any newlines and fits on a single line
|
|
|
|
// is left that way.
|
|
|
|
// Not only that, any user-written-line containing multiple JSX siblings
|
|
|
|
// should also be kept on one line if possible,
|
|
|
|
// so each user-written-line is wrapped in its own group.
|
|
|
|
//
|
|
|
|
// Elements that contain newlines or don't fit on a single line (recursively)
|
|
|
|
// are fully-split, using hardline and shouldBreak: true.
|
|
|
|
//
|
|
|
|
// To support that case properly, all leading and trailing spaces
|
|
|
|
// are stripped from the list of children, and replaced with a single hardline.
|
2017-01-12 02:15:12 +03:00
|
|
|
function printJSXElement(path, options, print) {
|
|
|
|
const n = path.getValue();
|
|
|
|
|
2017-01-24 02:47:11 +03:00
|
|
|
// Turn <div></div> into <div />
|
2017-01-19 00:25:20 +03:00
|
|
|
if (isEmptyJSXElement(n)) {
|
|
|
|
n.openingElement.selfClosing = true;
|
|
|
|
delete n.closingElement;
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:47:11 +03:00
|
|
|
// If no children, just print the opening element
|
2017-01-19 00:25:20 +03:00
|
|
|
const openingLines = path.call(print, "openingElement");
|
2017-01-12 02:15:12 +03:00
|
|
|
if (n.openingElement.selfClosing) {
|
|
|
|
assert.ok(!n.closingElement);
|
2017-01-19 00:25:20 +03:00
|
|
|
return openingLines;
|
|
|
|
}
|
2017-03-08 04:19:05 +03:00
|
|
|
// Record any breaks. Should never go from true to false, only false to true.
|
|
|
|
let forcedBreak = willBreak(openingLines);
|
2017-01-12 02:15:12 +03:00
|
|
|
|
2017-02-07 21:47:34 +03:00
|
|
|
const jsxWhitespace = options.singleQuote
|
|
|
|
? ifBreak("{' '}", " ")
|
|
|
|
: ifBreak('{" "}', " ");
|
|
|
|
const children = printJSXChildren(path, options, print, jsxWhitespace);
|
2017-01-19 00:25:20 +03:00
|
|
|
|
2017-01-27 22:20:01 +03:00
|
|
|
// Trim trailing lines, recording if there was a hardline
|
|
|
|
let numTrailingHard = 0;
|
2017-01-24 02:47:11 +03:00
|
|
|
while (children.length && isLineNext(util.getLast(children))) {
|
|
|
|
if (willBreak(util.getLast(children))) {
|
2017-01-27 22:20:01 +03:00
|
|
|
++numTrailingHard;
|
2017-01-24 02:47:11 +03:00
|
|
|
forcedBreak = true;
|
|
|
|
}
|
2017-01-19 00:25:20 +03:00
|
|
|
children.pop();
|
|
|
|
}
|
2017-01-27 22:20:01 +03:00
|
|
|
// allow one extra newline
|
|
|
|
if (numTrailingHard > 1) {
|
|
|
|
children.push(hardline);
|
|
|
|
}
|
2017-01-12 02:15:12 +03:00
|
|
|
|
2017-01-27 22:20:01 +03:00
|
|
|
// Trim leading lines, recording if there was a hardline
|
|
|
|
let numLeadingHard = 0;
|
2017-01-24 02:47:11 +03:00
|
|
|
while (children.length && isLineNext(children[0])) {
|
|
|
|
if (willBreak(children[0])) {
|
2017-01-27 22:20:01 +03:00
|
|
|
++numLeadingHard;
|
2017-01-24 02:47:11 +03:00
|
|
|
forcedBreak = true;
|
|
|
|
}
|
2017-01-19 00:25:20 +03:00
|
|
|
children.shift();
|
|
|
|
}
|
2017-01-27 22:20:01 +03:00
|
|
|
// allow one extra newline
|
|
|
|
if (numLeadingHard > 1) {
|
|
|
|
children.unshift(hardline);
|
|
|
|
}
|
2017-01-12 02:15:12 +03:00
|
|
|
|
2017-01-24 02:47:11 +03:00
|
|
|
// Group by line, recording if there was a hardline.
|
2017-02-07 21:47:34 +03:00
|
|
|
let groups = [[]]; // Initialize the first line's group
|
|
|
|
children.forEach((child, i) => {
|
|
|
|
// leading and trailing JSX whitespace don't go into a group
|
|
|
|
if (child === jsxWhitespace) {
|
|
|
|
if (i === 0) {
|
|
|
|
groups.unshift(child);
|
|
|
|
return;
|
|
|
|
} else if (i === children.length - 1) {
|
|
|
|
groups.push(child);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-24 02:47:11 +03:00
|
|
|
}
|
2017-01-12 02:15:12 +03:00
|
|
|
|
2017-02-07 21:47:34 +03:00
|
|
|
let prev = children[i - 1];
|
|
|
|
if (prev && willBreak(prev)) {
|
|
|
|
forcedBreak = true;
|
2017-01-12 23:45:47 +03:00
|
|
|
|
2017-02-07 21:47:34 +03:00
|
|
|
// On a new line, so create a new group and put this element in it.
|
|
|
|
groups.push([child]);
|
|
|
|
} else {
|
|
|
|
// Not on a newline, so add this element to the current group.
|
|
|
|
util.getLast(groups).push(child);
|
|
|
|
}
|
2017-01-24 02:47:11 +03:00
|
|
|
|
2017-02-07 21:47:34 +03:00
|
|
|
// Ensure we record hardline of last element.
|
|
|
|
if (!forcedBreak && i === children.length - 1) {
|
|
|
|
if (willBreak(child)) forcedBreak = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const childrenGroupedByLine = [
|
|
|
|
hardline,
|
|
|
|
// Conditional groups suppress break propagation; we want to output
|
|
|
|
// hard lines without breaking up the entire jsx element.
|
|
|
|
// Note that leading and trailing JSX Whitespace don't go into a group.
|
2017-02-16 06:56:11 +03:00
|
|
|
concat(
|
|
|
|
groups.map(
|
|
|
|
contents =>
|
|
|
|
Array.isArray(contents)
|
|
|
|
? conditionalGroup([concat(contents)])
|
|
|
|
: contents
|
|
|
|
)
|
|
|
|
)
|
2017-02-07 21:47:34 +03:00
|
|
|
];
|
2017-01-12 23:45:47 +03:00
|
|
|
|
2017-01-19 00:25:20 +03:00
|
|
|
const closingLines = path.call(print, "closingElement");
|
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
const multiLineElem = group(
|
|
|
|
concat([
|
|
|
|
openingLines,
|
|
|
|
indent(
|
|
|
|
options.tabWidth,
|
|
|
|
group(concat(childrenGroupedByLine), { shouldBreak: true })
|
|
|
|
),
|
|
|
|
hardline,
|
|
|
|
closingLines
|
|
|
|
])
|
|
|
|
);
|
2017-01-19 00:25:20 +03:00
|
|
|
|
2017-01-24 02:47:11 +03:00
|
|
|
if (forcedBreak) {
|
|
|
|
return multiLineElem;
|
2017-01-12 02:15:12 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 02:47:11 +03:00
|
|
|
return conditionalGroup([
|
2017-01-28 18:50:22 +03:00
|
|
|
group(concat([openingLines, concat(children), closingLines])),
|
2017-01-24 02:47:11 +03:00
|
|
|
multiLineElem
|
|
|
|
]);
|
2017-01-19 00:25:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function maybeWrapJSXElementInParens(path, elem, options) {
|
2017-01-12 02:15:12 +03:00
|
|
|
const parent = path.getParentNode();
|
2017-01-19 00:25:20 +03:00
|
|
|
if (!parent) return elem;
|
|
|
|
|
|
|
|
const NO_WRAP_PARENTS = {
|
2017-03-05 19:56:07 +03:00
|
|
|
ArrayExpression: true,
|
2017-01-19 02:31:46 +03:00
|
|
|
JSXElement: true,
|
2017-03-01 23:05:57 +03:00
|
|
|
JSXExpressionContainer: true,
|
2017-01-19 02:31:46 +03:00
|
|
|
ExpressionStatement: true,
|
|
|
|
CallExpression: true,
|
2017-01-26 22:51:08 +03:00
|
|
|
ConditionalExpression: true,
|
2017-01-28 18:50:22 +03:00
|
|
|
LogicalExpression: true
|
2017-01-19 00:25:20 +03:00
|
|
|
};
|
|
|
|
if (NO_WRAP_PARENTS[parent.type]) {
|
2017-01-12 02:15:12 +03:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:54:01 +03:00
|
|
|
return group(
|
2017-01-13 23:03:53 +03:00
|
|
|
concat([
|
|
|
|
ifBreak("("),
|
2017-01-28 18:50:22 +03:00
|
|
|
indent(options.tabWidth, concat([softline, elem])),
|
2017-01-13 23:03:53 +03:00
|
|
|
softline,
|
|
|
|
ifBreak(")")
|
|
|
|
])
|
|
|
|
);
|
2017-01-12 02:15:12 +03:00
|
|
|
}
|
|
|
|
|
2017-01-19 01:01:17 +03:00
|
|
|
function isBinaryish(node) {
|
|
|
|
return node.type === "BinaryExpression" || node.type === "LogicalExpression";
|
|
|
|
}
|
|
|
|
|
2017-02-15 17:41:06 +03:00
|
|
|
function shouldInlineLogicalExpression(node) {
|
2017-02-16 06:56:11 +03:00
|
|
|
return node.type === "LogicalExpression" &&
|
|
|
|
(node.right.type === "ObjectExpression" ||
|
|
|
|
node.right.type === "ArrayExpression");
|
2017-02-15 17:41:06 +03:00
|
|
|
}
|
|
|
|
|
2017-01-19 01:01:17 +03:00
|
|
|
// For binary expressions to be consistent, we need to group
|
|
|
|
// subsequent operators with the same precedence level under a single
|
|
|
|
// group. Otherwise they will be nested such that some of them break
|
|
|
|
// onto new lines but not all. Operators with the same precedence
|
|
|
|
// level should either all break or not. Because we group them by
|
|
|
|
// precedence level and the AST is structured based on precedence
|
|
|
|
// level, things are naturally broken up correctly, i.e. `&&` is
|
|
|
|
// broken before `+`.
|
2017-01-27 22:03:44 +03:00
|
|
|
function printBinaryishExpressions(path, parts, print, options, isNested) {
|
2017-01-19 01:01:17 +03:00
|
|
|
let node = path.getValue();
|
|
|
|
|
|
|
|
// We treat BinaryExpression and LogicalExpression nodes the same.
|
2017-01-19 02:31:46 +03:00
|
|
|
if (isBinaryish(node)) {
|
2017-01-19 01:01:17 +03:00
|
|
|
// Put all operators with the same precedence level in the same
|
|
|
|
// group. The reason we only need to do this with the `left`
|
|
|
|
// expression is because given an expression like `1 + 2 - 3`, it
|
|
|
|
// is always parsed like `((1 + 2) - 3)`, meaning the `left` side
|
|
|
|
// is where the rest of the expression will exist. Binary
|
|
|
|
// expressions on the right side mean they have a difference
|
|
|
|
// precedence level and should be treated as a separate group, so
|
2017-03-09 00:11:53 +03:00
|
|
|
// print them normally. (This doesn't hold for the `**` operator,
|
|
|
|
// which is unique in that it is right-associative.)
|
2017-01-19 02:31:46 +03:00
|
|
|
if (
|
|
|
|
util.getPrecedence(node.left.operator) ===
|
2017-02-09 18:36:05 +03:00
|
|
|
util.getPrecedence(node.operator)
|
2017-03-09 00:11:53 +03:00
|
|
|
&& node.operator !== "**"
|
2017-01-19 02:31:46 +03:00
|
|
|
) {
|
2017-01-19 01:01:17 +03:00
|
|
|
// Flatten them out by recursively calling this function. The
|
|
|
|
// printed values will all be appended to `parts`.
|
2017-01-28 18:50:22 +03:00
|
|
|
path.call(
|
|
|
|
left =>
|
|
|
|
printBinaryishExpressions(
|
|
|
|
left,
|
|
|
|
parts,
|
|
|
|
print,
|
|
|
|
options,
|
|
|
|
/* isNested */ true
|
|
|
|
),
|
|
|
|
"left"
|
|
|
|
);
|
2017-01-19 02:31:46 +03:00
|
|
|
} else {
|
2017-01-19 01:01:17 +03:00
|
|
|
parts.push(path.call(print, "left"));
|
|
|
|
}
|
|
|
|
|
2017-02-15 17:41:06 +03:00
|
|
|
const right = concat([
|
|
|
|
node.operator,
|
|
|
|
shouldInlineLogicalExpression(node) ? " " : line,
|
|
|
|
path.call(print, "right")
|
|
|
|
]);
|
2017-02-09 18:44:03 +03:00
|
|
|
|
2017-03-03 06:45:26 +03:00
|
|
|
// If there's only a single binary expression, we want to create a group
|
|
|
|
// in order to avoid having a small right part like -1 be on its own line.
|
2017-02-09 18:44:03 +03:00
|
|
|
const parent = path.getParentNode();
|
2017-03-04 02:39:37 +03:00
|
|
|
const shouldGroup = parent.type !== node.type &&
|
2017-03-03 06:45:26 +03:00
|
|
|
node.left.type !== node.type &&
|
|
|
|
node.right.type !== node.type;
|
2017-02-09 18:44:03 +03:00
|
|
|
|
|
|
|
parts.push(" ", shouldGroup ? group(right) : right);
|
2017-01-27 22:03:44 +03:00
|
|
|
|
|
|
|
// The root comments are already printed, but we need to manually print
|
|
|
|
// the other ones since we don't call the normal print on BinaryExpression,
|
|
|
|
// only for the left and right parts
|
|
|
|
if (isNested && node.comments) {
|
2017-01-28 18:50:22 +03:00
|
|
|
parts.push(comments.printComments(path, p => "", options));
|
2017-01-27 22:03:44 +03:00
|
|
|
}
|
2017-01-19 02:31:46 +03:00
|
|
|
} else {
|
2017-01-19 01:01:17 +03:00
|
|
|
// Our stopping case. Simply print the node normally.
|
|
|
|
parts.push(path.call(print));
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:10:14 +03:00
|
|
|
function adjustClause(clause, options, forceSpace) {
|
2017-01-16 20:54:39 +03:00
|
|
|
if (clause === "") {
|
|
|
|
return ";";
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:10:14 +03:00
|
|
|
if (isCurlyBracket(clause) || forceSpace) {
|
2017-01-28 18:50:22 +03:00
|
|
|
return concat([" ", clause]);
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-28 18:50:22 +03:00
|
|
|
return indent(options.tabWidth, concat([line, clause]));
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-05 20:10:14 +03:00
|
|
|
function isCurlyBracket(doc) {
|
|
|
|
const str = getFirstString(doc);
|
|
|
|
return str === "{" || str === "{}";
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEmptyBlock(doc) {
|
|
|
|
const str = getFirstString(doc);
|
|
|
|
return str === "{}";
|
|
|
|
}
|
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
function lastNonSpaceCharacter(lines) {
|
|
|
|
var pos = lines.lastPos();
|
|
|
|
do {
|
|
|
|
var ch = lines.charAt(pos);
|
2016-12-31 22:38:58 +03:00
|
|
|
|
2017-01-19 02:31:46 +03:00
|
|
|
if (/\S/.test(ch)) return ch;
|
2016-12-23 21:38:10 +03:00
|
|
|
} while (lines.prevPos(pos));
|
|
|
|
}
|
|
|
|
|
2017-01-18 00:47:20 +03:00
|
|
|
function nodeStr(node, options) {
|
|
|
|
const str = node.value;
|
2016-12-23 21:38:10 +03:00
|
|
|
isString.assert(str);
|
2016-12-31 07:10:22 +03:00
|
|
|
|
2017-01-21 01:47:52 +03:00
|
|
|
// Workaround a bug in the Javascript version of the flow parser where
|
|
|
|
// astral unicode characters like \uD801\uDC28 are incorrectly parsed as
|
|
|
|
// a sequence of \uFFFD.
|
2017-02-03 19:50:51 +03:00
|
|
|
if (options.parser === "flow" && str.indexOf("\ufffd") !== -1) {
|
2017-01-21 01:47:52 +03:00
|
|
|
return node.raw;
|
|
|
|
}
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
const raw = node.extra ? node.extra.raw : node.raw;
|
|
|
|
// `rawContent` is the string exactly like it appeared in the input source
|
|
|
|
// code, with its enclosing quote.
|
|
|
|
const rawContent = raw.slice(1, -1);
|
2017-01-12 07:36:47 +03:00
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
const double = { quote: '"', regex: /"/g };
|
|
|
|
const single = { quote: "'", regex: /'/g };
|
2017-01-12 07:36:47 +03:00
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
const preferred = options.singleQuote ? single : double;
|
|
|
|
const alternate = preferred === single ? double : single;
|
|
|
|
|
|
|
|
let shouldUseAlternateQuote = false;
|
2017-01-18 00:47:20 +03:00
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
// If `rawContent` contains at least one of the quote preferred for enclosing
|
|
|
|
// the string, we might want to enclose with the alternate quote instead, to
|
|
|
|
// minimize the number of escaped quotes.
|
|
|
|
if (rawContent.includes(preferred.quote)) {
|
|
|
|
const numPreferredQuotes = (rawContent.match(preferred.regex) || []).length;
|
|
|
|
const numAlternateQuotes = (rawContent.match(alternate.regex) || []).length;
|
|
|
|
|
|
|
|
shouldUseAlternateQuote = numPreferredQuotes > numAlternateQuotes;
|
2017-01-18 00:47:20 +03:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
const enclosingQuote = shouldUseAlternateQuote
|
|
|
|
? alternate.quote
|
|
|
|
: preferred.quote;
|
|
|
|
|
|
|
|
// It might sound unnecessary to use `makeString` even if `node.raw` already
|
|
|
|
// is enclosed with `enclosingQuote`, but it isn't. `node.raw` could contain
|
|
|
|
// unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
|
|
|
|
// sure that we consistently output the minimum amount of escaped quotes.
|
|
|
|
return makeString(rawContent, enclosingQuote);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeString(rawContent, enclosingQuote) {
|
|
|
|
const otherQuote = enclosingQuote === '"' ? "'" : '"';
|
|
|
|
|
|
|
|
// Matches _any_ escape and unescaped quotes (both single and double).
|
|
|
|
const regex = /\\([\s\S])|(['"])/g;
|
|
|
|
|
|
|
|
// Escape and unescape single and double quotes as needed to be able to
|
|
|
|
// enclose `rawContent` with `enclosingQuote`.
|
|
|
|
const newContent = rawContent.replace(regex, (match, escaped, quote) => {
|
|
|
|
// If we matched an escape, and the escaped character is a quote of the
|
|
|
|
// other type than we intend to enclose the string with, there's no need for
|
|
|
|
// it to be escaped, so return it _without_ the backslash.
|
|
|
|
if (escaped === otherQuote) {
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we matched an unescaped quote and it is of the _same_ type as we
|
|
|
|
// intend to enclose the string with, it must be escaped, so return it with
|
|
|
|
// a backslash.
|
|
|
|
if (quote === enclosingQuote) {
|
|
|
|
return "\\" + quote;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise return the escape or unescaped quote as-is.
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
|
2017-02-02 20:54:10 +03:00
|
|
|
return enclosingQuote + newContent + enclosingQuote;
|
2016-12-23 21:38:10 +03:00
|
|
|
}
|
2016-12-31 07:01:07 +03:00
|
|
|
|
2017-01-30 20:36:23 +03:00
|
|
|
function printNumber(rawNumber) {
|
2017-02-03 19:50:51 +03:00
|
|
|
return rawNumber
|
|
|
|
.toLowerCase()
|
2017-01-30 20:36:23 +03:00
|
|
|
// Remove unnecessary plus and zeroes from scientific notation.
|
2017-03-06 14:44:05 +03:00
|
|
|
.replace(/^([\d.]+e)(?:\+|(-))?0*(\d)/, "$1$2$3")
|
|
|
|
// Remove unnecessary scientific notation (1e0).
|
|
|
|
.replace(/^([\d.]+)e[+-]?0+$/, "$1")
|
2017-01-30 20:36:23 +03:00
|
|
|
// Make sure numbers always start with a digit.
|
|
|
|
.replace(/^\./, "0.")
|
|
|
|
// Remove trailing dot.
|
|
|
|
.replace(/\.(?=e|$)/, "");
|
|
|
|
}
|
|
|
|
|
2016-12-31 07:01:07 +03:00
|
|
|
function isFirstStatement(path) {
|
|
|
|
const parent = path.getParentNode();
|
|
|
|
const node = path.getValue();
|
|
|
|
const body = parent.body;
|
2017-01-19 02:31:46 +03:00
|
|
|
return body && body[0] === node;
|
2016-12-31 07:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function isLastStatement(path) {
|
|
|
|
const parent = path.getParentNode();
|
|
|
|
const node = path.getValue();
|
|
|
|
const body = parent.body;
|
|
|
|
return body && body[body.length - 1] === node;
|
|
|
|
}
|
2017-01-19 23:43:10 +03:00
|
|
|
|
2017-02-28 18:00:28 +03:00
|
|
|
function hasLeadingOwnLineComment(text, node) {
|
|
|
|
const res = node.comments &&
|
2017-03-01 20:37:02 +03:00
|
|
|
node.comments.some(
|
|
|
|
comment =>
|
|
|
|
comment.leading &&
|
|
|
|
util.hasNewline(text, util.locStart(comment), { backwards: true }) &&
|
|
|
|
util.hasNewline(text, util.locEnd(comment))
|
2017-02-28 18:00:28 +03:00
|
|
|
);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-09 04:06:18 +03:00
|
|
|
// This recurses the return argument, looking for the first token
|
|
|
|
// (the leftmost leaf node) and, if it (or its parents) has any
|
|
|
|
// leadingComments, returns true (so it can be wrapped in parens).
|
|
|
|
function returnArgumentHasLeadingComment(options, argument) {
|
|
|
|
if (hasLeadingOwnLineComment(options.originalText, argument)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasCommentableLeftSide = argument.type === "BinaryExpression" ||
|
|
|
|
argument.type === "LogicalExpression" ||
|
|
|
|
argument.type === "ConditionalExpression" ||
|
|
|
|
argument.type === "CallExpression" ||
|
|
|
|
argument.type === "MemberExpression" ||
|
|
|
|
argument.type === "SequenceExpression" ||
|
|
|
|
argument.type === "TaggedTemplateExpression";
|
|
|
|
|
|
|
|
if (hasCommentableLeftSide) {
|
|
|
|
const getLeftSide = (node) => {
|
|
|
|
if (node.expressions) {
|
|
|
|
return node.expressions[0];
|
|
|
|
}
|
|
|
|
return node.left || node.test || node.callee || node.object || node.tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
let leftMost = argument;
|
|
|
|
let newLeftMost;
|
|
|
|
while (newLeftMost = getLeftSide(leftMost)) {
|
|
|
|
leftMost = newLeftMost;
|
|
|
|
|
|
|
|
if (hasLeadingOwnLineComment(options.originalText, leftMost)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-05 05:23:37 +03:00
|
|
|
// Hack to differentiate between the following two which have the same ast
|
|
|
|
// type T = { method: () => void };
|
|
|
|
// type T = { method(): void };
|
|
|
|
function isObjectTypePropertyAFunction(node) {
|
|
|
|
return node.type === "ObjectTypeProperty" &&
|
|
|
|
node.value.type === "FunctionTypeAnnotation" &&
|
|
|
|
!node.static &&
|
|
|
|
util.locStart(node.key) !== util.locStart(node.value);
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:17:20 +03:00
|
|
|
function isFlowNodeStartingWithDeclare(node, options) {
|
|
|
|
if (options.parser !== "flow") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options.originalText
|
|
|
|
.slice(0, util.locStart(node))
|
|
|
|
.match(/declare\s*$/);
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:35:22 +03:00
|
|
|
function printArrayItems(path, options, printPath, print) {
|
|
|
|
const printedElements = [];
|
|
|
|
let separatorParts = [];
|
|
|
|
|
|
|
|
path.each(
|
|
|
|
function(childPath) {
|
|
|
|
printedElements.push(concat(separatorParts));
|
|
|
|
printedElements.push(group(print(childPath)));
|
|
|
|
|
|
|
|
separatorParts = [",", line];
|
|
|
|
if (
|
|
|
|
childPath.getValue() &&
|
|
|
|
util.isNextLineEmpty(options.originalText, childPath.getValue())
|
|
|
|
) {
|
|
|
|
separatorParts.push(softline);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
printPath
|
|
|
|
);
|
|
|
|
|
|
|
|
return concat(printedElements);
|
|
|
|
}
|
|
|
|
|
2017-01-19 23:43:10 +03:00
|
|
|
function printAstToDoc(ast, options) {
|
|
|
|
function printGenerically(path) {
|
|
|
|
return comments.printComments(
|
|
|
|
path,
|
|
|
|
p => genericPrint(p, options, printGenerically),
|
|
|
|
options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:47:11 +03:00
|
|
|
const doc = printGenerically(FastPath.from(ast));
|
|
|
|
docUtils.propagateBreaks(doc);
|
|
|
|
return doc;
|
2017-01-19 23:43:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-20 21:12:37 +03:00
|
|
|
module.exports = { printAstToDoc };
|