From b93a207bb24f67646244e94d989343cc506dc34a Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 15 Feb 2017 19:56:11 -0800 Subject: [PATCH] Run prettier on 0.17.0 --- bin/prettier.js | 6 +- index.js | 4 +- src/comments.js | 36 +++++++--- src/doc-debug.js | 22 +++---- src/fast-path.js | 68 +++++++++---------- src/printer.js | 166 +++++++++++++++++++++++++---------------------- src/util.js | 8 +-- 7 files changed, 169 insertions(+), 141 deletions(-) diff --git a/bin/prettier.js b/bin/prettier.js index 608b03af0..7094b5af1 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -129,7 +129,7 @@ const options = { parser: getParserOption(), singleQuote: argv["single-quote"], trailingComma: argv["trailing-comma"], - jsxBracketSameLine: argv["jsx-bracket-same-line"], + jsxBracketSameLine: argv["jsx-bracket-same-line"] }; function format(input) { @@ -142,7 +142,9 @@ function format(input) { const pp = prettier.format(input, options); const pppp = prettier.format(pp, options); if (pp !== pppp) { - const diff = require('diff').createTwoFilesPatch('', '', pp, pppp, '', '', {context: 2}); + const diff = require( + "diff" + ).createTwoFilesPatch("", "", pp, pppp, "", "", { context: 2 }); console.error(diff); } return; diff --git a/index.js b/index.js index 5e90f5aa6..6afd22848 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,9 @@ function ensureAllCommentsPrinted(astComments) { astComments.forEach(comment => { if (!comment.printed) { throw new Error( - 'Comment "' + comment.value.trim() + '" was not printed. Please report this error!' + 'Comment "' + + comment.value.trim() + + '" was not printed. Please report this error!' ); } delete comment.printed; diff --git a/src/comments.js b/src/comments.js index cf6c2d7e7..d2f1f99f9 100644 --- a/src/comments.js +++ b/src/comments.js @@ -82,7 +82,7 @@ function decorateComment(node, comment, text) { if ( locStart(child) - locStart(comment) <= 0 && - locEnd(comment) - locEnd(child) <= 0 + locEnd(comment) - locEnd(child) <= 0 ) { // The comment is completely contained by this child node. comment.enclosingNode = child; @@ -142,8 +142,8 @@ function attach(comments, ast, text) { // We also need to check if it's the first line of the file. if ( handleMemberExpressionComment(enclosingNode, followingNode, comment) || - handleIfStatementComments(enclosingNode, followingNode, comment) || - handleTryStatementComments(enclosingNode, followingNode, comment) + handleIfStatementComments(enclosingNode, followingNode, comment) || + handleTryStatementComments(enclosingNode, followingNode, comment) ) { // We're good } else if (followingNode) { @@ -158,7 +158,13 @@ function attach(comments, ast, text) { addDanglingComment(ast, comment); } } else if (util.hasNewline(text, locEnd(comment))) { - if (handleConditionalExpressionComments(enclosingNode, followingNode, comment)) { + if ( + handleConditionalExpressionComments( + enclosingNode, + followingNode, + comment + ) + ) { // We're good } else if (precedingNode) { // There is content before this comment on the same line, but @@ -362,9 +368,9 @@ function handleTryStatementComments(enclosingNode, followingNode, comment) { function handleMemberExpressionComment(enclosingNode, followingNode, comment) { if ( enclosingNode && - enclosingNode.type === "MemberExpression" && - followingNode && - followingNode.type === "Identifier" + enclosingNode.type === "MemberExpression" && + followingNode && + followingNode.type === "Identifier" ) { addLeadingComment(enclosingNode, comment); return true; @@ -373,8 +379,16 @@ function handleMemberExpressionComment(enclosingNode, followingNode, comment) { return false; } -function handleConditionalExpressionComments(enclosingNode, followingNode, comment) { - if (enclosingNode && enclosingNode.type === 'ConditionalExpression' && followingNode) { +function handleConditionalExpressionComments( + enclosingNode, + followingNode, + comment +) { + if ( + enclosingNode && + enclosingNode.type === "ConditionalExpression" && + followingNode + ) { addLeadingComment(followingNode, comment); return true; } @@ -442,7 +456,9 @@ function printTrailingComment(commentPath, print, options, parentNode) { comment ); - return lineSuffix(concat([hardline, isLineBeforeEmpty ? hardline : "", contents])); + return lineSuffix( + concat([hardline, isLineBeforeEmpty ? hardline : "", contents]) + ); } else if (isBlock) { // Trailing block comments never need a newline return concat([" ", contents]); diff --git a/src/doc-debug.js b/src/doc-debug.js index e1fc15671..79b4a3c5a 100644 --- a/src/doc-debug.js +++ b/src/doc-debug.js @@ -19,21 +19,19 @@ function flattenDoc(doc) { return Object.assign({}, doc, { parts: res }); } else if (doc.type === "if-break") { return Object.assign({}, doc, { - breakContents: ( - doc.breakContents != null ? flattenDoc(doc.breakContents) : null - ), - flatContents: ( - doc.flatContents != null ? flattenDoc(doc.flatContents) : null - ) + breakContents: doc.breakContents != null + ? flattenDoc(doc.breakContents) + : null, + flatContents: doc.flatContents != null + ? flattenDoc(doc.flatContents) + : null }); } else if (doc.type === "group") { return Object.assign({}, doc, { contents: flattenDoc(doc.contents), - expandedStates: ( - doc.expandedStates - ? doc.expandedStates.map(flattenDoc) - : doc.expandedStates - ) + expandedStates: doc.expandedStates + ? doc.expandedStates.map(flattenDoc) + : doc.expandedStates }); } else if (doc.contents) { return Object.assign({}, doc, { contents: flattenDoc(doc.contents) }); @@ -97,7 +95,7 @@ function printDoc(doc) { return "lineSuffix(" + printDoc(doc.contents) + ")"; } - throw new Error('Unknown doc type ' + doc.type); + throw new Error("Unknown doc type " + doc.type); } module.exports = { diff --git a/src/fast-path.js b/src/fast-path.js index d4dbca3a3..84afe6be3 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -213,21 +213,21 @@ FPp.needsParens = function(assumeExpressionContext) { // all expressions. if ( (parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && - parent.superClass === node && - (node.type === "ArrowFunctionExpression" || - node.type === "AssignmentExpression" || - node.type === "AwaitExpression" || - node.type === "BinaryExpression" || - node.type === "ConditionalExpression" || - node.type === "LogicalExpression" || - node.type === "NewExpression" || - node.type === "ObjectExpression" || - node.type === "ParenthesizedExpression" || - node.type === "SequenceExpression" || - node.type === "TaggedTemplateExpression" || - node.type === "UnaryExpression" || - node.type === "UpdateExpression" || - node.type === "YieldExpression") + parent.superClass === node && + (node.type === "ArrowFunctionExpression" || + node.type === "AssignmentExpression" || + node.type === "AwaitExpression" || + node.type === "BinaryExpression" || + node.type === "ConditionalExpression" || + node.type === "LogicalExpression" || + node.type === "NewExpression" || + node.type === "ObjectExpression" || + node.type === "ParenthesizedExpression" || + node.type === "SequenceExpression" || + node.type === "TaggedTemplateExpression" || + node.type === "UnaryExpression" || + node.type === "UpdateExpression" || + node.type === "YieldExpression") ) { return true; } @@ -236,11 +236,11 @@ FPp.needsParens = function(assumeExpressionContext) { // be parenthesized unless it's an ident or literal if ( parent.type === "BinaryExpression" && - parent.operator === "**" && - parent.left === node && - node.type !== "Identifier" && - node.type !== "Literal" && - node.type !== "NumericLiteral" + parent.operator === "**" && + parent.left === node && + node.type !== "Identifier" && + node.type !== "Literal" && + node.type !== "NumericLiteral" ) { return true; } @@ -265,8 +265,8 @@ FPp.needsParens = function(assumeExpressionContext) { case "UnaryExpression": if ( node.prefix && - (node.operator === "++" && parent.operator === "+" || - node.operator === "--" && parent.operator === "-") + (node.operator === "++" && parent.operator === "+" || + node.operator === "--" && parent.operator === "-") ) { return true; } @@ -287,8 +287,8 @@ FPp.needsParens = function(assumeExpressionContext) { case "BinaryExpression": if ( node.operator === "in" && - parent.type === "ForStatement" && - parent.init === node + parent.type === "ForStatement" && + parent.init === node ) { return true; } @@ -402,8 +402,8 @@ FPp.needsParens = function(assumeExpressionContext) { case "AssignmentExpression": if ( parent.type === "ArrowFunctionExpression" && - parent.body === node && - node.left.type === "ObjectPattern" + parent.body === node && + node.left.type === "ObjectPattern" ) { return true; } @@ -505,8 +505,8 @@ FPp.needsParens = function(assumeExpressionContext) { default: if ( parent.type === "NewExpression" && - name === "callee" && - parent.callee === node + name === "callee" && + parent.callee === node ) { return containsCallExpression(node); } @@ -514,8 +514,8 @@ FPp.needsParens = function(assumeExpressionContext) { if ( assumeExpressionContext !== true && - !this.canBeFirstInStatement() && - this.firstInStatement() + !this.canBeFirstInStatement() && + this.firstInStatement() ) return true; @@ -583,8 +583,8 @@ FPp.firstInStatement = function() { if ( n.SequenceExpression.check(parent) && - parentName === "expressions" && - childName === 0 + parentName === "expressions" && + childName === 0 ) { assert.strictEqual(parent.expressions[0], child); continue; @@ -612,8 +612,8 @@ FPp.firstInStatement = function() { if ( n.UnaryExpression.check(parent) && - !parent.prefix && - childName === "argument" + !parent.prefix && + childName === "argument" ) { assert.strictEqual(parent.argument, child); continue; diff --git a/src/printer.js b/src/printer.js index a84970e8c..895ef4c73 100644 --- a/src/printer.js +++ b/src/printer.js @@ -47,21 +47,20 @@ function genericPrint(path, options, printPath) { } // Escape hatch - if (node.comments && - node.comments.length > 0 && - node.comments[0].value.trim() === 'prettier-ignore') { - return options.originalText.slice( - util.locStart(node), - util.locEnd(node) - ); + if ( + node.comments && + node.comments.length > 0 && + node.comments[0].value.trim() === "prettier-ignore" + ) { + return options.originalText.slice(util.locStart(node), util.locEnd(node)); } if ( node.decorators && - node.decorators.length > 0 && - // If the parent node is an export declaration, it will be - // responsible for printing node.decorators. - !util.getParentExportDeclaration(path) + node.decorators.length > 0 && + // If the parent node is an export declaration, it will be + // responsible for printing node.decorators. + !util.getParentExportDeclaration(path) ) { const separator = node.decorators.length === 1 && node.decorators[0].expression.type === "Identifier" @@ -75,8 +74,8 @@ function genericPrint(path, options, printPath) { ); } else if ( util.isExportDeclaration(node) && - node.declaration && - node.declaration.decorators + node.declaration && + node.declaration.decorators ) { // Export declarations are responsible for printing any decorators // that logically apply to node.declaration. @@ -188,9 +187,10 @@ function genericPrintNoParens(path, options, print) { if ( shouldInlineLogicalExpression(n) || (parent.type === "IfStatement" || - parent.type === "WhileStatement" || - parent.type === "DoStatement" || - parent.type === "ForStatement") && n !== parent.body + parent.type === "WhileStatement" || + parent.type === "DoStatement" || + parent.type === "ForStatement") && + n !== parent.body ) { return group(concat(parts)); } @@ -287,13 +287,13 @@ function genericPrintNoParens(path, options, print) { if ( n.params.length === 1 && - !n.rest && - n.params[0].type === "Identifier" && - !n.params[0].typeAnnotation && - !n.params[0].leadingComments && - !n.params[0].trailingComments && - !n.predicate && - !n.returnType + !n.rest && + n.params[0].type === "Identifier" && + !n.params[0].typeAnnotation && + !n.params[0].leadingComments && + !n.params[0].trailingComments && + !n.predicate && + !n.returnType ) { parts.push(path.call(print, "params", 0)); } else { @@ -316,9 +316,9 @@ function genericPrintNoParens(path, options, print) { // as the arrow. if ( n.body.type === "ArrayExpression" || - n.body.type === "ObjectExpression" || - n.body.type === "JSXElement" || - n.body.type === "BlockStatement" + n.body.type === "ObjectExpression" || + n.body.type === "JSXElement" || + n.body.type === "BlockStatement" ) { return group(collapsed); } @@ -463,7 +463,7 @@ function genericPrintNoParens(path, options, print) { var value = specifierPath.getValue(); if ( namedTypes.ImportDefaultSpecifier.check(value) || - namedTypes.ImportNamespaceSpecifier.check(value) + namedTypes.ImportNamespaceSpecifier.check(value) ) { standalones.push(print(specifierPath)); } else { @@ -538,13 +538,13 @@ function genericPrintNoParens(path, options, print) { var parent = path.getParentNode(); if ( !hasContent && - !hasDirectives && - !n.comments && - (parent.type === "ArrowFunctionExpression" || - parent.type === "FunctionExpression" || - parent.type === "FunctionDeclaration" || - parent.type === "ObjectMethod" || - parent.type === "ClassMethod") + !hasDirectives && + !n.comments && + (parent.type === "ArrowFunctionExpression" || + parent.type === "FunctionExpression" || + parent.type === "FunctionDeclaration" || + parent.type === "ObjectMethod" || + parent.type === "ClassMethod") ) { return "{}"; } @@ -578,15 +578,20 @@ function genericPrintNoParens(path, options, print) { case "ReturnStatement": parts.push("return"); - if (n.argument && - n.argument.comments && - n.argument.comments.some(comment => comment.leading)) { + if ( + n.argument && + n.argument.comments && + n.argument.comments.some(comment => comment.leading) + ) { parts.push( concat([ - ' (', - indent(options.tabWidth, concat([softline, path.call(print, "argument")])), + " (", + indent( + options.tabWidth, + concat([softline, path.call(print, "argument")]) + ), line, - ')' + ")" ]) ); } else if (n.argument) { @@ -622,7 +627,7 @@ function genericPrintNoParens(path, options, print) { var leftBrace = n.exact ? "{|" : "{"; var rightBrace = n.exact ? "|}" : "}"; var parent = path.getParentNode(0); - var parentIsUnionTypeAnnotation = parent.type === "UnionTypeAnnotation" + var parentIsUnionTypeAnnotation = parent.type === "UnionTypeAnnotation"; if (isTypeAnnotation) { fields.push("indexers", "callProperties"); @@ -640,7 +645,9 @@ function genericPrintNoParens(path, options, print) { props.push(group(print(childPath))); separatorParts = [separator, line]; - if (util.isNextLineEmpty(options.originalText, childPath.getValue())) { + if ( + util.isNextLineEmpty(options.originalText, childPath.getValue()) + ) { separatorParts.push(hardline); } }, @@ -673,10 +680,7 @@ function genericPrintNoParens(path, options, print) { leftBrace, indent( options.tabWidth + (parentIsUnionTypeAnnotation ? 2 : 0), - concat([ - options.bracketSpacing ? line : softline, - concat(props) - ]) + concat([options.bracketSpacing ? line : softline, concat(props)]) ), ifBreak(canHaveTrailingComma && options.trailingComma ? "," : ""), indent( @@ -765,8 +769,10 @@ function genericPrintNoParens(path, options, print) { printedElements.push(group(print(childPath))); separatorParts = [",", line]; - if (childPath.getValue() && - util.isNextLineEmpty(options.originalText, childPath.getValue())) { + if ( + childPath.getValue() && + util.isNextLineEmpty(options.originalText, childPath.getValue()) + ) { separatorParts.push(softline); } }, @@ -1157,7 +1163,7 @@ function genericPrintNoParens(path, options, print) { let res; if ( (n.value.type === "StringLiteral" || n.value.type === "Literal") && - typeof n.value.value === "string" + typeof n.value.value === "string" ) { res = '"' + util.htmlEscapeInsideDoubleQuote(n.value.value) + '"'; } else { @@ -1220,9 +1226,9 @@ function genericPrintNoParens(path, options, print) { // don't break up opening elements with a single long text attribute if ( n.attributes.length === 1 && - n.attributes[0].value && - n.attributes[0].value.type === "Literal" && - typeof n.attributes[0].value.value === "string" + n.attributes[0].value && + n.attributes[0].value.type === "Literal" && + typeof n.attributes[0].value.value === "string" ) { return group( concat([ @@ -1246,9 +1252,9 @@ function genericPrintNoParens(path, options, print) { path.map(attr => concat([line, print(attr)]), "attributes") ) ), - n.selfClosing ? line : (options.jsxBracketSameLine ? ">" : softline) + n.selfClosing ? line : options.jsxBracketSameLine ? ">" : softline ]), - n.selfClosing ? "/>" : (options.jsxBracketSameLine ? "" : ">") + n.selfClosing ? "/>" : options.jsxBracketSameLine ? "" : ">" ]) ); } @@ -1257,7 +1263,10 @@ function genericPrintNoParens(path, options, print) { case "JSXText": throw new Error("JSXTest should be handled by JSXElement"); case "JSXEmptyExpression": - return concat([comments.printDanglingComments(path, options, /* sameIndent */ true), softline]); + return concat([ + comments.printDanglingComments(path, options, /* sameIndent */ true), + softline + ]); case "TypeAnnotatedIdentifier": return concat([ path.call(print, "annotation"), @@ -1482,7 +1491,7 @@ function genericPrintNoParens(path, options, print) { case "InterfaceDeclaration": { if ( n.type === "DeclareInterface" || - isFlowNodeStartingWithDeclare(n, options) + isFlowNodeStartingWithDeclare(n, options) ) { parts.push("declare "); } @@ -1599,7 +1608,7 @@ function genericPrintNoParens(path, options, print) { case "TypeAlias": { if ( n.type === "DeclareTypeAlias" || - isFlowNodeStartingWithDeclare(n, options) + isFlowNodeStartingWithDeclare(n, options) ) { parts.push("declare "); } @@ -1748,11 +1757,11 @@ function printPropertyKey(path, options, print) { if ( (key.type === "StringLiteral" || key.type === "Literal" && typeof key.value === "string") && - 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$_]/)) + 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$_]/)) ) { // 'a' -> a return key.value; @@ -2002,8 +2011,8 @@ function printExportDeclaration(path, options, print) { if ( decl.type === "ExportDefaultDeclaration" && - (decl.declaration.type !== "ClassDeclaration" && - decl.declaration.type !== "FunctionDeclaration") + (decl.declaration.type !== "ClassDeclaration" && + decl.declaration.type !== "FunctionDeclaration") ) { parts.push(";"); } @@ -2011,13 +2020,13 @@ function printExportDeclaration(path, options, print) { if (decl.specifiers && decl.specifiers.length > 0) { if ( decl.specifiers.length === 1 && - decl.specifiers[0].type === "ExportBatchSpecifier" + decl.specifiers[0].type === "ExportBatchSpecifier" ) { parts.push("*"); } else if ( decl.specifiers.length === 1 && decl.specifiers[0].type === "ExportDefaultSpecifier" || - decl.specifiers[0].type === "ExportNamespaceSpecifier" + decl.specifiers[0].type === "ExportNamespaceSpecifier" ) { parts.push(path.map(print, "specifiers")[0]); } else { @@ -2220,7 +2229,7 @@ function printMemberChain(path, options, print) { for (; i + 1 < printedNodes.length; ++i) { if ( printedNodes[i].node.type === "MemberExpression" && - printedNodes[i + 1].node.type === "MemberExpression" + printedNodes[i + 1].node.type === "MemberExpression" ) { currentGroup.push(printedNodes[i]); } else { @@ -2265,8 +2274,7 @@ function printMemberChain(path, options, print) { // node is just an identifier with the name starting with a capital // letter or just a sequence of _$. The rationale is that they are // likely to be factories. - const shouldMerge = - groups[0].length === 1 && + const shouldMerge = groups[0].length === 1 && groups[0][0].node.type === "Identifier" && groups[0][0].node.name.match(/(^[A-Z])|^[_$]+$/) && groups.length >= 2; @@ -2519,11 +2527,14 @@ function printJSXElement(path, options, print) { // 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. - concat(groups.map(contents => - Array.isArray(contents) - ? conditionalGroup([concat(contents)]) - : contents - )) + concat( + groups.map( + contents => + Array.isArray(contents) + ? conditionalGroup([concat(contents)]) + : contents + ) + ) ]; const closingLines = path.call(print, "closingElement"); @@ -2580,10 +2591,9 @@ function isBinaryish(node) { } function shouldInlineLogicalExpression(node) { - return node.type === "LogicalExpression" && ( - node.right.type === "ObjectExpression" || - node.right.type === "ArrayExpression" - ); + return node.type === "LogicalExpression" && + (node.right.type === "ObjectExpression" || + node.right.type === "ArrayExpression"); } // For binary expressions to be consistent, we need to group diff --git a/src/util.js b/src/util.js index 99c1585c6..9825b6259 100644 --- a/src/util.js +++ b/src/util.js @@ -36,7 +36,7 @@ function fixFaultyLocations(node, text) { } } else if ( n.MethodDefinition && n.MethodDefinition.check(node) || - n.Property.check(node) && (node.method || node.shorthand) + n.Property.check(node) && (node.method || node.shorthand) ) { if (n.FunctionExpression.check(node.value)) { // FunctionExpression method values should be anonymous, @@ -198,9 +198,9 @@ function hasNewlineInRange(text, start, end) { // Note: this function doesn't ignore leading comments unlike isNextLineEmpty function isPreviousLineEmpty(text, node) { let idx = locStart(node) - 1; - idx = skipSpaces(text, idx, {backwards: true}); - idx = skipNewline(text, idx, {backwards: true}); - return hasNewline(text, idx, {backwards: true}); + idx = skipSpaces(text, idx, { backwards: true }); + idx = skipNewline(text, idx, { backwards: true }); + return hasNewline(text, idx, { backwards: true }); } function isNextLineEmpty(text, node) {