prettier
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -142,9 +142,90 @@ #!/usr/bin/env node | ||
if (argv["debug-check"]) { | ||
function massageAST(ast) { | ||
if (Array.isArray(ast)) { | ||
return ast.map(e => massageAST(e)).filter(e => e); | ||
} | ||
if (ast && typeof ast === "object") { | ||
// We remove extra `;` and add them when needed | ||
if (ast.type === "EmptyStatement") { | ||
return undefined; | ||
} | ||
// We move text around, including whitespaces and add {" "} | ||
if (ast.type === "JSXText") { | ||
return undefined; | ||
} | ||
if ( | ||
ast.type === "JSXExpressionContainer" && | ||
ast.expression.type === "Literal" && | ||
ast.expression.value === " " | ||
) { | ||
return undefined; | ||
} | ||
const newObj = {}; | ||
for (var key in ast) { | ||
newObj[key] = massageAST(ast[key]); | ||
} | ||
[ | ||
"loc", | ||
"range", | ||
"raw", | ||
"comments", | ||
"start", | ||
"end", | ||
"tokens", | ||
"flags" | ||
].forEach(name => { | ||
delete newObj[name]; | ||
}); | ||
// We convert <div></div> to <div /> | ||
if (ast.type === "JSXOpeningElement") { | ||
delete newObj.selfClosing; | ||
} | ||
if (ast.type === "JSXElement") { | ||
delete newObj.closingElement; | ||
} | ||
// We change {'key': value} into {key: value} | ||
if ( | ||
ast.type === "Property" && | ||
typeof ast.key === "object" && | ||
ast.key && | ||
(ast.key.type === "Literal" || ast.key.type === "Identifier") | ||
) { | ||
delete newObj.key; | ||
} | ||
return newObj; | ||
} | ||
return ast; | ||
} | ||
function cleanAST(ast) { | ||
return JSON.stringify(massageAST(ast), null, 2); | ||
} | ||
function diff(a, b) { | ||
return require("diff") | ||
.createTwoFilesPatch("", "", a, b, "", "", { context: 2 }); | ||
} | ||
const pp = prettier.format(input, options); | ||
const pppp = prettier.format(pp, options); | ||
if (pp !== pppp) { | ||
const diff = require( | ||
"diff" | ||
).createTwoFilesPatch("", "", pp, pppp, "", "", { context: 2 }); | ||
console.error(diff); | ||
process.stdout.write("\n"); | ||
console.error('prettier(input) !== prettier(prettier(input))'); | ||
console.error(diff(pp, pppp)); | ||
} else { | ||
const ast = cleanAST(prettier.__debug.parse(input, options)); | ||
const past = cleanAST(prettier.__debug.parse(pp, options)); | ||
if (ast !== past) { | ||
process.stdout.write("\n"); | ||
console.error('ast(input) !== ast(prettier(input))'); | ||
console.error(diff(ast, past)); | ||
console.error(diff(input, pp)); | ||
} | ||
} | ||
@@ -151,0 +232,0 @@ return; |
@@ -0,1 +1,53 @@ | ||
# 1.3.0 | ||
[link](https://github.com/jlongster/prettier/compare/1.2.2...1.3.0) | ||
* add printer branches for some TypeScript nodes (#1331) | ||
* Skip trailing commas with FlowShorthandWithOneArg (#1364) | ||
* add TSLastTypeNode and TSIndexedAccessType (#1370) | ||
* add TSConstructorType (#1367) | ||
* fix do-while break (#1373) | ||
* Fix missing trailing commas on flow generics (#1381) | ||
* Add example of using yarn test with arguments (#1383) | ||
* Have --debug-check also run ast verification (#1337) | ||
* Fix empty line in block with EmptyStatement (#1375) | ||
* parent decides how to print type annotations (#1391) | ||
* add TSTypeOperator (#1396) | ||
* fix TSTypeReference not printing typeArguments (#1392) | ||
* add TSMappedType and TSTypeParameter (#1393) | ||
* fix TSFunctionType failing on TypeParameters (#1394) | ||
* add TSIntersectionType (#1395) | ||
* fix typeParameters printing TSFunctionType w/o breaking flow (#1397) | ||
* Fix optional flow parenthesis (#1357) | ||
* [experimental] Add linting step in test pipeline (#1172) | ||
* fix VariableDeclarator not printing type parameters (#1415) | ||
* add TSMethodSignature (#1416) | ||
* Add TSParameterProperty, TSAbstractClassDeclaration and TSAbstractMethodDefinition (#1410) | ||
* Inline nullable in flow generics (#1426) | ||
* fixed method 'check' error 'format' of undefined (#1424) | ||
* feat(typescript): add delcare modifier support for vars, classes and functions (#1436) | ||
* Allow flow declarations to break on StringLiteralTypeAnnotations (#1437) | ||
* Require '::a.b' to have a preceding ; in no-semi style (#1442) | ||
* Require '(a || b).c++' to have a preceding ; in no-semi style (#1443) | ||
* Upgrade flow parser to 0.45 (#1447) | ||
* Add supertype tests and add TSAbstractClassProperty (#1467) | ||
* Break class expression returned by arrow call (#1464) | ||
* update typescript snapshots to account for #1464 (#1470) | ||
* [WIP] add TSNamespaceExportDeclaration (#1459) | ||
* update yarn.lock (#1471) | ||
* [RFC] Do not indent calls with a single template literal argument (#873) | ||
* Proper indentation for template literals (#1385) | ||
* Add parenthesis for unusual nested ternaries (#1386) | ||
* Preserve inline comment as last argument (#1390) | ||
* Only add parenthesis on ternaries inside of arrow functions if doesn't break (#1450) | ||
* Fix windows line ending on template literals (#1439) | ||
* Add space around `=` for flow generics default arguments (#1476) | ||
* Don't break for unparenthesised single argument flow function (#1452) | ||
* Don't break on empty arrays and objects (#1440) | ||
* Do not break on [0] (#1441) | ||
* Reorder flow object props (#1451) | ||
* Break inline object first in function arguments (#1453) | ||
* Break inline object first in function arguments (#1453) (#1173) | ||
* Inline template literals as arrow body (#1485) | ||
# 1.2.2 | ||
@@ -2,0 +54,0 @@ |
@@ -109,3 +109,3 @@ "use strict"; | ||
try { | ||
const formatted = this.format(text, opts); | ||
const formatted = formatWithShebang(text, normalizeOptions(opts)); | ||
return formatted === text; | ||
@@ -118,2 +118,5 @@ } catch (e) { | ||
__debug: { | ||
parse: function(text, opts) { | ||
return parse(text, opts); | ||
}, | ||
formatAST: function(ast, opts) { | ||
@@ -120,0 +123,0 @@ opts = normalizeOptions(opts); |
{ | ||
"name": "prettier", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Prettier is an opinionated JavaScript formatter", | ||
@@ -18,3 +18,3 @@ "bin": { | ||
"esutils": "2.0.2", | ||
"flow-parser": "0.43.0", | ||
"flow-parser": "0.45.0", | ||
"get-stdin": "5.0.1", | ||
@@ -36,4 +36,4 @@ "glob": "7.1.1", | ||
"rollup-plugin-node-resolve": "2.0.0", | ||
"typescript": "2.2.1", | ||
"typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#bfb1506c48b625871ffeb67dbec7941d460f8941" | ||
"typescript": "2.3.2", | ||
"typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#a294afa8158c9c088521eed72b6745eed302361c" | ||
}, | ||
@@ -40,0 +40,0 @@ "scripts": { |
@@ -476,5 +476,5 @@ # Prettier | ||
* The tests uses [Jest](https://facebook.github.io/jest/) snapshots. | ||
* You can make changes and run `jest -u` to update the snapshots. Then run `git | ||
diff` to take a look at what changed. Always update the snapshots when opening | ||
a PR. | ||
* You can make changes and run `jest -u` (or `yarn test -- -u`) to update the | ||
snapshots. Then run `git diff` to take a look at what changed. Always update | ||
the snapshots when opening a PR. | ||
* You can run `AST_COMPARE=1 jest` for a more robust test run. That formats each | ||
@@ -481,0 +481,0 @@ file, re-parses it, and compares the new AST with the original one and makes |
"use strict"; | ||
var assert = require("assert"); | ||
var types = require("ast-types"); | ||
var types = require("./ast-types") | ||
var n = types.namedTypes; | ||
@@ -9,3 +9,2 @@ var isArray = types.builtInTypes.array; | ||
var docBuilders = require("./doc-builders"); | ||
var fromString = docBuilders.fromString; | ||
var concat = docBuilders.concat; | ||
@@ -15,3 +14,2 @@ var hardline = docBuilders.hardline; | ||
var indent = docBuilders.indent; | ||
var align = docBuilders.align; | ||
var lineSuffix = docBuilders.lineSuffix; | ||
@@ -131,3 +129,3 @@ var join = docBuilders.join; | ||
function attach(comments, ast, text, options) { | ||
function attach(comments, ast, text) { | ||
if (!isArray.check(comments)) { | ||
@@ -200,2 +198,9 @@ return; | ||
if ( | ||
handleLastFunctionArgComments( | ||
text, | ||
precedingNode, | ||
enclosingNode, | ||
followingNode, | ||
comment | ||
) || | ||
handleConditionalExpressionComments( | ||
@@ -817,3 +822,2 @@ enclosingNode, | ||
const contents = printComment(commentPath); | ||
const text = options.originalText; | ||
const isBlock = comment.type === "Block" || comment.type === "CommentBlock"; | ||
@@ -833,3 +837,3 @@ | ||
function printTrailingComment(commentPath, print, options, parentNode) { | ||
function printTrailingComment(commentPath, print, options) { | ||
const comment = commentPath.getValue(); | ||
@@ -873,3 +877,2 @@ const contents = printComment(commentPath); | ||
function printDanglingComments(path, options, sameIndent) { | ||
const text = options.originalText; | ||
const parts = []; | ||
@@ -876,0 +879,0 @@ const node = path.getValue(); |
"use strict"; | ||
const utils = require("./doc-utils"); | ||
const willBreak = utils.willBreak; | ||
function assertDoc(val) { | ||
@@ -7,0 +4,0 @@ if ( |
@@ -24,2 +24,12 @@ "use strict"; | ||
function makeAlign(ind, n) { | ||
if (n === -Infinity) { | ||
return { | ||
indent: 0, | ||
align: { | ||
spaces: 0, | ||
tabs: 0 | ||
} | ||
}; | ||
} | ||
return { | ||
@@ -26,0 +36,0 @@ indent: ind.indent, |
"use strict"; | ||
var assert = require("assert"); | ||
var types = require("ast-types"); | ||
var types = require("./ast-types") | ||
var util = require("./util"); | ||
var n = types.namedTypes; | ||
var Node = n.Node; | ||
var isArray = types.builtInTypes.array; | ||
var isNumber = types.builtInTypes.number; | ||
var startsWithNoLookaheadToken = util.startsWithNoLookaheadToken; | ||
@@ -71,3 +71,4 @@ function FastPath(value) { | ||
var value = s[i]; | ||
if (n.Node.check(value) && --count < 0) { | ||
if ((n.Node.check(value)) && --count < 0) { | ||
return value; | ||
@@ -185,3 +186,3 @@ } | ||
// more efficient because we're iterating backwards through a stack. | ||
FPp.needsParens = function(assumeExpressionContext) { | ||
FPp.needsParens = function() { | ||
var parent = this.getParentNode(); | ||
@@ -469,3 +470,2 @@ if (!parent) { | ||
case "JSXSpreadAttribute": | ||
case "ArrowFunctionExpression": | ||
return true; | ||
@@ -542,6 +542,2 @@ | ||
function isBinary(node) { | ||
return n.BinaryExpression.check(node) || n.LogicalExpression.check(node); | ||
} | ||
function containsCallExpression(node) { | ||
@@ -565,56 +561,2 @@ if (n.CallExpression.check(node)) { | ||
// Tests if an expression starts with `{`, or (if forbidFunctionAndClass holds) `function` or `class`. | ||
// Will be overzealous if there's already necessary grouping parentheses. | ||
function startsWithNoLookaheadToken(node, forbidFunctionAndClass) { | ||
node = getLeftMost(node); | ||
switch (node.type) { | ||
case "FunctionExpression": | ||
case "ClassExpression": | ||
return forbidFunctionAndClass; | ||
case "ObjectExpression": | ||
return true; | ||
case "MemberExpression": | ||
return startsWithNoLookaheadToken(node.object, forbidFunctionAndClass); | ||
case "TaggedTemplateExpression": | ||
if (node.tag.type === "FunctionExpression") { | ||
// IIFEs are always already parenthesized | ||
return false; | ||
} | ||
return startsWithNoLookaheadToken(node.tag, forbidFunctionAndClass); | ||
case "CallExpression": | ||
if (node.callee.type === "FunctionExpression") { | ||
// IIFEs are always already parenthesized | ||
return false; | ||
} | ||
return startsWithNoLookaheadToken(node.callee, forbidFunctionAndClass); | ||
case "ConditionalExpression": | ||
return startsWithNoLookaheadToken(node.test, forbidFunctionAndClass); | ||
case "UpdateExpression": | ||
return ( | ||
!node.prefix && | ||
startsWithNoLookaheadToken(node.argument, forbidFunctionAndClass) | ||
); | ||
case "BindExpression": | ||
return ( | ||
node.object && | ||
startsWithNoLookaheadToken(node.object, forbidFunctionAndClass) | ||
); | ||
case "SequenceExpression": | ||
return startsWithNoLookaheadToken( | ||
node.expressions[0], | ||
forbidFunctionAndClass | ||
); | ||
default: | ||
return false; | ||
} | ||
} | ||
function getLeftMost(node) { | ||
if (node.left) { | ||
return getLeftMost(node.left); | ||
} else { | ||
return node; | ||
} | ||
} | ||
module.exports = FastPath; |
"use strict"; | ||
var types = require("ast-types"); | ||
var n = types.namedTypes; | ||
function isExportDeclaration(node) { | ||
@@ -273,2 +270,57 @@ if (node) | ||
// Tests if an expression starts with `{`, or (if forbidFunctionAndClass holds) `function` or `class`. | ||
// Will be overzealous if there's already necessary grouping parentheses. | ||
function startsWithNoLookaheadToken(node, forbidFunctionAndClass) { | ||
node = getLeftMost(node); | ||
switch (node.type) { | ||
case "FunctionExpression": | ||
case "ClassExpression": | ||
return forbidFunctionAndClass; | ||
case "ObjectExpression": | ||
return true; | ||
case "MemberExpression": | ||
return startsWithNoLookaheadToken(node.object, forbidFunctionAndClass); | ||
case "TaggedTemplateExpression": | ||
if (node.tag.type === "FunctionExpression") { | ||
// IIFEs are always already parenthesized | ||
return false; | ||
} | ||
return startsWithNoLookaheadToken(node.tag, forbidFunctionAndClass); | ||
case "CallExpression": | ||
if (node.callee.type === "FunctionExpression") { | ||
// IIFEs are always already parenthesized | ||
return false; | ||
} | ||
return startsWithNoLookaheadToken(node.callee, forbidFunctionAndClass); | ||
case "ConditionalExpression": | ||
return startsWithNoLookaheadToken(node.test, forbidFunctionAndClass); | ||
case "UpdateExpression": | ||
return ( | ||
!node.prefix && | ||
startsWithNoLookaheadToken(node.argument, forbidFunctionAndClass) | ||
); | ||
case "BindExpression": | ||
return ( | ||
node.object && | ||
startsWithNoLookaheadToken(node.object, forbidFunctionAndClass) | ||
); | ||
case "SequenceExpression": | ||
return startsWithNoLookaheadToken( | ||
node.expressions[0], | ||
forbidFunctionAndClass | ||
); | ||
default: | ||
return false; | ||
} | ||
} | ||
function getLeftMost(node) { | ||
if (node.left) { | ||
return getLeftMost(node.left); | ||
} else { | ||
return node; | ||
} | ||
} | ||
module.exports = { | ||
@@ -293,3 +345,4 @@ getPrecedence, | ||
setLocEnd, | ||
htmlEscapeInsideAngleBracket | ||
htmlEscapeInsideAngleBracket, | ||
startsWithNoLookaheadToken | ||
}; |
11
test.js
@@ -1,2 +0,9 @@ | ||
if (VeryVeryVeryVeryVeryVeryVeryVeryLong === VeryVeryVeryVeryVeryVeryVeryVeryLong) { | ||
} | ||
const x = ( | ||
<div | ||
attr1={1} | ||
attr2={2} | ||
// attr3={3} | ||
> | ||
{children} | ||
</div> | ||
) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1966046
33
36764
+ Addedflow-parser@0.45.0(transitive)
- Removedflow-parser@0.43.0(transitive)
Updatedflow-parser@0.45.0