Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Fix for prettier#803
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 28, 2021
1 parent 6543c09 commit 22136c5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

- [#799](https://github.com/prettier/plugin-ruby/issues/799) - jscheid, kddeisz - Multi-byte characters shouldn't give invalid source string bounds.
- [#801](https://github.com/prettier/plugin-ruby/issues/801) - jscheid, kddeisz - When converting a conditional to the modifier form, make sure to add parentheses if there is a chained method call.
- [#803](https://github.com/prettier/plugin-ruby/issues/803) - jscheid, kddeisz - Fix `formatWithCursor` support to match new parser format.

## [1.5.0] - 2021-01-21

Expand Down
8 changes: 7 additions & 1 deletion bin/print
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ if (fs.existsSync(process.argv[contentIdx])) {
content = process.argv.slice(contentIdx).join(" ").replace(/\\n/g, "\n");
}

console.log(prettier.format(content, { parser, plugins: ["."] }));
const { formatted } = prettier.formatWithCursor(content, {
parser,
plugins: ["."],
cursorOffset: 1
});

console.log(formatted);
69 changes: 36 additions & 33 deletions src/ruby/nodes/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,30 @@ function isSymbolArray(args) {
// 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
// expressions.
function printSpecialArrayWord(path, opts, print) {
function printArrayLiteralWord(path, opts, print) {
return concat(path.map(print, "body"));
}

// Prints out a special array literal. Accepts the parts of the array literal as
// an argument, where the first element of the parts array is a string that
// contains the special start.
function printSpecialArrayParts(parts) {
function printArrayLiteral(start, parts) {
return group(
concat([
parts[0],
start,
"[",
indent(concat([softline, join(line, parts.slice(1))])),
indent(concat([softline, join(line, parts)])),
concat([softline, "]"])
])
);
}

// Generates a print function with an embedded special start character for the
// specific type of array literal that we're dealing with. The print function
// returns an array as it expects to eventually be handed off to
// printSpecialArrayParts.
function printSpecialArray(start) {
return function printSpecialArrayWithStart(path, opts, print) {
return [start].concat(path.map(print, "body"));
};
}
const arrayLiteralStarts = {
qsymbols: "%i",
qwords: "%w",
symbols: "%I",
words: "%W"
};

// An array node is any literal array in Ruby. This includes all of the special
// array literals as well as regular arrays. If it is a special array literal
Expand All @@ -105,30 +102,40 @@ function printArray(path, opts, print) {
return printEmptyCollection(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 (opts.rubyArrayLiteral && isStringArray(args)) {
const printString = (stringPath) => stringPath.call(print, "body", 0);
const parts = path.map(printString, "body", 0, "body");
if (opts.rubyArrayLiteral) {
// 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)) {
const printString = (stringPath) => stringPath.call(print, "body", 0);
const parts = path.map(printString, "body", 0, "body");

return printSpecialArrayParts(["%w"].concat(parts));
}
return printArrayLiteral("%w", 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 (opts.rubyArrayLiteral && isSymbolArray(args)) {
const printSymbol = (symbolPath) => symbolPath.call(print, "body", 0);
const parts = path.map(printSymbol, "body", 0, "body");
// 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)) {
const printSymbol = (symbolPath) => symbolPath.call(print, "body", 0);
const parts = path.map(printSymbol, "body", 0, "body");

return printSpecialArrayParts(["%i"].concat(parts));
return printArrayLiteral("%i", parts);
}
}

// If we don't have a regular args node at this point then we have a special
// array literal. In that case we're going to print out the body (which will
// return to us an array with the first one being the start of the array) and
// send that over to the printSpecialArrayParts function.
// send that over to the printArrayLiteral function.
if (!["args", "args_add_star"].includes(args.type)) {
return printSpecialArrayParts(path.call(print, "body", 0));
return path.call(
(arrayPath) =>
printArrayLiteral(
arrayLiteralStarts[arrayPath.getValue().type],
arrayPath.map(print, "body")
),
"body",
0
);
}

// Here we have a normal array of any type of object with no special literal
Expand All @@ -151,9 +158,5 @@ function printArray(path, opts, print) {

module.exports = {
array: printArray,
qsymbols: printSpecialArray("%i"),
qwords: printSpecialArray("%w"),
symbols: printSpecialArray("%I"),
word: printSpecialArrayWord,
words: printSpecialArray("%W")
word: printArrayLiteralWord
};
2 changes: 1 addition & 1 deletion src/ruby/nodes/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function printCommand(path, opts, print) {
} else if (hasDef(path.getValue())) {
breakArgs = joinedArgs;
} else {
breakArgs = align(command.length + 1, joinedArgs);
breakArgs = align(docLength(command) + 1, joinedArgs);
}

return group(
Expand Down
11 changes: 8 additions & 3 deletions src/ruby/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const noComments = [
"args_add_star",
"mlhs",
"mlhs_add_post",
"mlhs_add_star"
"mlhs_add_star",
"mlhs_paren"
];

// Certain nodes are used more for organizational purposed than for actually
Expand Down Expand Up @@ -97,8 +98,12 @@ function getCommentChildNodes(node) {

return parts;
}
default:
return node.body;
default: {
if (Array.isArray(node.body)) {
return node.body.filter((child) => child && typeof child === "object");
}
return [];
}
}
}

Expand Down

0 comments on commit 22136c5

Please sign in to comment.