Skip to content

Commit c1a61eb

Browse files
committed
Hackily fix indentation of range formatting
See #1609 (comment) Also update the snapshot to reflect that the indentation actually should decrease by one space, since there were 13 spaces in the input and we round down after dividing by tabWidth.
1 parent 91372be commit c1a61eb

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ function format(text, opts, addIndents) {
7373
opts.newLine = guessLineEnding(text);
7474
const str = printDocToString(doc, opts);
7575
ensureAllCommentsPrinted(astComments);
76-
// Remove added indentation after last newline
76+
// Remove extra leading newline as well as the added indentation after last newline
7777
if (addIndents > 0) {
78-
return str.trimRight() + opts.newLine;
78+
return str.slice(opts.newLine.length).trimRight() + opts.newLine;
7979
}
8080
return str;
8181
}

src/printer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4262,7 +4262,12 @@ function printAstToDoc(ast, options, addIndents) {
42624262

42634263
const doc = printGenerically(FastPath.from(ast));
42644264
docUtils.propagateBreaks(doc);
4265-
return addIndentsToDoc(doc, addIndents);
4265+
if (addIndents > 0) {
4266+
// Add a hardline to make the indents take effect
4267+
// It should be removed in index.js format()
4268+
return addIndentsToDoc(concat([hardline, doc]), addIndents);
4269+
}
4270+
return doc;
42664271
}
42674272

42684273
function addIndentsToDoc(doc, numIndents) {

tests/range/__snapshots__/jsfmt.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function ugly ( {a=1, b = 2 } ) {
1414
function ugly ( {a=1, b = 2 } ) {
1515
function ugly ( {a=1, b = 2 } ) {
1616
function ugly ( {a=1, b = 2 } ) {
17-
\`multiline template string
17+
\`multiline template string
1818
with too much indentation\`;
1919
// The [165, 246) range selects the above two lines, including the second newline
2020
}

0 commit comments

Comments
 (0)