src/nodes/arrays.js in prettier-1.0.0.pre.rc1 vs src/nodes/arrays.js in prettier-1.0.0.pre.rc2

- old
+ new

@@ -16,47 +16,51 @@ // that looks something like: // // ['a', 'b', 'c'] // function isStringArray(args) { - return args.body.every((arg) => { - // We want to verify that every node inside of this array is a string - // literal. We also want to make sure none of them have comments attached. - if (arg.type !== "string_literal" || arg.comments) { - return false; - } + return ( + args.body.length > 1 && + args.body.every((arg) => { + // We want to verify that every node inside of this array is a string + // literal. We also want to make sure none of them have comments attached. + if (arg.type !== "string_literal" || arg.comments) { + return false; + } - // If the string has multiple parts (meaning plain string content but also - // interpolated content) then we know it's not a simple string. - if (arg.body.length !== 1) { - return false; - } + // If the string has multiple parts (meaning plain string content but also + // interpolated content) then we know it's not a simple string. + if (arg.body.length !== 1) { + return false; + } - const part = arg.body[0]; + const part = arg.body[0]; - // If the only part of this string is not @tstring_content then it's - // interpolated, so again we can return false. - if (part.type !== "@tstring_content") { - return false; - } + // If the only part of this string is not @tstring_content then it's + // interpolated, so again we can return false. + if (part.type !== "@tstring_content") { + return false; + } - // Finally, verify that the string doesn't contain a space, an escape - // character, or brackets so that we know it can be put into a string - // literal array. - return !/[\s\\[\]]/.test(part.body); - }); + // Finally, verify that the string doesn't contain a space, an escape + // character, or brackets so that we know it can be put into a string + // literal array. + return !/[\s\\[\]]/.test(part.body); + }) + ); } // Checks that every argument within this args node is a symbol_literal node (as // opposed to a dyna_symbol) so it has no interpolation. This means we're // dealing with an array that looks something like: // // [:a, :b, :c] // function isSymbolArray(args) { - return args.body.every( - (arg) => arg.type === "symbol_literal" && !arg.comments + return ( + args.body.length > 1 && + args.body.every((arg) => arg.type === "symbol_literal" && !arg.comments) ); } // Prints out a word that is a part of a special array literal that accepts // interpolation. The body is an array of either plain strings or interpolated @@ -121,19 +125,19 @@ return array.comments ? printEmptyArrayWithComments(path, opts) : "[]"; } // If we have an array that contains only simple string literals with no // spaces or interpolation, then we're going to print a %w array. - if (isStringArray(args)) { + if (opts.rubyArrayLiteral && isStringArray(args)) { const printString = (stringPath) => stringPath.call(print, "body", 0); const parts = path.map(printString, "body", 0, "body"); return printSpecialArrayParts(["%w"].concat(parts)); } // If we have an array that contains only simple symbol literals with no // interpolation, then we're going to print a %i array. - if (isSymbolArray(args)) { + if (opts.rubyArrayLiteral && isSymbolArray(args)) { const printSymbol = (symbolPath) => symbolPath.call(print, "body", 0); const parts = path.map(printSymbol, "body", 0, "body"); return printSpecialArrayParts(["%i"].concat(parts)); }