From 067b57dba83c1ef951c29d49a84b0b6740cd4e70 Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 24 May 2018 09:21:38 -0400 Subject: [PATCH] Allow toggling on/off inline conditionals and loops --- src/conditionals.js | 5 +- src/index.js | 14 + src/loops.js | 7 +- src/params.js | 6 +- src/print.js | 200 ++++++------ test/__snapshots__/index.test.js.snap | 430 +++++++++++++++++++++++++- test/config/inline.json | 4 + test/config/noinline.json | 4 + test/index.test.js | 30 +- 9 files changed, 564 insertions(+), 136 deletions(-) create mode 100644 test/config/inline.json create mode 100644 test/config/noinline.json diff --git a/src/conditionals.js b/src/conditionals.js index 0072232b..5167d82d 100644 --- a/src/conditionals.js +++ b/src/conditionals.js @@ -1,4 +1,4 @@ -const { concat, hardline, group, ifBreak, indent, softline } = require("prettier").doc.builders; +const { breakParent, concat, hardline, group, ifBreak, indent, softline } = require("prettier").doc.builders; const printWithAddition = (keyword, path, print) => concat([ `${keyword} `, @@ -13,7 +13,7 @@ const printTernaryConditions = (keyword, truthyValue, falsyValue) => { return keyword === "if" ? parts : parts.reverse(); }; -const printConditional = keyword => (path, print) => { +const printConditional = keyword => (path, options, print) => { const [_predicate, _statements, addition] = path.getValue().body; // If the addition is not an elsif or an else, then it's the second half of a @@ -62,6 +62,7 @@ const printConditional = keyword => (path, print) => { concat([softline, "end"]) ]), concat([ + options.inlineConditionals ? "" : breakParent, path.call(print, "body", 1), ` ${keyword} `, path.call(print, "body", 0) diff --git a/src/index.js b/src/index.js index c4ec191d..b4defbc9 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,20 @@ module.exports = { print } }, + options: { + inlineConditionals: { + type: "boolean", + category: "Global", + default: true, + description: "When it fits on one line, allow if and unless statements to use the modifier form." + }, + inlineLoops: { + type: "boolean", + category: "Global", + default: true, + description: "When it fits on one line, allow while and until statements to use the modifier form." + } + }, defaultOptions: { printWidth: 80, tabWidth: 2 diff --git a/src/loops.js b/src/loops.js index 1e07c3f4..d262a40e 100644 --- a/src/loops.js +++ b/src/loops.js @@ -1,19 +1,20 @@ -const { concat, group, hardline, ifBreak, indent, softline } = require("prettier").doc.builders; +const { breakParent, concat, group, hardline, ifBreak, indent, softline } = require("prettier").doc.builders; -const printLoop = keyword => (path, print) => group(ifBreak( +const printLoop = keyword => (path, options, print) => group(ifBreak( concat([ concat([`${keyword} `, path.call(print, "body", 0)]), indent(concat([softline, path.call(print, "body", 1)])), concat([softline, "end"]) ]), concat([ + options.inlineLoops ? "" : breakParent, path.call(print, "body", 1), ` ${keyword} `, path.call(print, "body", 0) ]) )); -const printFor = (path, print) => group(concat([ +const printFor = (path, options, print) => group(concat([ path.call(print, "body", 1), ".each do |", path.call(print, "body", 0), diff --git a/src/params.js b/src/params.js index 6f420550..3e1b8040 100644 --- a/src/params.js +++ b/src/params.js @@ -1,14 +1,14 @@ const { concat, group, join } = require("prettier").doc.builders; -const printKwargRestParam = (path, print) => ( +const printKwargRestParam = (path, options, print) => ( concat(["**", path.call(print, "body", 0)]) ); -const printRestParam = (path, print) => ( +const printRestParam = (path, options, print) => ( concat(["*", path.call(print, "body", 0)]) ); -const printParams = (path, print) => { +const printParams = (path, options, print) => { const [reqs, opts, rest, post, kwargs, kwarg_rest, block] = path.getValue().body; let parts = []; diff --git a/src/print.js b/src/print.js index 50cfa81c..d107b63c 100644 --- a/src/print.js +++ b/src/print.js @@ -8,26 +8,26 @@ const { printWhile, printUntil, printFor } = require("./loops"); const { printKwargRestParam, printRestParam, printParams } = require("./params"); const lineNoFrom = require("./layout"); -const concatBody = (path, print) => concat(path.map(print, "body")); +const concatBody = (path, options, print) => concat(path.map(print, "body")); const nodes = { - alias: (path, print) => concat([ + alias: (path, options, print) => concat([ "alias ", join(" ", path.map(print, "body")) ]), - aref: (path, print) => group(concat([ + aref: (path, options, print) => group(concat([ path.call(print, "body", 0), "[", indent(concat([softline, path.call(print, "body", 1)])), concat([softline, "]"]) ])), - aref_field: (path, print) => group(concat([ + aref_field: (path, options, print) => group(concat([ path.call(print, "body", 0), "[", indent(concat([softline, path.call(print, "body", 1)])), concat([softline, "]"]) ])), - arg_paren: (path, print) => { + arg_paren: (path, options, print) => { if (path.getValue().body === null) { return ""; } @@ -38,7 +38,7 @@ const nodes = { concat([softline, ")"]) ])) }, - args_add: (path, print) => { + args_add: (path, options, print) => { if (path.getValue().body[0].type === "args_new") { return path.call(print, "body", 1); } @@ -50,7 +50,7 @@ const nodes = { path.call(print, "body", 1) ]); }, - args_add_block: (path, print) => { + args_add_block: (path, options, print) => { const [args, block] = path.getValue().body; const parts = args.type === "args_new" ? [] : [path.call(print, "body", 0)]; @@ -63,7 +63,7 @@ const nodes = { return group(concat(parts)); }, - args_add_star: (path, print) => { + args_add_star: (path, options, print) => { if (path.getValue().body[0].type === "args_new") { return concat(["*", path.call(print, "body", 1)]); } @@ -75,8 +75,8 @@ const nodes = { concat(["*", path.call(print, "body", 1)]) ]); }, - args_new: (path, print) => group(concat(["[", softline])), - array: (path, print) => { + args_new: (path, options, print) => group(concat(["[", softline])), + array: (path, options, print) => { if (path.getValue().body[0] === null) { return '[]'; } @@ -92,7 +92,7 @@ const nodes = { "]" ])); }, - assoc_new: (path, print) => { + assoc_new: (path, options, print) => { const parts = []; switch (path.getValue().body[0].type) { @@ -114,35 +114,35 @@ const nodes = { return group(concat(parts)); }, - assoc_splat: (path, print) => group(concat([ + assoc_splat: (path, options, print) => group(concat([ "**", path.call(print, "body", 0) ])), - assoclist_from_args: (path, print) => group( + assoclist_from_args: (path, options, print) => group( join(concat([",", line]), path.map(print, "body", 0)) ), - assign: (path, print) => group(concat([ + assign: (path, options, print) => group(concat([ path.call(print, "body", 0), " =", indent(concat([line, path.call(print, "body", 1)])) ])), - bare_assoc_hash: (path, print) => group( + bare_assoc_hash: (path, options, print) => group( join(concat([",", line]), path.map(print, "body", 0) )), - begin: (path, print) => group(concat([ + begin: (path, options, print) => group(concat([ "begin", indent(concat([hardline, concat(path.map(print, "body"))])), group(concat([hardline, "end"])) ])), - binary: (path, print) => join(` ${path.getValue().body[1]} `, [ + binary: (path, options, print) => join(` ${path.getValue().body[1]} `, [ path.call(print, "body", 0), path.call(print, "body", 2) ]), - block_var: (path, print) => concat(["|", path.call(print, "body", 0), "| "]), - blockarg: (path, print) => concat(["&", path.call(print, "body", 0)]), - bodystmt: (path, print) => { + block_var: (path, options, print) => concat(["|", path.call(print, "body", 0), "| "]), + blockarg: (path, options, print) => concat(["&", path.call(print, "body", 0)]), + bodystmt: (path, options, print) => { const [statements, ...additions] = path.getValue().body; const parts = [path.call(print, "body", 0)]; @@ -157,7 +157,7 @@ const nodes = { return group(concat(parts)); }, - brace_block: (path, print) => { + brace_block: (path, options, print) => { const parts = ["{ "]; if (path.getValue().body[0]) { @@ -167,17 +167,17 @@ const nodes = { return group(concat(parts)); }, - break: (path, print) => { + break: (path, options, print) => { if (path.getValue().body[0].length > 0) { return concat(["break ", path.call(print, "body", 0)]); } return "break"; }, - call: (path, print) => join(path.getValue().body[1], [ + call: (path, options, print) => join(path.getValue().body[1], [ path.call(print, "body", 0), path.call(print, "body", 2) ]), - case: (path, print) => { + case: (path, options, print) => { const parts = ["case "]; if (path.getValue().body[0]) { @@ -191,7 +191,7 @@ const nodes = { return group(concat(parts)); }, - class: (path, print) => { + class: (path, options, print) => { const [_constant, superclass, _statements] = path.getValue().body; const parts = [concat(["class ", path.call(print, "body", 0)])]; @@ -206,11 +206,11 @@ const nodes = { return group(concat(parts)); }, - command: (path, print) => group(join(" ", path.map(print, "body"))), - const_path_field: (path, print) => join("::", path.map(print, "body")), - const_path_ref: (path, print) => join("::", path.map(print, "body")), - const_ref: (path, print) => path.call(print, "body", 0), - def: (path, print) => group(concat([ + command: (path, options, print) => group(join(" ", path.map(print, "body"))), + const_path_field: (path, options, print) => join("::", path.map(print, "body")), + const_path_ref: (path, options, print) => join("::", path.map(print, "body")), + const_ref: (path, options, print) => path.call(print, "body", 0), + def: (path, options, print) => group(concat([ group(concat([ "def ", path.call(print, "body", 0), @@ -219,7 +219,7 @@ const nodes = { indent(concat([hardline, path.call(print, "body", 2)])), group(concat([hardline, "end"])) ])), - defs: (path, print) => group(concat([ + defs: (path, options, print) => group(concat([ group(concat([ "def ", path.call(print, "body", 0), @@ -230,14 +230,14 @@ const nodes = { indent(concat([hardline, path.call(print, "body", 4)])), group(concat([hardline, "end"])) ])), - defined: (path, print) => group(concat([ + defined: (path, options, print) => group(concat([ "defined?(", softline, indent(concat(path.map(print, "body"))), softline, ")" ])), - do_block: (path, print) => { + do_block: (path, options, print) => { const parts = ["do"]; if (path.getValue().body[0]) { @@ -251,26 +251,26 @@ const nodes = { return group(concat(parts)); }, - dot2: (path, print) => concat([ + dot2: (path, options, print) => concat([ path.call(print, "body", 0), "..", path.call(print, "body", 1) ]), - dot3: (path, print) => concat([ + dot3: (path, options, print) => concat([ path.call(print, "body", 0), "...", path.call(print, "body", 1) ]), - dyna_symbol: (path, print) => concat([ + dyna_symbol: (path, options, print) => concat([ ":\"", concat(path.map(print, "body")), "\"" ]), - else: (path, print) => concat([ + else: (path, options, print) => concat([ "else", indent(concat([softline, path.call(print, "body", 0)])) ]), - elsif: (path, print) => { + elsif: (path, options, print) => { const [_predicate, _statements, addition] = path.getValue().body; const parts = [ group(concat(["elsif ", path.call(print, "body", 0)])), @@ -283,18 +283,18 @@ const nodes = { return group(concat(parts)); }, - ensure: (path, print) => group(concat([ + ensure: (path, options, print) => group(concat([ "ensure", indent(concat([hardline, concat(path.map(print, "body"))])) ])), fcall: concatBody, - field: (path, print) => group(concat([ + field: (path, options, print) => group(concat([ path.call(print, "body", 0), path.getValue().body[1], path.call(print, "body", 2) ])), for: printFor, - hash: (path, print) => { + hash: (path, options, print) => { if (path.getValue().body[0] === null) { return '{}'; } @@ -308,7 +308,7 @@ const nodes = { if: printIf, ifop: printTernary, if_mod: printIf, - lambda: (path, print) => { + lambda: (path, options, print) => { const args = path.call(print, "body", 0); return group(ifBreak( @@ -328,19 +328,19 @@ const nodes = { )); }, kwrest_param: printKwargRestParam, - massign: (path, print) => group(concat([ + massign: (path, options, print) => group(concat([ path.call(print, "body", 0), " =", indent(concat([line, path.call(print, "body", 1)])) ])), - method_add_arg: (path, print) => { + method_add_arg: (path, options, print) => { if (path.getValue().body[1].type === "args_new") { return path.call(print, "body", 0); } return group(concat(path.map(print, "body"))); }, - method_add_block: (path, print) => join(" ", path.map(print, "body")), - mlhs_add: (path, print) => { + method_add_block: (path, options, print) => join(" ", path.map(print, "body")), + mlhs_add: (path, options, print) => { if (path.getValue().body[0].type === "mlhs_new") { return path.call(print, "body", 1); } @@ -352,33 +352,33 @@ const nodes = { path.call(print, "body", 1) ]); }, - mlhs_add_post: (path, print) => group(concat([ + mlhs_add_post: (path, options, print) => group(concat([ path.call(print, "body", 0), ",", group(concat([line, path.call(print, "body", 1)])) ])), - mlhs_add_star: (path, print) => group(concat([ + mlhs_add_star: (path, options, print) => group(concat([ path.call(print, "body", 0), ",", group(concat([line, "*", path.call(print, "body", 1)])) ])), - mrhs_add_star: (path, print) => group(concat([ + mrhs_add_star: (path, options, print) => group(concat([ "*", concat(path.map(print, "body")) ])), - mlhs_paren: (path, print) => group(concat([ + mlhs_paren: (path, options, print) => group(concat([ "(", indent(concat([softline, path.call(print, "body", 0)])), concat([softline, ")"]) ])), - mlhs_new: (path, print) => "", - mrhs_new_from_args: (path, print) => path.call(print, "body", 0), - module: (path, print) => group(concat([ + mlhs_new: (path, options, print) => "", + mrhs_new_from_args: (path, options, print) => path.call(print, "body", 0), + module: (path, options, print) => group(concat([ group(concat(["module ", path.call(print, "body", 0)])), indent(path.call(print, "body", 1)), dedent(concat([hardline, "end"])) ])), - mrhs_add: (path, print) => { + mrhs_add: (path, options, print) => { if (path.getValue().body[0].type === "mrhs_new") { return path.call(print, "body", 1); } @@ -390,21 +390,21 @@ const nodes = { path.call(print, "body", 1) ])); }, - mrhs_new: (path, print) => "", - next: (path, print) => { + mrhs_new: (path, options, print) => "", + next: (path, options, print) => { if (path.getValue().body.length > 0) { return concat(["next ", path.call(print, "body", 0)]); } return "next"; }, - opassign: (path, print) => group(concat([ + opassign: (path, options, print) => group(concat([ path.call(print, "body", 0), " ", path.call(print, "body", 1), indent(concat([line, path.call(print, "body", 2)])) ])), params: printParams, - paren: (path, print) => ( + paren: (path, options, print) => ( concat(["(", concat(path.getValue().body.reduce((parts, part, index) => { if (Array.isArray(part)) { return parts.concat(path.map(print, "body", index)); @@ -412,25 +412,25 @@ const nodes = { return [...parts, path.call(print, "body", index)]; }, [])), ")"]) ), - program: (path, print) => markAsRoot(concat([ + program: (path, options, print) => markAsRoot(concat([ join(literalline, path.map(print, "body")), literalline ])), - qsymbols_add: (path, print) => concat([ + qsymbols_add: (path, options, print) => concat([ path.call(print, "body", 0), path.getValue().body[0].type === "qsymbols_new" ? "" : line, path.call(print, "body", 1) ]), - qsymbols_new: (path, print) => group(concat(["%i[", softline])), - qwords_add: (path, print) => concat([ + qsymbols_new: (path, options, print) => group(concat(["%i[", softline])), + qwords_add: (path, options, print) => concat([ path.call(print, "body", 0), path.getValue().body[0].type === "qwords_new" ? "" : line, path.call(print, "body", 1) ]), - qwords_new: (path, print) => group(concat(["%w[", softline])), - redo: (path, print) => "redo", + qwords_new: (path, options, print) => group(concat(["%w[", softline])), + redo: (path, options, print) => "redo", regexp_add: concatBody, - regexp_literal: (path, print) => { + regexp_literal: (path, options, print) => { const delim = path.call(print, "body", 1); const delimPairs = { "]": "[", "}": "{", ")": "(" }; @@ -443,8 +443,8 @@ const nodes = { delim ])); }, - regexp_new: (path, print) => "", - rescue: (path, print) => { + regexp_new: (path, options, print) => "", + rescue: (path, options, print) => { const [exception, variable, _statements, addition] = path.getValue().body; const parts = ["rescue"]; @@ -465,17 +465,17 @@ const nodes = { return group(concat(parts)); }, - rescue_mod: (path, print) => group(join(" rescue ", path.map(print, "body"))), + rescue_mod: (path, options, print) => group(join(" rescue ", path.map(print, "body"))), rest_param: printRestParam, - retry: (path, print) => "retry", - return: (path, print) => group(concat(["return ", concat(path.map(print, "body"))])), - return0: (path, print) => "return", - sclass: (path, print) => group(concat([ + retry: (path, options, print) => "retry", + return: (path, options, print) => group(concat(["return ", concat(path.map(print, "body"))])), + return0: (path, options, print) => "return", + sclass: (path, options, print) => group(concat([ group(concat([hardline, "class << ", path.call(print, "body", 0)])), indent(path.call(print, "body", 1)), concat([hardline, "end"]) ])), - stmts_add: (path, print) => { + stmts_add: (path, options, print) => { const [leftLineNo, rightLineNo] = path.getValue().body.map(lineNoFrom); const buffer = (rightLineNo - leftLineNo) > 1 ? concat([hardline, hardline]) : hardline; @@ -484,23 +484,23 @@ const nodes = { } return group(join(buffer, path.map(print, "body"))); }, - stmts_new: (path, print) => "", - string_concat: (path, print) => group(concat([ + stmts_new: (path, options, print) => "", + string_concat: (path, options, print) => group(concat([ path.call(print, "body", 0), " \\", indent(concat([hardline, path.call(print, "body", 1)])) ])), - string_content: (path, print) => "", - string_add: (path, print) => [ + string_content: (path, options, print) => "", + string_add: (path, options, print) => [ ...path.call(print, "body", 0), path.call(print, "body", 1) ], - string_embexpr: (path, print) => concat([ + string_embexpr: (path, options, print) => concat([ "#{", path.call(print, "body", 0), "}" ]), - string_literal: (path, print) => { + string_literal: (path, options, print) => { const parts = path.call(print, "body", 0); if (parts === '') { return "''"; @@ -509,43 +509,43 @@ const nodes = { const delim = parts.some(part => part.parts && part.parts[0] === "#{") ? "\"" : "\'"; return concat([delim, ...parts, delim]); }, - super: (path, print) => group(concat([ + super: (path, options, print) => group(concat([ "super", concat(path.map(print, "body")) ])), - symbol: (path, print) => concat([ + symbol: (path, options, print) => concat([ ":", concat(path.map(print, "body")) ]), symbol_literal: concatBody, - symbols_add: (path, print) => concat([ + symbols_add: (path, options, print) => concat([ path.call(print, "body", 0), path.getValue().body[0].type === "symbols_new" ? "" : line, path.call(print, "body", 1) ]), - symbols_new: (path, print) => group(concat(["%I[", softline])), - top_const_field: (path, print) => group(concat([ + symbols_new: (path, options, print) => group(concat(["%I[", softline])), + top_const_field: (path, options, print) => group(concat([ "::", path.call(print, "body", 0) ])), - top_const_ref: (path, print) => group(concat([ + top_const_ref: (path, options, print) => group(concat([ "::", path.call(print, "body", 0) ])), - unary: (path, print) => concat([ + unary: (path, options, print) => concat([ path.getValue().body[0][0], path.call(print, "body", 1) ]), - undef: (path, print) => concat(["undef ", concat(path.map(print, "body", 0))]), + undef: (path, options, print) => concat(["undef ", concat(path.map(print, "body", 0))]), unless: printUnless, unless_mod: printUnless, until: printUntil, until_mod: printUntil, var_field: concatBody, - var_ref: (path, print) => path.call(print, "body", 0), + var_ref: (path, options, print) => path.call(print, "body", 0), vcall: concatBody, - void_stmt: (path, print) => "", - when: (path, print) => { + void_stmt: (path, options, print) => "", + when: (path, options, print) => { const [_predicates, _statements, addition] = path.getValue().body; const parts = [ group(concat(["when ", path.call(print, "body", 0)])), @@ -561,31 +561,31 @@ const nodes = { while: printWhile, while_mod: printWhile, word_add: concatBody, - word_new: (path, print) => "", - words_add: (path, print) => concat([ + word_new: (path, options, print) => "", + words_add: (path, options, print) => concat([ path.call(print, "body", 0), path.getValue().body[0].type === "words_new" ? "" : line, path.call(print, "body", 1) ]), - words_new: (path, print) => group(concat(["%W[", softline])), + words_new: (path, options, print) => group(concat(["%W[", softline])), xstring_add: concatBody, - xstring_literal: (path, print) => group(concat([ + xstring_literal: (path, options, print) => group(concat([ "%x[", softline, indent(concat(path.map(print, "body"))), softline, "]" ])), - xstring_new: (path, print) => "", - yield: (path, print) => concat([ + xstring_new: (path, options, print) => "", + yield: (path, options, print) => concat([ "yield ", concat(path.map(print, "body")) ]), - yield0: (path, print) => "yield", - zsuper: (path, print) => "super" + yield0: (path, options, print) => "yield", + zsuper: (path, options, print) => "super" }; -const debugNode = (path, print) => { +const debugNode = (path, options, print) => { console.log("=== UNSUPPORTED NODE ==="); console.log(JSON.stringify(path.getValue(), null, 2)); console.log("========================"); @@ -599,7 +599,7 @@ const genericPrint = (path, options, print) => { return body; } - return (nodes[type] || debugNode)(path, print); + return (nodes[type] || debugNode)(path, options, print); }; module.exports = genericPrint; diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 3d561751..910df9ff 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`array.rb matches expected output 1`] = ` +exports[`array.rb matches expected output for inline.json 1`] = ` "[] [1, 2, 3] @@ -19,7 +19,55 @@ exports[`array.rb matches expected output 1`] = ` " `; -exports[`assign.rb matches expected output 1`] = ` +exports[`array.rb matches expected output for noinline.json 1`] = ` +"[] + +[1, 2, 3] + +['a', 'b', 'c'] + +%w[a b c] + +%i[a b c] + +%W[a#{a}a b#{b}a c#{c}c] + +%I[a#{a}a b#{b}b c#{c}c] + +[1, 2, *[3, 4], 5, 6] +" +`; + +exports[`assign.rb matches expected output for inline.json 1`] = ` +"a = 1 + +b = + begin + 2 + end + +c, d, e = [1, 2, 3] + +f = 1, 2, 3 + +g, h, i = 1, 2, 3 + +j, *k = 1, 2, 3 + +l, *m, n, o = 1, 2, 3 + +q = *[1, 2, 3] + +(r, s), t = [1, 2], 3 + +u ||= 1 + +v ||= + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +" +`; + +exports[`assign.rb matches expected output for noinline.json 1`] = ` "a = 1 b = @@ -48,7 +96,7 @@ v ||= " `; -exports[`blocks.rb matches expected output 1`] = ` +exports[`blocks.rb matches expected output for inline.json 1`] = ` "loop { 1 } loop do @@ -65,7 +113,24 @@ end " `; -exports[`case.rb matches expected output 1`] = ` +exports[`blocks.rb matches expected output for noinline.json 1`] = ` +"loop { 1 } + +loop do + 1 +end + +[1, 2, 3].map do |i| + i * 2 +end + +[1, 2, 3].each do |i| + p i +end +" +`; + +exports[`case.rb matches expected output for inline.json 1`] = ` "case when a 1 @@ -97,7 +162,39 @@ end " `; -exports[`class.rb matches expected output 1`] = ` +exports[`case.rb matches expected output for noinline.json 1`] = ` +"case +when a + 1 +end + +case a +when b + 1 +end + +case a +when b, c + 1 +end + +case a +when b + 1 +when c + 2 +end + +case a +when b + 1 +else + 2 +end +" +`; + +exports[`class.rb matches expected output for inline.json 1`] = ` "module Prettier class Vehicle @@ -134,7 +231,44 @@ end " `; -exports[`hash.rb matches expected output 1`] = ` +exports[`class.rb matches expected output for noinline.json 1`] = ` +"module Prettier + + class Vehicle + + attr_accessor :wheels + + def initialize(wheels) + self.wheels = wheels + end + + def drive + @wheels + end + end + + class Car < Vehicle + WHEELS = 4 + + def initialize + super(WHEELS) + end + + def drive + super + end + end +end + +::Prettier::Vehicle.new(3) + +def Vehicle.drive + 'vroom' +end +" +`; + +exports[`hash.rb matches expected output for inline.json 1`] = ` "{} { a: 'a', b: 'b', c: 'c' } { a: 'a', b: 'b', c: 'c' } @@ -142,7 +276,15 @@ exports[`hash.rb matches expected output 1`] = ` " `; -exports[`if.rb matches expected output 1`] = ` +exports[`hash.rb matches expected output for noinline.json 1`] = ` +"{} +{ a: 'a', b: 'b', c: 'c' } +{ a: 'a', b: 'b', c: 'c' } +{ Foo => 1, Bar => 2 } +" +`; + +exports[`if.rb matches expected output for inline.json 1`] = ` "if a super_super_super_super_super_super_super_super_super_super_super_super_super_super_long end @@ -205,7 +347,86 @@ end " `; -exports[`kwargs.rb matches expected output 1`] = ` +exports[`if.rb matches expected output for noinline.json 1`] = ` +"if a + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +end + +if a + 1 +elsif b + 2 +end + +if a + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +else + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +end + +if a + 1 +elsif b + 2 +elsif c + 3 +else + 4 +end + +unless a + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +end + +unless a + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +else + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +end + +if a + 1 +end + +if super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + 1 +end + +unless a + 1 +end + +unless super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + 1 +end + +a ? 1 : 2 + +a ? 2 : 1 + +a ? 1 : 2 + +if a + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +else + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long +end +" +`; + +exports[`kwargs.rb matches expected output for inline.json 1`] = ` +"def foo(a:, b:, c: 1, d: 2) + a + b + c + d +end + +foo(a: 1, b: 2, c: 3, d: 4) + +hash = { a: 1, b: 2, c: 3 } +foo(**hash, d: 4) +" +`; + +exports[`kwargs.rb matches expected output for noinline.json 1`] = ` "def foo(a:, b:, c: 1, d: 2) a + b + c + d end @@ -217,7 +438,7 @@ foo(**hash, d: 4) " `; -exports[`lambda.rb matches expected output 1`] = ` +exports[`lambda.rb matches expected output for inline.json 1`] = ` "-> { 1 } a = -> { 1 } @@ -236,7 +457,26 @@ d = " `; -exports[`layout.rb matches expected output 1`] = ` +exports[`lambda.rb matches expected output for noinline.json 1`] = ` +"-> { 1 } + +a = -> { 1 } + +b = ->(a, b, c) { a + b + c } + +c = + lambda do + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + end + +d = + lambda do |a, b, c| + super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + end +" +`; + +exports[`layout.rb matches expected output for inline.json 1`] = ` "1.0 def foobar @@ -257,7 +497,28 @@ end " `; -exports[`method.rb matches expected output 1`] = ` +exports[`layout.rb matches expected output for noinline.json 1`] = ` +"1.0 + +def foobar + :a + + 'b' + 1 + 2 + + 3 +end + +2 +3 +4 + +5 +" +`; + +exports[`method.rb matches expected output for inline.json 1`] = ` "def foo(a, b, c = 1, d = 2, *e, f, g, h:, i:, j: 1, k: 2, **l, &block) 'what' end @@ -281,7 +542,31 @@ foo(h: 1, **bar, i: 2) " `; -exports[`numbers.rb matches expected output 1`] = ` +exports[`method.rb matches expected output for noinline.json 1`] = ` +"def foo(a, b, c = 1, d = 2, *e, f, g, h:, i:, j: 1, k: 2, **l, &block) + 'what' +end + +foo(1) +foo(1, 2) +foo(1, 2, *abc) +foo(1, 2, *abc, 3, 4) + +foo(*bar) +foo(**baz) +foo(&block) + +foo(*bar, &block) +foo(**baz, &block) +foo(*bar, **baz, &block) + +foo(h: 1, **bar) +foo(**bar, h: 1) +foo(h: 1, **bar, i: 2) +" +`; + +exports[`numbers.rb matches expected output for inline.json 1`] = ` "123 -123 1_123 @@ -300,7 +585,26 @@ exports[`numbers.rb matches expected output 1`] = ` " `; -exports[`regex.rb matches expected output 1`] = ` +exports[`numbers.rb matches expected output for noinline.json 1`] = ` +"123 +-123 +1_123 +-543 +123_456_789_123_456_789 +123.45 +1.2e-3 +0xaabb +0377 +-0b1010 +0b001_001 +?a +?\\\\C-a +?\\\\M-a +?\\\\M-\\\\C-a +" +`; + +exports[`regex.rb matches expected output for inline.json 1`] = ` "a = /abc/ b = %r{abc} @@ -319,7 +623,26 @@ h = %r{abc}i " `; -exports[`rescue.rb matches expected output 1`] = ` +exports[`regex.rb matches expected output for noinline.json 1`] = ` +"a = /abc/ + +b = %r{abc} + +c = /abc/ + +d = %r[abc] + +e = %r(abc) + +f = /a#{b}c/ + +g = /abc/i + +h = %r{abc}i +" +`; + +exports[`rescue.rb matches expected output for inline.json 1`] = ` "begin 1 rescue StandardError @@ -338,7 +661,47 @@ a rescue nil " `; -exports[`strings.rb matches expected output 1`] = ` +exports[`rescue.rb matches expected output for noinline.json 1`] = ` +"begin + 1 +rescue StandardError + retry +rescue NoMethodError => exception + redo +rescue + 2 +else + 3 +ensure + 4 +end + +a rescue nil +" +`; + +exports[`strings.rb matches expected output for inline.json 1`] = ` +"'' + +'abc' + +'abc' + +\\"#{abc}\\" + +\\"abc #{de} fghi #{jkl} mno\\" + +'abc' \\\\ + 'def' \\\\ + 'ghi' + +\\"abc #{'abc'}\\" +{ 'a' => 1 } +{ \\"a #{a}\\" => 1 } +" +`; + +exports[`strings.rb matches expected output for noinline.json 1`] = ` "'' 'abc' @@ -359,7 +722,7 @@ exports[`strings.rb matches expected output 1`] = ` " `; -exports[`while.rb matches expected output 1`] = ` +exports[`while.rb matches expected output for inline.json 1`] = ` "1 while true 1 while true @@ -385,3 +748,38 @@ until super_super_super_super_super_super_super_super_super_super_super_super_su end " `; + +exports[`while.rb matches expected output for noinline.json 1`] = ` +"while true + 1 +end + +while true + 1 +end + +while super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + 1 +end + +while super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + 1 +end + +until true + 1 +end + +until true + 1 +end + +until super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + 1 +end + +until super_super_super_super_super_super_super_super_super_super_super_super_super_super_long + 1 +end +" +`; diff --git a/test/config/inline.json b/test/config/inline.json new file mode 100644 index 00000000..8bea4e88 --- /dev/null +++ b/test/config/inline.json @@ -0,0 +1,4 @@ +{ + "inlineConditionals": true, + "inlineLoops": true +} diff --git a/test/config/noinline.json b/test/config/noinline.json new file mode 100644 index 00000000..4881491d --- /dev/null +++ b/test/config/noinline.json @@ -0,0 +1,4 @@ +{ + "inlineConditionals": false, + "inlineLoops": false +} diff --git a/test/index.test.js b/test/index.test.js index f069b960..dc0bbb40 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,21 +3,27 @@ const fs = require("fs"); const prettier = require("prettier"); -fs.readdirSync("./test").forEach(filename => { - if (!filename.match(/.+\.rb$/)) { - return; - } +fs.readdirSync("./test/config").forEach(configFilename => { + const config = JSON.parse(fs.readFileSync(`./test/config/${configFilename}`, "utf8")); - test(`${filename} matches expected output`, done => { - fs.readFile(`./test/${filename}`, "utf8", (error, code) => { - if (error) { - done.fail(error); - } + fs.readdirSync("./test").forEach(filename => { + if (!filename.match(/.+\.rb$/)) { + return; + } - const formatted = prettier.format(code, { parser: "ruby", plugins: ["."] }); - expect(formatted).toMatchSnapshot(); + test(`${filename} matches expected output for ${configFilename}`, done => { + fs.readFile(`./test/${filename}`, "utf8", (error, code) => { + if (error) { + done.fail(error); + } - done(); + const formatted = prettier.format(code, { + parser: "ruby", plugins: ["."], ...config + }); + expect(formatted).toMatchSnapshot(); + + done(); + }); }); }); });