Skip to content

Commit

Permalink
Use same print width for range formatting as normal formatting. (#6050)
Browse files Browse the repository at this point in the history
* Use same print width for range formatting as normal formatting.

Fixes an issue were a file would end up formatted differently
with ranged formatting (--range-start & --range-end) versus
normal whole file formatting.

* Document range bugfix in changelog.
  • Loading branch information
mathieulj authored and j-f1 committed May 4, 2019
1 parent 84cc273 commit 26183e5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
38 changes: 38 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -42,6 +42,44 @@ Examples:
-->

- Range: Fix ranged formatting not using the correct line width ([#6050] by [@mathieulj])

<!-- prettier-ignore -->
```js
// Input
function f() {
if (true) {
call("this line is 79 chars", "long", "it should", "stay as single line");
}
}

// Output (Prettier stable run with --range-start 30 --range-end 110)
function f() {
if (true) {
call(
"this line is 79 chars",
"long",
"it should",
"stay as single line"
);
}
}

// Output (Prettier stable run without range)
function f() {
if (true) {
call("this line is 79 chars", "long", "it should", "stay as single line");
}
}

// Output (Prettier master with and without range)
function f() {
if (true) {
call("this line is 79 chars", "long", "it should", "stay as single line");
}
}
```

- JavaScript: Fix closure compiler typecasts ([#5947] by [@jridgewell])

If a closing parenthesis follows after a typecast in an inner expression, the typecast would wrap everything to the that following parenthesis.
Expand Down
1 change: 0 additions & 1 deletion src/main/core.js
Expand Up @@ -210,7 +210,6 @@ function formatRange(text, opts) {
Object.assign({}, opts, {
rangeStart: 0,
rangeEnd: Infinity,
printWidth: opts.printWidth - alignmentSize,
// track the cursor offset only if it's within our range
cursorOffset:
opts.cursorOffset >= rangeStart && opts.cursorOffset < rangeEnd
Expand Down
22 changes: 22 additions & 0 deletions tests/range/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -334,6 +334,28 @@ try {
================================================================================
`;
exports[`nested-print-width.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
function f() {
if (true) {
call("this line is 79 chars", "long", "it should", "stay as single line");
}
}
=====================================output=====================================
function f() {
if (true) {
call("this line is 79 chars", "long", "it should", "stay as single line");
}
}
================================================================================
`;
exports[`nested2.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
Expand Down
5 changes: 5 additions & 0 deletions tests/range/nested-print-width.js
@@ -0,0 +1,5 @@
function f() {
if (true) {<<<PRETTIER_RANGE_START>>>
call("this line is 79 chars", "long", "it should", "stay as single line");
<<<PRETTIER_RANGE_END>>>}
}

0 comments on commit 26183e5

Please sign in to comment.