@prettier/plugin-ruby
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -9,2 +9,12 @@ # Changelog | ||
## [0.5.0] - 2019-02-13 | ||
### Added | ||
- Automatically convert arrays of all string literals to %w arrays. | ||
- Automatically convert arrays of all symbol literals to %i arrays. | ||
### Changed | ||
- [INTERNAL] Move the `args_add` and `args_new` handling into the parser. | ||
- Change `command_call` nodes to properly indent when broken and to not add a trailing comma. (Thanks to @uri for the report.) | ||
- Rename the `trailingComma` option to `addTrailingCommas` to not conflict with the JS option. | ||
## [0.4.1] - 2019-02-12 | ||
@@ -115,3 +125,4 @@ ### Changed | ||
[Unreleased]: https://github.com/CultureHQ/add-to-calendar/compare/v0.4.1...HEAD | ||
[Unreleased]: https://github.com/CultureHQ/add-to-calendar/compare/v0.5.0...HEAD | ||
[0.5.0]: https://github.com/CultureHQ/add-to-calendar/compare/v0.4.1...v0.5.0 | ||
[0.4.1]: https://github.com/CultureHQ/add-to-calendar/compare/v0.4.0...v0.4.1 | ||
@@ -118,0 +129,0 @@ [0.4.0]: https://github.com/CultureHQ/add-to-calendar/compare/v0.3.7...v0.4.0 |
{ | ||
"name": "@prettier/plugin-ruby", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "prettier plugin for the Ruby programming language", | ||
@@ -5,0 +5,0 @@ "main": "src/ruby.js", |
@@ -113,2 +113,3 @@ <div align="center"> | ||
| `tabWidth` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). | | ||
| `addTrailingCommas` | `false` | Adds a trailing comma to array literals, hash literals, and method calls. | | ||
| `inlineConditionals` | `true` | When it fits on one line, allows if and unless statements to use the modifier form. | | ||
@@ -118,3 +119,2 @@ | `inlineLoops` | `true` | When it fits on one line, allows while and until statements to use the modifier form. | | ||
| `preferSingleQuotes` | `true` | When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals. | | ||
| `trailingComma` | `false` | Adds a trailing comma to array literals, hash literals, and method calls. | | ||
@@ -121,0 +121,0 @@ ## Development |
174
src/nodes.js
@@ -19,21 +19,3 @@ const { align, breakParent, concat, dedent, dedentToRoot, group, hardline, ifBreak, indent, join, line, lineSuffix, literalline, markAsRoot, softline, trim } = require("prettier").doc.builders; | ||
}, | ||
aref: (path, opts, print) => { | ||
if (!path.getValue().body[1]) { | ||
return concat([path.call(print, "body", 0), "[]"]); | ||
} | ||
return group(concat([ | ||
path.call(print, "body", 0), | ||
"[", | ||
indent(concat([softline, path.call(print, "body", 1)])), | ||
concat([softline, "]"]) | ||
])); | ||
}, | ||
aref_field: (path, opts, print) => group(concat([ | ||
path.call(print, "body", 0), | ||
"[", | ||
indent(concat([softline, path.call(print, "body", 1)])), | ||
concat([softline, "]"]) | ||
])), | ||
arg_paren: (path, opts, print) => { | ||
arg_paren: (path, { addTrailingCommas }, print) => { | ||
if (path.getValue().body[0] === null) { | ||
@@ -45,45 +27,26 @@ return ""; | ||
"(", | ||
indent(concat([softline, path.call(print, "body", 0)])), | ||
indent(concat([ | ||
softline, | ||
join(concat([",", line]), path.call(print, "body", 0)), | ||
addTrailingCommas ? ifBreak(",", "") : "" | ||
])), | ||
concat([softline, ")"]) | ||
])) | ||
}, | ||
args_add: (path, opts, print) => { | ||
const [leftArg, rightArg] = path.getValue().body; | ||
args: makeList, | ||
args_add_block: (path, opts, print) => { | ||
const parts = path.call(print, "body", 0); | ||
if (leftArg.type === "args_new") { | ||
return path.call(print, "body", 1); | ||
} | ||
const buffer = skipAssignIndent(rightArg) ? ", " : concat([",", line]); | ||
return join(buffer, path.map(print, "body")); | ||
}, | ||
args_add_block: (path, { trailingComma }, print) => { | ||
const [args, block] = path.getValue().body; | ||
const parts = args.type === "args_new" ? [] : [path.call(print, "body", 0)]; | ||
if (block) { | ||
if (parts.length > 0) { | ||
parts.push(",", line); | ||
} | ||
if (path.getValue().body[1]) { | ||
parts.push(concat(["&", path.call(print, "body", 1)])); | ||
} | ||
return group(concat([ | ||
...parts, | ||
path.getParentNode().type !== "command" && trailingComma ? ifBreak(",", "") : "" | ||
])); | ||
return parts; | ||
}, | ||
args_add_star: (path, opts, print) => { | ||
if (path.getValue().body[0].type === "args_new") { | ||
return concat(["*", path.call(print, "body", 1)]); | ||
} | ||
const [before, star, ...after] = path.map(print, "body"); | ||
const parts = [...before, concat(["*", star]), ...after]; | ||
return concat([ | ||
path.call(print, "body", 0), | ||
",", | ||
line, | ||
concat(["*", path.call(print, "body", 1)]) | ||
]); | ||
return parts; | ||
}, | ||
args_new: (path, opts, print) => group(concat(["[", softline])), | ||
assoc_new: (path, { preferHashLabels }, print) => { | ||
@@ -191,15 +154,13 @@ const parts = []; | ||
break: (path, opts, print) => { | ||
const printed = path.call(print, "body", 0); | ||
const content = path.getValue().body[0]; | ||
if (path.getValue().body[0].body.length === 0) { | ||
if (content.body.length === 0) { | ||
return "break"; | ||
} | ||
const { contents: { parts: [first] } } = printed; | ||
if (first && !first.parts || (first.parts && first.parts[0] !== "(")) { | ||
return concat(["break ", printed]); | ||
if (content.body[0].body[0].type === "paren") { | ||
return concat(["break ", path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)]); | ||
} | ||
return concat(["break", printed]); | ||
return concat(["break ", join(", ", path.call(print, "body", 0))]); | ||
}, | ||
@@ -259,16 +220,25 @@ call: (path, opts, print) => { | ||
const command = path.call(print, "body", 0); | ||
const args = join(concat([",", line]), path.call(print, "body", 1)); | ||
// Hate, hate, hate this but can't figure out how to fix it. | ||
return group(ifBreak( | ||
concat([command, " ", align(command.length + 1, path.call(print, "body", 1))]), | ||
concat([command, " ", path.call(print, "body", 1)]) | ||
concat([command, " ", align(command.length + 1, args)]), | ||
concat([command, " ", args]) | ||
)); | ||
}, | ||
command_call: (path, opts, print) => group(concat([ | ||
path.call(print, "body", 0), | ||
makeCall(path, opts, print), | ||
path.call(print, "body", 2), | ||
" ", | ||
path.call(print, "body", 3) | ||
])), | ||
command_call: (path, opts, print) => { | ||
const parts = [ | ||
path.call(print, "body", 0), | ||
makeCall(path, opts, print), | ||
path.call(print, "body", 2), | ||
" " | ||
]; | ||
const args = join(concat([",", line]), path.call(print, "body", 3)); | ||
return group(ifBreak( | ||
concat([...parts, align(parts.reduce((sum, part) => sum + part.length, 0), args)]), | ||
concat([...parts, args]) | ||
)); | ||
}, | ||
const_path_field: (path, opts, print) => join("::", path.map(print, "body")), | ||
@@ -330,3 +300,3 @@ const_path_ref: (path, opts, print) => join("::", path.map(print, "body")), | ||
])), | ||
hash: (path, { trailingComma }, print) => { | ||
hash: (path, { addTrailingCommas }, print) => { | ||
if (path.getValue().body[0] === null) { | ||
@@ -341,3 +311,3 @@ return '{}'; | ||
concat(path.map(print, "body")), | ||
trailingComma ? ifBreak(",", "") : "", | ||
addTrailingCommas ? ifBreak(",", "") : "", | ||
])), | ||
@@ -413,3 +383,3 @@ concat([line, "}"]) | ||
concat([",", line]), | ||
path.map(print, "body") | ||
[...path.call(print, "body", 0), path.call(print, "body", 1)] | ||
)), | ||
@@ -438,12 +408,8 @@ module: (path, opts, print) => { | ||
if (path.getValue().body[0].type === "args_add") { | ||
return concat(["next ", path.call(print, "body", 0)]); | ||
if (args.body[0].type !== "paren") { | ||
return concat(["next ", join(",", path.call(print, "body", 0))]); | ||
} | ||
if (args.body[1].type !== "paren") { | ||
return concat(["next ", path.call(print, "body", 0)]); | ||
} | ||
// Ignoring the parens node and just going straight to the content | ||
return concat(["next ", path.call(print, "body", 0, "body", 0, "body", 1, "body", 0)]); | ||
return concat(["next ", path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)]); | ||
}, | ||
@@ -456,3 +422,15 @@ opassign: (path, opts, print) => group(concat([ | ||
])), | ||
paren: surround("(", ")"), | ||
paren: (path, opts, print) => { | ||
let content = path.call(print, "body", 0); | ||
if (["args", "args_add_star", "args_add_block"].includes(path.getValue().body[0].type)) { | ||
content = join(concat([",", line]), content); | ||
} | ||
return group(concat([ | ||
"(", | ||
indent(concat([softline, content])), | ||
concat([softline, ")"]) | ||
])); | ||
}, | ||
program: (path, opts, print) => markAsRoot(concat([ | ||
@@ -508,8 +486,8 @@ join(literalline, path.map(print, "body")), | ||
if (args.body[1] && args.body[1].type === "paren") { | ||
return concat(["return ", path.call(print, "body", 0, "body", 0, "body", 1, "body", 0)]); | ||
if (args.body[0] && args.body[0].type === "paren") { | ||
// Ignoring the parens node and just going straight to the content | ||
return concat(["return ", path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)]); | ||
} | ||
// Ignoring the parens node and just going straight to the content | ||
return concat(["return ", path.call(print, "body", 0)]); | ||
return concat(["return ", join(", ", path.call(print, "body", 0))]); | ||
}, | ||
@@ -549,9 +527,7 @@ return0: literal("return"), | ||
super: (path, opts, print) => { | ||
const buffer = path.getValue().body[0].type === "arg_paren" ? "": " "; | ||
if (path.getValue().body[0].type === "arg_paren") { | ||
return concat(["super", path.call(print, "body", 0)]); | ||
} | ||
return group(concat([ | ||
"super", | ||
buffer, | ||
path.call(print, "body", 0) | ||
])) | ||
return concat(["super ", join(", ", path.call(print, "body", 0))]); | ||
}, | ||
@@ -576,11 +552,11 @@ symbol: prefix(":"), | ||
var_ref: first, | ||
vcall: concatBody, | ||
vcall: first, | ||
when: (path, opts, print) => { | ||
const [_predicates, _statements, addition] = path.getValue().body; | ||
const printedStatements = path.call(print, "body", 1); | ||
const parts = [group(concat(["when ", path.call(print, "body", 0)]))]; | ||
const stmts = path.call(print, "body", 1); | ||
const parts = [group(concat(["when ", join(", ", path.call(print, "body", 0))]))]; | ||
if (!printedStatements.parts.every(part => !part)) { | ||
parts.push(indent(concat([hardline, printedStatements]))); | ||
if (!stmts.parts.every(part => !part)) { | ||
parts.push(indent(concat([hardline, stmts]))); | ||
} | ||
@@ -594,9 +570,11 @@ | ||
}, | ||
yield: (path, opts, print) => concat([ | ||
"yield", | ||
path.getValue().body[0].type === "paren" ? "" : " ", | ||
concat(path.map(print, "body")) | ||
]), | ||
yield: (path, opts, print) => { | ||
if (path.getValue().body[0].type === "paren") { | ||
return concat(["yield", path.call(print, "body", 0)]); | ||
} | ||
return concat(["yield ", join(", ", path.call(print, "body", 0))]); | ||
}, | ||
yield0: literal("yield"), | ||
zsuper: literal("super") | ||
}; |
const { concat, group, ifBreak, indent, join, line, softline } = require("prettier").doc.builders; | ||
const isStringArray = args => args.body.every(arg => ( | ||
arg.type === "string_literal" | ||
&& arg.body[0].body.length === 1 | ||
&& !arg.body[0].body[0].body.includes(" ") | ||
)); | ||
const isSymbolArray = args => args.body.every(arg => ( | ||
arg.type === "symbol_literal" | ||
)); | ||
const makeArray = start => (path, opts, print) => [start, ...path.map(print, "body")]; | ||
const getSpecialArrayParts = (path, print, args) => args.body.map((_arg, index) => ( | ||
path.call(print, "body", 0, "body", index, "body", 0, "body", 0) | ||
)); | ||
const printAref = (path, opts, print) => group(concat([ | ||
path.call(print, "body", 0), | ||
"[", | ||
indent(concat([ | ||
softline, | ||
join(concat([",", line]), path.call(print, "body", 1)) | ||
])), | ||
concat([softline, "]"]) | ||
])); | ||
const printSpecialArray = ([first, ...rest]) => group(concat([ | ||
first, | ||
"[", | ||
indent(concat([softline, join(line, rest)])), | ||
concat([softline, "]"]) | ||
])); | ||
module.exports = { | ||
array: (path, { trailingComma }, print) => { | ||
if (path.getValue().body[0] === null) { | ||
aref: (path, opts, print) => { | ||
if (!path.getValue().body[1]) { | ||
return concat([path.call(print, "body", 0), "[]"]); | ||
} | ||
return printAref(path, opts, print); | ||
}, | ||
aref_field: printAref, | ||
array: (path, { addTrailingCommas }, print) => { | ||
const args = path.getValue().body[0]; | ||
if (args === null) { | ||
return '[]'; | ||
} | ||
if (["args_add", "args_add_star"].includes(path.getValue().body[0].type)) { | ||
if (isStringArray(args)) { | ||
return printSpecialArray(["%w", ...getSpecialArrayParts(path, print, args)]); | ||
} | ||
if (isSymbolArray(args)) { | ||
return printSpecialArray(["%i", ...getSpecialArrayParts(path, print, args)]); | ||
} | ||
if (["args", "args_add_star"].includes(args.type)) { | ||
return group(concat([ | ||
@@ -16,4 +65,4 @@ "[", | ||
softline, | ||
path.call(print, "body", 0), | ||
trailingComma ? ifBreak(",", "") : "" | ||
join(concat([",", line]), path.call(print, "body", 0)), | ||
addTrailingCommas ? ifBreak(",", "") : "" | ||
])), | ||
@@ -24,9 +73,3 @@ concat([softline, "]"]) | ||
const [first, ...rest] = path.call(print, "body", 0); | ||
return group(concat([ | ||
first, | ||
"[", | ||
indent(concat([softline, join(line, rest)])), | ||
concat([softline, "]"]) | ||
])); | ||
return printSpecialArray(path.call(print, "body", 0)); | ||
}, | ||
@@ -33,0 +76,0 @@ qsymbols: makeArray("%i"), |
@@ -24,2 +24,8 @@ const parse = require("./parse"); | ||
options: { | ||
addTrailingCommas: { | ||
type: "boolean", | ||
category: "Global", | ||
default: false, | ||
description: "Adds a trailing comma to array literals, hash literals, and method calls." | ||
}, | ||
inlineConditionals: { | ||
@@ -48,8 +54,2 @@ type: "boolean", | ||
description: "When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals." | ||
}, | ||
trailingComma: { | ||
type: "boolean", | ||
category: "Global", | ||
default: false, | ||
description: "Adds a trailing comma to array literals, hash literals, and method calls." | ||
} | ||
@@ -56,0 +56,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
177706
1137
169459