diff --git a/src/printer.js b/src/printer.js index 88334f66..24d27eb7 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1486,10 +1486,7 @@ function genericPrintNoParens(path, options, print, args) { if (n.value) { let res; - if ( - (n.value.type === "StringLiteral" || n.value.type === "Literal") && - typeof n.value.value === "string" - ) { + if (isStringLiteral(n.value)) { const value = n.value.extra ? n.value.extra.raw : n.value.raw; res = '"' + value.slice(1, -1).replace(/"/g, """) + '"'; } else { @@ -1570,9 +1567,7 @@ function genericPrintNoParens(path, options, print, args) { if ( n.attributes.length === 1 && n.attributes[0].value && - (n.attributes[0].value.type === "Literal" || - n.attributes[0].value.type === "StringLiteral") && - typeof n.attributes[0].value.value === "string" + isStringLiteral(n.attributes[0].value) ) { return group( concat([ @@ -2893,12 +2888,7 @@ function printPropertyKey(path, options, print) { const node = path.getNode(); const key = node.key; - if ( - (key.type === "StringLiteral" || - (key.type === "Literal" && typeof key.value === "string")) && - isIdentifierName(key.value) && - !node.computed - ) { + if (isStringLiteral(key) && isIdentifierName(key.value) && !node.computed) { // 'a' -> a return path.call( keyPath => comments.printComments(keyPath, () => key.value, options), @@ -4229,10 +4219,10 @@ function printAssignment( const canBreak = (isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) || - ((leftNode.type === "Identifier" || leftNode.type === "MemberExpression") && - (rightNode.type === "StringLiteral" || - (rightNode.type === "Literal" && typeof rightNode.value === "string") || - isMemberExpressionChain(rightNode))); + ((leftNode.type === "Identifier" || + isStringLiteral(leftNode) || + leftNode.type === "MemberExpression") && + (isStringLiteral(rightNode) || isMemberExpressionChain(rightNode))); const printed = printAssignmentRight( rightNode, @@ -4706,6 +4696,13 @@ function isLiteral(node) { ); } +function isStringLiteral(node) { + return ( + node.type === "StringLiteral" || + (node.type === "Literal" && typeof node.value === "string") + ); +} + function removeLines(doc) { // Force this doc into flat mode by statically converting all // lines into spaces (or soft lines into nothing). Hard lines diff --git a/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap b/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap index 7a6debdf..70bbb805 100644 --- a/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap +++ b/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap @@ -17,6 +17,18 @@ function foo() { `; +exports[`long-value.js 1`] = ` +const x = { + "ABC": "12345678901234567890123456789012345678901234567890123456789012345678901234567890" +}; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const x = { + ABC: + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" +}; + +`; + exports[`test.js 1`] = ` const a = classnames({ "some-prop": this.state.longLongLongLongLongLongLongLongLongTooLongProp diff --git a/tests/object-prop-break-in/jsfmt.spec.js b/tests/object-prop-break-in/jsfmt.spec.js index 7580dfab..c426c19c 100644 --- a/tests/object-prop-break-in/jsfmt.spec.js +++ b/tests/object-prop-break-in/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, null, ["typescript"]); +run_spec(__dirname, null, ["babylon", "typescript"]); diff --git a/tests/object-prop-break-in/long-value.js b/tests/object-prop-break-in/long-value.js new file mode 100644 index 00000000..730d326b --- /dev/null +++ b/tests/object-prop-break-in/long-value.js @@ -0,0 +1,3 @@ +const x = { + "ABC": "12345678901234567890123456789012345678901234567890123456789012345678901234567890" +};