dist/ruby/nodes/patterns.js in prettier-2.0.0.pre.rc4 vs dist/ruby/nodes/patterns.js in prettier-2.0.0
- old
+ new
@@ -17,28 +17,40 @@
}
return path.call(print);
};
const printAryPtn = (path, opts, print) => {
const [constant, preargs, splatarg, postargs] = path.getValue().body;
- let args = [];
+ let argDocs = [];
if (preargs) {
- args = args.concat(path.map((argPath) => printPatternArg(argPath, opts, print), "body", 1));
+ argDocs = argDocs.concat(path.map((argPath) => printPatternArg(argPath, opts, print), "body", 1));
}
if (splatarg) {
- args.push(["*", path.call(print, "body", 2)]);
+ argDocs.push(["*", path.call(print, "body", 2)]);
}
if (postargs) {
- args = args.concat(path.map(print, "body", 3));
+ argDocs = argDocs.concat(path.map(print, "body", 3));
}
- args = group(join([",", line], args));
- if (constant || patterns.includes(path.getParentNode().type)) {
- args = ["[", args, "]"];
+ let argDoc = group(join([",", line], argDocs));
+ // There are a couple of cases where we _must_ use brackets. They include:
+ //
+ // * When the number of arguments inside the array pattern is one 1, then we
+ // have to include them, otherwise it matches the whole array. Consider the
+ // difference between `in [elem]` and `in elem`.
+ // * If we have a wrapping constant, then we definitely need the brackets.
+ // Consider the difference between `in Const[elem]` and `in Const elem`
+ // * If we're nested inside a parent pattern, then we have to have brackets.
+ // Consider the difference between `in key: first, second` and
+ // `in key: [first, second]`.
+ if (argDocs.length === 1 ||
+ constant ||
+ patterns.includes(path.getParentNode().type)) {
+ argDoc = ["[", argDoc, "]"];
}
if (constant) {
- return [path.call(print, "body", 0), args];
+ return [path.call(print, "body", 0), argDoc];
}
- return args;
+ return argDoc;
};
exports.printAryPtn = printAryPtn;
const printFndPtn = (path, opts, print) => {
const [constant] = path.getValue().body;
const docs = [
@@ -84,12 +96,13 @@
}
return args;
};
exports.printHshPtn = printHshPtn;
const printIn = (path, opts, print) => {
+ const keyword = "in ";
const parts = [
- "in ",
- align("in ".length, path.call((valuePath) => printPatternArg(valuePath, opts, print), "body", 0)),
+ keyword,
+ align(keyword.length, path.call((valuePath) => printPatternArg(valuePath, opts, print), "body", 0)),
indent([hardline, path.call(print, "body", 1)])
];
if (path.getValue().body[2]) {
parts.push(hardline, path.call(print, "body", 2));
}