Skip to content

Commit

Permalink
Fix up backtick breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jul 18, 2019
1 parent 253a5e0 commit 7118098
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

### Changed

- If xstring literals (command line calls surrounded by backticks) break, then we indent and place the command on a new line. Previously, this was resulting in new lines getting added each time the code was formatted. Now this happens correctly. (Thanks to @dudeofawesome for the report.)

## [0.14.0] - 2019-07-17

### Added
Expand Down
20 changes: 16 additions & 4 deletions src/nodes/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,24 @@ module.exports = {
word_add: concatBody,
word_new: empty,
xstring: makeList,
xstring_literal: (path, opts, print) =>
group(
xstring_literal: (path, opts, print) => {
const parts = path.call(print, "body", 0);

if (typeof parts[0] === "string") {
parts[0] = parts[0].trimStart();
}

const lastIndex = parts.length - 1;
if (typeof parts[lastIndex] === "string") {
parts[lastIndex] = parts[lastIndex].trimEnd();
}

return group(
concat([
"`",
indent(concat([softline, join(softline, path.call(print, "body", 0))])),
indent(concat([softline, join(softline, parts)])),
concat([softline, "`"])
])
)
);
}
};
3 changes: 3 additions & 0 deletions test/js/strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ describe("strings", () => {

test("breaking %x literals with method chains", () =>
expect(`%x[${long}].to_s`).toChangeFormat(`\`\n ${long}\n\`.to_s`));

test("trims the start and end appropriately", () =>
expect(`\`\n\n${long}\n\n\``).toChangeFormat(`\`\n ${long}\n\``));
});

describe("dynamic symbols", () => {
Expand Down

0 comments on commit 7118098

Please sign in to comment.