Format the codebase using the pre-1.0 release (#1194)
parent
9a5b447c7f
commit
8e1583fd16
|
@ -221,7 +221,7 @@ if (stdin) {
|
|||
let input;
|
||||
try {
|
||||
input = fs.readFileSync(filename, "utf8");
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
// Add newline to split errors from filename line.
|
||||
process.stdout.write("\n");
|
||||
|
||||
|
|
4
index.js
4
index.js
|
@ -20,9 +20,9 @@ function guessLineEnding(text) {
|
|||
function parse(text, opts) {
|
||||
let parseFunction;
|
||||
|
||||
if (opts.parser === 'flow') {
|
||||
if (opts.parser === "flow") {
|
||||
parseFunction = parser.parseWithFlow;
|
||||
} else if (opts.parser === 'typescript') {
|
||||
} else if (opts.parser === "typescript") {
|
||||
parseFunction = parser.parseWithTypeScript;
|
||||
} else {
|
||||
parseFunction = parser.parseWithBabylon;
|
||||
|
|
161
src/comments.js
161
src/comments.js
|
@ -19,7 +19,8 @@ var comparePos = util.comparePos;
|
|||
var childNodesCacheKey = Symbol("child-nodes");
|
||||
var locStart = util.locStart;
|
||||
var locEnd = util.locEnd;
|
||||
var getNextNonSpaceNonCommentCharacter = util.getNextNonSpaceNonCommentCharacter;
|
||||
var getNextNonSpaceNonCommentCharacter =
|
||||
util.getNextNonSpaceNonCommentCharacter;
|
||||
|
||||
// TODO Move a non-caching implementation of this function into ast-types,
|
||||
// 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
|
||||
// nodes in order anyway.
|
||||
for (var i = resultArray.length - 1; i >= 0; --i) {
|
||||
if (locStart(resultArray[i]) <= locStart(node) &&
|
||||
locEnd(resultArray[i]) <= locEnd(node)) {
|
||||
if (
|
||||
locStart(resultArray[i]) <= locStart(node) &&
|
||||
locEnd(resultArray[i]) <= locEnd(node)
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -82,7 +85,7 @@ function decorateComment(node, comment, text) {
|
|||
// Time to dust off the old binary search robes and wizard hat.
|
||||
var left = 0, right = childNodes.length;
|
||||
while (left < right) {
|
||||
var middle = left + right >> 1;
|
||||
var middle = (left + right) >> 1;
|
||||
var child = childNodes[middle];
|
||||
|
||||
if (
|
||||
|
@ -155,7 +158,12 @@ function attach(comments, ast, text, options) {
|
|||
comment
|
||||
) ||
|
||||
handleMemberExpressionComments(enclosingNode, followingNode, comment) ||
|
||||
handleIfStatementComments(text, enclosingNode, followingNode, comment) ||
|
||||
handleIfStatementComments(
|
||||
text,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment
|
||||
) ||
|
||||
handleTryStatementComments(enclosingNode, followingNode, comment) ||
|
||||
handleClassComments(enclosingNode, comment) ||
|
||||
handleImportSpecifierComments(enclosingNode, comment) ||
|
||||
|
@ -168,7 +176,11 @@ function attach(comments, ast, text, options) {
|
|||
comment
|
||||
) ||
|
||||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
|
||||
handleImportDeclarationComments(enclosingNode, precedingNode, comment) ||
|
||||
handleImportDeclarationComments(
|
||||
enclosingNode,
|
||||
precedingNode,
|
||||
comment
|
||||
) ||
|
||||
handleAssignmentPatternComments(enclosingNode, comment)
|
||||
) {
|
||||
// We're good
|
||||
|
@ -194,7 +206,12 @@ function attach(comments, ast, text, options) {
|
|||
) ||
|
||||
handleImportSpecifierComments(enclosingNode, comment) ||
|
||||
handleTemplateLiteralComments(enclosingNode, comment) ||
|
||||
handleIfStatementComments(text, enclosingNode, followingNode, comment) ||
|
||||
handleIfStatementComments(
|
||||
text,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment
|
||||
) ||
|
||||
handleClassComments(enclosingNode, comment) ||
|
||||
handleLabeledStatementComments(enclosingNode, comment) ||
|
||||
handleCallExpressionComments(precedingNode, enclosingNode, comment) ||
|
||||
|
@ -219,7 +236,12 @@ function attach(comments, ast, text, options) {
|
|||
}
|
||||
} else {
|
||||
if (
|
||||
handleIfStatementComments(text, enclosingNode, followingNode, comment) ||
|
||||
handleIfStatementComments(
|
||||
text,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment
|
||||
) ||
|
||||
handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) ||
|
||||
handleTemplateLiteralComments(enclosingNode, comment) ||
|
||||
handleCommentInEmptyParens(enclosingNode, comment) ||
|
||||
|
@ -365,11 +387,14 @@ function addBlockOrNotComment(node, comment) {
|
|||
// // comment
|
||||
// ...
|
||||
// }
|
||||
function handleIfStatementComments(text, enclosingNode, followingNode, comment) {
|
||||
function handleIfStatementComments(
|
||||
text,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment
|
||||
) {
|
||||
if (
|
||||
!enclosingNode ||
|
||||
enclosingNode.type !== "IfStatement" ||
|
||||
!followingNode
|
||||
!enclosingNode || enclosingNode.type !== "IfStatement" || !followingNode
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
@ -444,7 +469,8 @@ function handleConditionalExpressionComments(
|
|||
comment,
|
||||
text
|
||||
) {
|
||||
const isSameLineAsPrecedingNode = precedingNode &&
|
||||
const isSameLineAsPrecedingNode =
|
||||
precedingNode &&
|
||||
!util.hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
|
||||
|
||||
if (
|
||||
|
@ -498,16 +524,18 @@ function handleCommentInEmptyParens(enclosingNode, comment) {
|
|||
enclosingNode.type === "ArrowFunctionExpression" ||
|
||||
enclosingNode.type === "ClassMethod" ||
|
||||
enclosingNode.type === "ObjectMethod") &&
|
||||
enclosingNode.params.length === 0) ||
|
||||
(enclosingNode.type === "CallExpression" &&
|
||||
enclosingNode.arguments.length === 0))
|
||||
enclosingNode.params.length === 0) ||
|
||||
(enclosingNode.type === "CallExpression" &&
|
||||
enclosingNode.arguments.length === 0))
|
||||
) {
|
||||
addDanglingComment(enclosingNode, comment);
|
||||
return true;
|
||||
}
|
||||
if (enclosingNode &&
|
||||
if (
|
||||
enclosingNode &&
|
||||
(enclosingNode.type === "MethodDefinition" &&
|
||||
enclosingNode.value.params.length === 0)) {
|
||||
enclosingNode.value.params.length === 0)
|
||||
) {
|
||||
addDanglingComment(enclosingNode.value, comment);
|
||||
return true;
|
||||
}
|
||||
|
@ -607,10 +635,7 @@ function handleUnionTypeComments(
|
|||
followingNode,
|
||||
comment
|
||||
) {
|
||||
if (
|
||||
enclosingNode &&
|
||||
enclosingNode.type === "UnionTypeAnnotation"
|
||||
) {
|
||||
if (enclosingNode && enclosingNode.type === "UnionTypeAnnotation") {
|
||||
addTrailingComment(precedingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
@ -619,10 +644,9 @@ function handleUnionTypeComments(
|
|||
|
||||
function handlePropertyComments(enclosingNode, comment) {
|
||||
if (
|
||||
enclosingNode && (
|
||||
enclosingNode.type === "Property" ||
|
||||
enclosingNode.type === "ObjectProperty"
|
||||
)
|
||||
enclosingNode &&
|
||||
(enclosingNode.type === "Property" ||
|
||||
enclosingNode.type === "ObjectProperty")
|
||||
) {
|
||||
addLeadingComment(enclosingNode, comment);
|
||||
return true;
|
||||
|
@ -648,8 +672,10 @@ function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
|
|||
}
|
||||
return true;
|
||||
} else if (
|
||||
enclosingNode && enclosingNode.type === 'Program' &&
|
||||
enclosingNode.body.length === 0 && enclosingNode.directives &&
|
||||
enclosingNode &&
|
||||
enclosingNode.type === "Program" &&
|
||||
enclosingNode.body.length === 0 &&
|
||||
enclosingNode.directives &&
|
||||
enclosingNode.directives.length === 0
|
||||
) {
|
||||
if (isLastComment) {
|
||||
|
@ -663,9 +689,10 @@ function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
|
|||
}
|
||||
|
||||
function handleForComments(enclosingNode, precedingNode, comment) {
|
||||
if (enclosingNode && (
|
||||
enclosingNode.type === "ForInStatement" ||
|
||||
enclosingNode.type === "ForOfStatement")
|
||||
if (
|
||||
enclosingNode &&
|
||||
(enclosingNode.type === "ForInStatement" ||
|
||||
enclosingNode.type === "ForOfStatement")
|
||||
) {
|
||||
addLeadingComment(enclosingNode, comment);
|
||||
return true;
|
||||
|
@ -673,11 +700,17 @@ function handleForComments(enclosingNode, precedingNode, comment) {
|
|||
return false;
|
||||
}
|
||||
|
||||
function handleImportDeclarationComments(enclosingNode, precedingNode, comment) {
|
||||
function handleImportDeclarationComments(
|
||||
enclosingNode,
|
||||
precedingNode,
|
||||
comment
|
||||
) {
|
||||
if (
|
||||
precedingNode &&
|
||||
enclosingNode && enclosingNode.type === "ImportDeclaration" &&
|
||||
comment.type !== "CommentBlock" && comment.type !== "Block"
|
||||
enclosingNode &&
|
||||
enclosingNode.type === "ImportDeclaration" &&
|
||||
comment.type !== "CommentBlock" &&
|
||||
comment.type !== "Block"
|
||||
) {
|
||||
addTrailingComment(precedingNode, comment);
|
||||
return true;
|
||||
|
@ -701,12 +734,16 @@ function handleClassMethodComments(enclosingNode, comment) {
|
|||
return false;
|
||||
}
|
||||
|
||||
function handleVariableDeclaratorComments(enclosingNode, followingNode, comment) {
|
||||
function handleVariableDeclaratorComments(
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
comment
|
||||
) {
|
||||
if (
|
||||
enclosingNode &&
|
||||
enclosingNode.type === "VariableDeclarator" &&
|
||||
followingNode && (
|
||||
followingNode.type === "ObjectExpression" ||
|
||||
followingNode &&
|
||||
(followingNode.type === "ObjectExpression" ||
|
||||
followingNode.type === "ArrayExpression")
|
||||
) {
|
||||
addLeadingComment(followingNode, comment);
|
||||
|
@ -825,15 +862,12 @@ function printDanglingComments(path, options, sameIndent) {
|
|||
return "";
|
||||
}
|
||||
|
||||
path.each(
|
||||
commentPath => {
|
||||
const comment = commentPath.getValue();
|
||||
if (!comment.leading && !comment.trailing) {
|
||||
parts.push(printComment(commentPath));
|
||||
}
|
||||
},
|
||||
"comments"
|
||||
);
|
||||
path.each(commentPath => {
|
||||
const comment = commentPath.getValue();
|
||||
if (!comment.leading && !comment.trailing) {
|
||||
parts.push(printComment(commentPath));
|
||||
}
|
||||
}, "comments");
|
||||
|
||||
if (parts.length === 0) {
|
||||
return "";
|
||||
|
@ -858,29 +892,24 @@ function printComments(path, print, options) {
|
|||
var leadingParts = [];
|
||||
var trailingParts = [printed];
|
||||
|
||||
path.each(
|
||||
function(commentPath) {
|
||||
var comment = commentPath.getValue();
|
||||
var leading = types.getFieldValue(comment, "leading");
|
||||
var trailing = types.getFieldValue(comment, "trailing");
|
||||
path.each(function(commentPath) {
|
||||
var comment = commentPath.getValue();
|
||||
var leading = types.getFieldValue(comment, "leading");
|
||||
var trailing = types.getFieldValue(comment, "trailing");
|
||||
|
||||
if (leading) {
|
||||
leadingParts.push(printLeadingComment(commentPath, print, options));
|
||||
if (leading) {
|
||||
leadingParts.push(printLeadingComment(commentPath, print, options));
|
||||
|
||||
const text = options.originalText;
|
||||
if (
|
||||
util.hasNewline(text, util.skipNewline(text, util.locEnd(comment)))
|
||||
) {
|
||||
leadingParts.push(hardline);
|
||||
}
|
||||
} else if (trailing) {
|
||||
trailingParts.push(
|
||||
printTrailingComment(commentPath, print, options, parent)
|
||||
);
|
||||
const text = options.originalText;
|
||||
if (util.hasNewline(text, util.skipNewline(text, util.locEnd(comment)))) {
|
||||
leadingParts.push(hardline);
|
||||
}
|
||||
},
|
||||
"comments"
|
||||
);
|
||||
} else if (trailing) {
|
||||
trailingParts.push(
|
||||
printTrailingComment(commentPath, print, options, parent)
|
||||
);
|
||||
}
|
||||
}, "comments");
|
||||
|
||||
return concat(leadingParts.concat(trailingParts));
|
||||
}
|
||||
|
|
|
@ -75,24 +75,30 @@ function printDoc(doc) {
|
|||
}
|
||||
|
||||
if (doc.type === "if-break") {
|
||||
return "ifBreak(" +
|
||||
return (
|
||||
"ifBreak(" +
|
||||
printDoc(doc.breakContents) +
|
||||
(doc.flatContents ? ", " + printDoc(doc.flatContents) : "") +
|
||||
")";
|
||||
")"
|
||||
);
|
||||
}
|
||||
|
||||
if (doc.type === "group") {
|
||||
if (doc.expandedStates) {
|
||||
return "conditionalGroup(" +
|
||||
return (
|
||||
"conditionalGroup(" +
|
||||
"[" +
|
||||
doc.expandedStates.map(printDoc).join(",") +
|
||||
"])";
|
||||
"])"
|
||||
);
|
||||
}
|
||||
|
||||
return (doc.break ? "wrappedGroup" : "group") +
|
||||
return (
|
||||
(doc.break ? "wrappedGroup" : "group") +
|
||||
"(" +
|
||||
printDoc(doc.contents) +
|
||||
")";
|
||||
")"
|
||||
);
|
||||
}
|
||||
|
||||
if (doc.type === "line-suffix") {
|
||||
|
|
|
@ -177,9 +177,8 @@ function printDocToString(doc, options) {
|
|||
// group has these, we need to manually go through
|
||||
// these states and find the first one that fits.
|
||||
if (doc.expandedStates) {
|
||||
const mostExpanded = doc.expandedStates[
|
||||
doc.expandedStates.length - 1
|
||||
];
|
||||
const mostExpanded =
|
||||
doc.expandedStates[doc.expandedStates.length - 1];
|
||||
|
||||
if (doc.break) {
|
||||
cmds.push([ind, MODE_BREAK, mostExpanded]);
|
||||
|
|
|
@ -235,8 +235,11 @@ FPp.needsParens = function(assumeExpressionContext) {
|
|||
}
|
||||
|
||||
if (
|
||||
parent.type === "ArrowFunctionExpression" && parent.body === node && startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ false)
|
||||
|| parent.type === "ExpressionStatement" && startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ true)
|
||||
(parent.type === "ArrowFunctionExpression" &&
|
||||
parent.body === node &&
|
||||
startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ false)) ||
|
||||
(parent.type === "ExpressionStatement" &&
|
||||
startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ true))
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
@ -250,22 +253,28 @@ FPp.needsParens = function(assumeExpressionContext) {
|
|||
|
||||
case "SpreadElement":
|
||||
case "SpreadProperty":
|
||||
return parent.type === "MemberExpression" &&
|
||||
return (
|
||||
parent.type === "MemberExpression" &&
|
||||
name === "object" &&
|
||||
parent.object === node;
|
||||
parent.object === node
|
||||
);
|
||||
|
||||
case "UpdateExpression":
|
||||
if (parent.type === "UnaryExpression") {
|
||||
return node.prefix &&
|
||||
return (
|
||||
node.prefix &&
|
||||
((node.operator === "++" && parent.operator === "+") ||
|
||||
(node.operator === "--" && parent.operator === "-"));
|
||||
(node.operator === "--" && parent.operator === "-"))
|
||||
);
|
||||
}
|
||||
// else fall through
|
||||
// else fall through
|
||||
case "UnaryExpression":
|
||||
switch (parent.type) {
|
||||
case "UnaryExpression":
|
||||
return node.operator === parent.operator &&
|
||||
(node.operator === "+" || node.operator === "-");
|
||||
return (
|
||||
node.operator === parent.operator &&
|
||||
(node.operator === "+" || node.operator === "-")
|
||||
);
|
||||
|
||||
case "MemberExpression":
|
||||
return name === "object" && parent.object === node;
|
||||
|
@ -302,7 +311,7 @@ FPp.needsParens = function(assumeExpressionContext) {
|
|||
if (node.operator === "in" && isLeftOfAForStatement(node)) {
|
||||
return true;
|
||||
}
|
||||
// else fall through
|
||||
// else fall through
|
||||
case "LogicalExpression":
|
||||
switch (parent.type) {
|
||||
case "CallExpression":
|
||||
|
@ -377,7 +386,7 @@ FPp.needsParens = function(assumeExpressionContext) {
|
|||
if (parent.type === "UnaryExpression") {
|
||||
return true;
|
||||
}
|
||||
// else fall through
|
||||
// else fall through
|
||||
case "AwaitExpression":
|
||||
switch (parent.type) {
|
||||
case "TaggedTemplateExpression":
|
||||
|
@ -404,24 +413,30 @@ FPp.needsParens = function(assumeExpressionContext) {
|
|||
|
||||
case "IntersectionTypeAnnotation":
|
||||
case "UnionTypeAnnotation":
|
||||
return parent.type === "ArrayTypeAnnotation" ||
|
||||
return (
|
||||
parent.type === "ArrayTypeAnnotation" ||
|
||||
parent.type === "NullableTypeAnnotation" ||
|
||||
parent.type === "IntersectionTypeAnnotation" ||
|
||||
parent.type === "UnionTypeAnnotation";
|
||||
parent.type === "UnionTypeAnnotation"
|
||||
);
|
||||
|
||||
case "NullableTypeAnnotation":
|
||||
return parent.type === "ArrayTypeAnnotation";
|
||||
|
||||
case "FunctionTypeAnnotation":
|
||||
return parent.type === "UnionTypeAnnotation" ||
|
||||
parent.type === "IntersectionTypeAnnotation";
|
||||
return (
|
||||
parent.type === "UnionTypeAnnotation" ||
|
||||
parent.type === "IntersectionTypeAnnotation"
|
||||
);
|
||||
|
||||
case "NumericLiteral":
|
||||
case "Literal":
|
||||
return parent.type === "MemberExpression" &&
|
||||
return (
|
||||
parent.type === "MemberExpression" &&
|
||||
isNumber.check(node.value) &&
|
||||
name === "object" &&
|
||||
parent.object === node;
|
||||
parent.object === node
|
||||
);
|
||||
|
||||
case "AssignmentExpression":
|
||||
if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
|
||||
|
@ -570,11 +585,20 @@ function startsWithNoLookaheadToken(node, forbidFunctionAndClass) {
|
|||
case "ConditionalExpression":
|
||||
return startsWithNoLookaheadToken(node.test, forbidFunctionAndClass);
|
||||
case "UpdateExpression":
|
||||
return !node.prefix && startsWithNoLookaheadToken(node.argument, forbidFunctionAndClass);
|
||||
return (
|
||||
!node.prefix &&
|
||||
startsWithNoLookaheadToken(node.argument, forbidFunctionAndClass)
|
||||
);
|
||||
case "BindExpression":
|
||||
return node.object && startsWithNoLookaheadToken(node.object, forbidFunctionAndClass);
|
||||
return (
|
||||
node.object &&
|
||||
startsWithNoLookaheadToken(node.object, forbidFunctionAndClass)
|
||||
);
|
||||
case "SequenceExpression":
|
||||
return startsWithNoLookaheadToken(node.expressions[0], forbidFunctionAndClass)
|
||||
return startsWithNoLookaheadToken(
|
||||
node.expressions[0],
|
||||
forbidFunctionAndClass
|
||||
);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -16,12 +16,8 @@ function parseWithFlow(text) {
|
|||
line: ast.errors[0].loc.start.line,
|
||||
column: ast.errors[0].loc.start.column
|
||||
};
|
||||
const msg = ast.errors[0].message +
|
||||
" (" +
|
||||
loc.line +
|
||||
":" +
|
||||
loc.column +
|
||||
")";
|
||||
const msg =
|
||||
ast.errors[0].message + " (" + loc.line + ":" + loc.column + ")";
|
||||
const error = new SyntaxError(msg);
|
||||
error.loc = loc;
|
||||
throw error;
|
||||
|
@ -58,16 +54,16 @@ function parseWithTypeScript(text) {
|
|||
// While we are working on typescript, we are putting it in devDependencies
|
||||
// so it shouldn't be picked up by static analysis
|
||||
const r = require;
|
||||
const parser = r('typescript-eslint-parser')
|
||||
const parser = r("typescript-eslint-parser");
|
||||
return parser.parse(text, {
|
||||
loc: true,
|
||||
range: true,
|
||||
tokens: true,
|
||||
attachComment: true,
|
||||
ecmaFeatures: {
|
||||
jsx: true,
|
||||
jsx: true
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { parseWithFlow, parseWithBabylon, parseWithTypeScript };
|
||||
|
|
837
src/printer.js
837
src/printer.js
File diff suppressed because it is too large
Load Diff
14
src/util.js
14
src/util.js
|
@ -170,7 +170,12 @@ function skipNewline(text, index, opts) {
|
|||
|
||||
const atIndex = text.charAt(index);
|
||||
if (backwards) {
|
||||
if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") {
|
||||
if (
|
||||
atIndex === "\n" ||
|
||||
atIndex === "\r" ||
|
||||
atIndex === "\u2028" ||
|
||||
atIndex === "\u2029"
|
||||
) {
|
||||
return index - 1;
|
||||
}
|
||||
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") {
|
||||
return index + 2;
|
||||
}
|
||||
if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") {
|
||||
if (
|
||||
atIndex === "\n" ||
|
||||
atIndex === "\r" ||
|
||||
atIndex === "\u2028" ||
|
||||
atIndex === "\u2029"
|
||||
) {
|
||||
return index + 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue