Format the codebase using the pre-1.0 release (#1194)

master
Christopher Chedeau 2017-04-12 10:16:11 -07:00 committed by GitHub
parent 9a5b447c7f
commit 8e1583fd16
9 changed files with 562 additions and 557 deletions

View File

@ -221,7 +221,7 @@ if (stdin) {
let input; let input;
try { try {
input = fs.readFileSync(filename, "utf8"); input = fs.readFileSync(filename, "utf8");
} catch(e) { } catch (e) {
// Add newline to split errors from filename line. // Add newline to split errors from filename line.
process.stdout.write("\n"); process.stdout.write("\n");

View File

@ -20,9 +20,9 @@ function guessLineEnding(text) {
function parse(text, opts) { function parse(text, opts) {
let parseFunction; let parseFunction;
if (opts.parser === 'flow') { if (opts.parser === "flow") {
parseFunction = parser.parseWithFlow; parseFunction = parser.parseWithFlow;
} else if (opts.parser === 'typescript') { } else if (opts.parser === "typescript") {
parseFunction = parser.parseWithTypeScript; parseFunction = parser.parseWithTypeScript;
} else { } else {
parseFunction = parser.parseWithBabylon; parseFunction = parser.parseWithBabylon;

View File

@ -19,7 +19,8 @@ var comparePos = util.comparePos;
var childNodesCacheKey = Symbol("child-nodes"); var childNodesCacheKey = Symbol("child-nodes");
var locStart = util.locStart; var locStart = util.locStart;
var locEnd = util.locEnd; var locEnd = util.locEnd;
var getNextNonSpaceNonCommentCharacter = util.getNextNonSpaceNonCommentCharacter; var getNextNonSpaceNonCommentCharacter =
util.getNextNonSpaceNonCommentCharacter;
// TODO Move a non-caching implementation of this function into ast-types, // TODO Move a non-caching implementation of this function into ast-types,
// and implement a caching wrapper function here. // and implement a caching wrapper function here.
@ -38,8 +39,10 @@ function getSortedChildNodes(node, text, resultArray) {
// time because we almost always (maybe always?) append the // time because we almost always (maybe always?) append the
// nodes in order anyway. // nodes in order anyway.
for (var i = resultArray.length - 1; i >= 0; --i) { for (var i = resultArray.length - 1; i >= 0; --i) {
if (locStart(resultArray[i]) <= locStart(node) && if (
locEnd(resultArray[i]) <= locEnd(node)) { locStart(resultArray[i]) <= locStart(node) &&
locEnd(resultArray[i]) <= locEnd(node)
) {
break; break;
} }
} }
@ -82,7 +85,7 @@ function decorateComment(node, comment, text) {
// Time to dust off the old binary search robes and wizard hat. // Time to dust off the old binary search robes and wizard hat.
var left = 0, right = childNodes.length; var left = 0, right = childNodes.length;
while (left < right) { while (left < right) {
var middle = left + right >> 1; var middle = (left + right) >> 1;
var child = childNodes[middle]; var child = childNodes[middle];
if ( if (
@ -155,7 +158,12 @@ function attach(comments, ast, text, options) {
comment comment
) || ) ||
handleMemberExpressionComments(enclosingNode, followingNode, comment) || handleMemberExpressionComments(enclosingNode, followingNode, comment) ||
handleIfStatementComments(text, enclosingNode, followingNode, comment) || handleIfStatementComments(
text,
enclosingNode,
followingNode,
comment
) ||
handleTryStatementComments(enclosingNode, followingNode, comment) || handleTryStatementComments(enclosingNode, followingNode, comment) ||
handleClassComments(enclosingNode, comment) || handleClassComments(enclosingNode, comment) ||
handleImportSpecifierComments(enclosingNode, comment) || handleImportSpecifierComments(enclosingNode, comment) ||
@ -168,7 +176,11 @@ function attach(comments, ast, text, options) {
comment comment
) || ) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) || handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
handleImportDeclarationComments(enclosingNode, precedingNode, comment) || handleImportDeclarationComments(
enclosingNode,
precedingNode,
comment
) ||
handleAssignmentPatternComments(enclosingNode, comment) handleAssignmentPatternComments(enclosingNode, comment)
) { ) {
// We're good // We're good
@ -194,7 +206,12 @@ function attach(comments, ast, text, options) {
) || ) ||
handleImportSpecifierComments(enclosingNode, comment) || handleImportSpecifierComments(enclosingNode, comment) ||
handleTemplateLiteralComments(enclosingNode, comment) || handleTemplateLiteralComments(enclosingNode, comment) ||
handleIfStatementComments(text, enclosingNode, followingNode, comment) || handleIfStatementComments(
text,
enclosingNode,
followingNode,
comment
) ||
handleClassComments(enclosingNode, comment) || handleClassComments(enclosingNode, comment) ||
handleLabeledStatementComments(enclosingNode, comment) || handleLabeledStatementComments(enclosingNode, comment) ||
handleCallExpressionComments(precedingNode, enclosingNode, comment) || handleCallExpressionComments(precedingNode, enclosingNode, comment) ||
@ -219,7 +236,12 @@ function attach(comments, ast, text, options) {
} }
} else { } else {
if ( if (
handleIfStatementComments(text, enclosingNode, followingNode, comment) || handleIfStatementComments(
text,
enclosingNode,
followingNode,
comment
) ||
handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) || handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) ||
handleTemplateLiteralComments(enclosingNode, comment) || handleTemplateLiteralComments(enclosingNode, comment) ||
handleCommentInEmptyParens(enclosingNode, comment) || handleCommentInEmptyParens(enclosingNode, comment) ||
@ -365,11 +387,14 @@ function addBlockOrNotComment(node, comment) {
// // comment // // comment
// ... // ...
// } // }
function handleIfStatementComments(text, enclosingNode, followingNode, comment) { function handleIfStatementComments(
text,
enclosingNode,
followingNode,
comment
) {
if ( if (
!enclosingNode || !enclosingNode || enclosingNode.type !== "IfStatement" || !followingNode
enclosingNode.type !== "IfStatement" ||
!followingNode
) { ) {
return false; return false;
} }
@ -444,7 +469,8 @@ function handleConditionalExpressionComments(
comment, comment,
text text
) { ) {
const isSameLineAsPrecedingNode = precedingNode && const isSameLineAsPrecedingNode =
precedingNode &&
!util.hasNewlineInRange(text, locEnd(precedingNode), locStart(comment)); !util.hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
if ( if (
@ -498,16 +524,18 @@ function handleCommentInEmptyParens(enclosingNode, comment) {
enclosingNode.type === "ArrowFunctionExpression" || enclosingNode.type === "ArrowFunctionExpression" ||
enclosingNode.type === "ClassMethod" || enclosingNode.type === "ClassMethod" ||
enclosingNode.type === "ObjectMethod") && enclosingNode.type === "ObjectMethod") &&
enclosingNode.params.length === 0) || enclosingNode.params.length === 0) ||
(enclosingNode.type === "CallExpression" && (enclosingNode.type === "CallExpression" &&
enclosingNode.arguments.length === 0)) enclosingNode.arguments.length === 0))
) { ) {
addDanglingComment(enclosingNode, comment); addDanglingComment(enclosingNode, comment);
return true; return true;
} }
if (enclosingNode && if (
enclosingNode &&
(enclosingNode.type === "MethodDefinition" && (enclosingNode.type === "MethodDefinition" &&
enclosingNode.value.params.length === 0)) { enclosingNode.value.params.length === 0)
) {
addDanglingComment(enclosingNode.value, comment); addDanglingComment(enclosingNode.value, comment);
return true; return true;
} }
@ -607,10 +635,7 @@ function handleUnionTypeComments(
followingNode, followingNode,
comment comment
) { ) {
if ( if (enclosingNode && enclosingNode.type === "UnionTypeAnnotation") {
enclosingNode &&
enclosingNode.type === "UnionTypeAnnotation"
) {
addTrailingComment(precedingNode, comment); addTrailingComment(precedingNode, comment);
return true; return true;
} }
@ -619,10 +644,9 @@ function handleUnionTypeComments(
function handlePropertyComments(enclosingNode, comment) { function handlePropertyComments(enclosingNode, comment) {
if ( if (
enclosingNode && ( enclosingNode &&
enclosingNode.type === "Property" || (enclosingNode.type === "Property" ||
enclosingNode.type === "ObjectProperty" enclosingNode.type === "ObjectProperty")
)
) { ) {
addLeadingComment(enclosingNode, comment); addLeadingComment(enclosingNode, comment);
return true; return true;
@ -648,8 +672,10 @@ function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
} }
return true; return true;
} else if ( } else if (
enclosingNode && enclosingNode.type === 'Program' && enclosingNode &&
enclosingNode.body.length === 0 && enclosingNode.directives && enclosingNode.type === "Program" &&
enclosingNode.body.length === 0 &&
enclosingNode.directives &&
enclosingNode.directives.length === 0 enclosingNode.directives.length === 0
) { ) {
if (isLastComment) { if (isLastComment) {
@ -663,9 +689,10 @@ function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
} }
function handleForComments(enclosingNode, precedingNode, comment) { function handleForComments(enclosingNode, precedingNode, comment) {
if (enclosingNode && ( if (
enclosingNode.type === "ForInStatement" || enclosingNode &&
enclosingNode.type === "ForOfStatement") (enclosingNode.type === "ForInStatement" ||
enclosingNode.type === "ForOfStatement")
) { ) {
addLeadingComment(enclosingNode, comment); addLeadingComment(enclosingNode, comment);
return true; return true;
@ -673,11 +700,17 @@ function handleForComments(enclosingNode, precedingNode, comment) {
return false; return false;
} }
function handleImportDeclarationComments(enclosingNode, precedingNode, comment) { function handleImportDeclarationComments(
enclosingNode,
precedingNode,
comment
) {
if ( if (
precedingNode && precedingNode &&
enclosingNode && enclosingNode.type === "ImportDeclaration" && enclosingNode &&
comment.type !== "CommentBlock" && comment.type !== "Block" enclosingNode.type === "ImportDeclaration" &&
comment.type !== "CommentBlock" &&
comment.type !== "Block"
) { ) {
addTrailingComment(precedingNode, comment); addTrailingComment(precedingNode, comment);
return true; return true;
@ -701,12 +734,16 @@ function handleClassMethodComments(enclosingNode, comment) {
return false; return false;
} }
function handleVariableDeclaratorComments(enclosingNode, followingNode, comment) { function handleVariableDeclaratorComments(
enclosingNode,
followingNode,
comment
) {
if ( if (
enclosingNode && enclosingNode &&
enclosingNode.type === "VariableDeclarator" && enclosingNode.type === "VariableDeclarator" &&
followingNode && ( followingNode &&
followingNode.type === "ObjectExpression" || (followingNode.type === "ObjectExpression" ||
followingNode.type === "ArrayExpression") followingNode.type === "ArrayExpression")
) { ) {
addLeadingComment(followingNode, comment); addLeadingComment(followingNode, comment);
@ -825,15 +862,12 @@ function printDanglingComments(path, options, sameIndent) {
return ""; return "";
} }
path.each( path.each(commentPath => {
commentPath => { const comment = commentPath.getValue();
const comment = commentPath.getValue(); if (!comment.leading && !comment.trailing) {
if (!comment.leading && !comment.trailing) { parts.push(printComment(commentPath));
parts.push(printComment(commentPath)); }
} }, "comments");
},
"comments"
);
if (parts.length === 0) { if (parts.length === 0) {
return ""; return "";
@ -858,29 +892,24 @@ function printComments(path, print, options) {
var leadingParts = []; var leadingParts = [];
var trailingParts = [printed]; var trailingParts = [printed];
path.each( path.each(function(commentPath) {
function(commentPath) { var comment = commentPath.getValue();
var comment = commentPath.getValue(); var leading = types.getFieldValue(comment, "leading");
var leading = types.getFieldValue(comment, "leading"); var trailing = types.getFieldValue(comment, "trailing");
var trailing = types.getFieldValue(comment, "trailing");
if (leading) { if (leading) {
leadingParts.push(printLeadingComment(commentPath, print, options)); leadingParts.push(printLeadingComment(commentPath, print, options));
const text = options.originalText; const text = options.originalText;
if ( if (util.hasNewline(text, util.skipNewline(text, util.locEnd(comment)))) {
util.hasNewline(text, util.skipNewline(text, util.locEnd(comment))) leadingParts.push(hardline);
) {
leadingParts.push(hardline);
}
} else if (trailing) {
trailingParts.push(
printTrailingComment(commentPath, print, options, parent)
);
} }
}, } else if (trailing) {
"comments" trailingParts.push(
); printTrailingComment(commentPath, print, options, parent)
);
}
}, "comments");
return concat(leadingParts.concat(trailingParts)); return concat(leadingParts.concat(trailingParts));
} }

View File

@ -75,24 +75,30 @@ function printDoc(doc) {
} }
if (doc.type === "if-break") { if (doc.type === "if-break") {
return "ifBreak(" + return (
"ifBreak(" +
printDoc(doc.breakContents) + printDoc(doc.breakContents) +
(doc.flatContents ? ", " + printDoc(doc.flatContents) : "") + (doc.flatContents ? ", " + printDoc(doc.flatContents) : "") +
")"; ")"
);
} }
if (doc.type === "group") { if (doc.type === "group") {
if (doc.expandedStates) { if (doc.expandedStates) {
return "conditionalGroup(" + return (
"conditionalGroup(" +
"[" + "[" +
doc.expandedStates.map(printDoc).join(",") + doc.expandedStates.map(printDoc).join(",") +
"])"; "])"
);
} }
return (doc.break ? "wrappedGroup" : "group") + return (
(doc.break ? "wrappedGroup" : "group") +
"(" + "(" +
printDoc(doc.contents) + printDoc(doc.contents) +
")"; ")"
);
} }
if (doc.type === "line-suffix") { if (doc.type === "line-suffix") {

View File

@ -177,9 +177,8 @@ function printDocToString(doc, options) {
// group has these, we need to manually go through // group has these, we need to manually go through
// these states and find the first one that fits. // these states and find the first one that fits.
if (doc.expandedStates) { if (doc.expandedStates) {
const mostExpanded = doc.expandedStates[ const mostExpanded =
doc.expandedStates.length - 1 doc.expandedStates[doc.expandedStates.length - 1];
];
if (doc.break) { if (doc.break) {
cmds.push([ind, MODE_BREAK, mostExpanded]); cmds.push([ind, MODE_BREAK, mostExpanded]);

View File

@ -235,8 +235,11 @@ FPp.needsParens = function(assumeExpressionContext) {
} }
if ( if (
parent.type === "ArrowFunctionExpression" && parent.body === node && startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ false) (parent.type === "ArrowFunctionExpression" &&
|| parent.type === "ExpressionStatement" && startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ true) parent.body === node &&
startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ false)) ||
(parent.type === "ExpressionStatement" &&
startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ true))
) { ) {
return true; return true;
} }
@ -250,22 +253,28 @@ FPp.needsParens = function(assumeExpressionContext) {
case "SpreadElement": case "SpreadElement":
case "SpreadProperty": case "SpreadProperty":
return parent.type === "MemberExpression" && return (
parent.type === "MemberExpression" &&
name === "object" && name === "object" &&
parent.object === node; parent.object === node
);
case "UpdateExpression": case "UpdateExpression":
if (parent.type === "UnaryExpression") { if (parent.type === "UnaryExpression") {
return node.prefix && return (
node.prefix &&
((node.operator === "++" && parent.operator === "+") || ((node.operator === "++" && parent.operator === "+") ||
(node.operator === "--" && parent.operator === "-")); (node.operator === "--" && parent.operator === "-"))
);
} }
// else fall through // else fall through
case "UnaryExpression": case "UnaryExpression":
switch (parent.type) { switch (parent.type) {
case "UnaryExpression": case "UnaryExpression":
return node.operator === parent.operator && return (
(node.operator === "+" || node.operator === "-"); node.operator === parent.operator &&
(node.operator === "+" || node.operator === "-")
);
case "MemberExpression": case "MemberExpression":
return name === "object" && parent.object === node; return name === "object" && parent.object === node;
@ -302,7 +311,7 @@ FPp.needsParens = function(assumeExpressionContext) {
if (node.operator === "in" && isLeftOfAForStatement(node)) { if (node.operator === "in" && isLeftOfAForStatement(node)) {
return true; return true;
} }
// else fall through // else fall through
case "LogicalExpression": case "LogicalExpression":
switch (parent.type) { switch (parent.type) {
case "CallExpression": case "CallExpression":
@ -377,7 +386,7 @@ FPp.needsParens = function(assumeExpressionContext) {
if (parent.type === "UnaryExpression") { if (parent.type === "UnaryExpression") {
return true; return true;
} }
// else fall through // else fall through
case "AwaitExpression": case "AwaitExpression":
switch (parent.type) { switch (parent.type) {
case "TaggedTemplateExpression": case "TaggedTemplateExpression":
@ -404,24 +413,30 @@ FPp.needsParens = function(assumeExpressionContext) {
case "IntersectionTypeAnnotation": case "IntersectionTypeAnnotation":
case "UnionTypeAnnotation": case "UnionTypeAnnotation":
return parent.type === "ArrayTypeAnnotation" || return (
parent.type === "ArrayTypeAnnotation" ||
parent.type === "NullableTypeAnnotation" || parent.type === "NullableTypeAnnotation" ||
parent.type === "IntersectionTypeAnnotation" || parent.type === "IntersectionTypeAnnotation" ||
parent.type === "UnionTypeAnnotation"; parent.type === "UnionTypeAnnotation"
);
case "NullableTypeAnnotation": case "NullableTypeAnnotation":
return parent.type === "ArrayTypeAnnotation"; return parent.type === "ArrayTypeAnnotation";
case "FunctionTypeAnnotation": case "FunctionTypeAnnotation":
return parent.type === "UnionTypeAnnotation" || return (
parent.type === "IntersectionTypeAnnotation"; parent.type === "UnionTypeAnnotation" ||
parent.type === "IntersectionTypeAnnotation"
);
case "NumericLiteral": case "NumericLiteral":
case "Literal": case "Literal":
return parent.type === "MemberExpression" && return (
parent.type === "MemberExpression" &&
isNumber.check(node.value) && isNumber.check(node.value) &&
name === "object" && name === "object" &&
parent.object === node; parent.object === node
);
case "AssignmentExpression": case "AssignmentExpression":
if (parent.type === "ArrowFunctionExpression" && parent.body === node) { if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
@ -570,11 +585,20 @@ function startsWithNoLookaheadToken(node, forbidFunctionAndClass) {
case "ConditionalExpression": case "ConditionalExpression":
return startsWithNoLookaheadToken(node.test, forbidFunctionAndClass); return startsWithNoLookaheadToken(node.test, forbidFunctionAndClass);
case "UpdateExpression": case "UpdateExpression":
return !node.prefix && startsWithNoLookaheadToken(node.argument, forbidFunctionAndClass); return (
!node.prefix &&
startsWithNoLookaheadToken(node.argument, forbidFunctionAndClass)
);
case "BindExpression": case "BindExpression":
return node.object && startsWithNoLookaheadToken(node.object, forbidFunctionAndClass); return (
node.object &&
startsWithNoLookaheadToken(node.object, forbidFunctionAndClass)
);
case "SequenceExpression": case "SequenceExpression":
return startsWithNoLookaheadToken(node.expressions[0], forbidFunctionAndClass) return startsWithNoLookaheadToken(
node.expressions[0],
forbidFunctionAndClass
);
default: default:
return false; return false;
} }

View File

@ -16,12 +16,8 @@ function parseWithFlow(text) {
line: ast.errors[0].loc.start.line, line: ast.errors[0].loc.start.line,
column: ast.errors[0].loc.start.column column: ast.errors[0].loc.start.column
}; };
const msg = ast.errors[0].message + const msg =
" (" + ast.errors[0].message + " (" + loc.line + ":" + loc.column + ")";
loc.line +
":" +
loc.column +
")";
const error = new SyntaxError(msg); const error = new SyntaxError(msg);
error.loc = loc; error.loc = loc;
throw error; throw error;
@ -58,16 +54,16 @@ function parseWithTypeScript(text) {
// While we are working on typescript, we are putting it in devDependencies // While we are working on typescript, we are putting it in devDependencies
// so it shouldn't be picked up by static analysis // so it shouldn't be picked up by static analysis
const r = require; const r = require;
const parser = r('typescript-eslint-parser') const parser = r("typescript-eslint-parser");
return parser.parse(text, { return parser.parse(text, {
loc: true, loc: true,
range: true, range: true,
tokens: true, tokens: true,
attachComment: true, attachComment: true,
ecmaFeatures: { ecmaFeatures: {
jsx: true, jsx: true
} }
}) });
} }
module.exports = { parseWithFlow, parseWithBabylon, parseWithTypeScript }; module.exports = { parseWithFlow, parseWithBabylon, parseWithTypeScript };

File diff suppressed because it is too large Load Diff

View File

@ -170,7 +170,12 @@ function skipNewline(text, index, opts) {
const atIndex = text.charAt(index); const atIndex = text.charAt(index);
if (backwards) { if (backwards) {
if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") { if (
atIndex === "\n" ||
atIndex === "\r" ||
atIndex === "\u2028" ||
atIndex === "\u2029"
) {
return index - 1; return index - 1;
} }
if (text.charAt(index - 1) === "\r" && atIndex === "\n") { if (text.charAt(index - 1) === "\r" && atIndex === "\n") {
@ -180,7 +185,12 @@ function skipNewline(text, index, opts) {
if (atIndex === "\r" && text.charAt(index + 1) === "\n") { if (atIndex === "\r" && text.charAt(index + 1) === "\n") {
return index + 2; return index + 2;
} }
if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") { if (
atIndex === "\n" ||
atIndex === "\r" ||
atIndex === "\u2028" ||
atIndex === "\u2029"
) {
return index + 1; return index + 1;
} }
} }