Skip to content

Commit 77a8fb0

Browse files
committed
1 parent e1c993e commit 77a8fb0

3 files changed

Lines changed: 26 additions & 28 deletions

File tree

index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ function ensureAllCommentsPrinted(astComments) {
4949
});
5050
}
5151

52-
function format(text, opts, addIndents) {
53-
addIndents = addIndents || 0;
52+
function format(text, opts, addAlignmentSize) {
53+
addAlignmentSize = addAlignmentSize || 0;
5454

5555
const rangeStart = text.lastIndexOf('\n', opts.rangeStart) + 1;
5656
// Use `text.length - 1` as the maximum since `indexOf` returns -1 if `fromIndex >= text.length`
@@ -63,25 +63,25 @@ function format(text, opts, addIndents) {
6363
// so this is just to get the non-range tests passing for now.
6464
if (opts.rangeEnd !== Infinity && (0 < rangeStart || rangeEnd < text.length)) {
6565
const rangeString = text.substring(rangeStart, rangeEnd)
66-
const numIndents = Math.floor(getAlignmentSize(rangeString.slice(0, rangeString.search(/[^ \t]/)), opts.tabWidth) / opts.tabWidth);
66+
const alignmentSize = getAlignmentSize(rangeString.slice(0, rangeString.search(/[^ \t]/)), opts.tabWidth);
6767

6868
const rangeFormatted = format(rangeString, Object.assign({}, opts, {
6969
rangeStart: 0,
7070
rangeEnd: Infinity,
71-
printWidth: opts.printWidth - numIndents * opts.tabWidth
72-
}), numIndents);
71+
printWidth: opts.printWidth - alignmentSize
72+
}), alignmentSize);
7373

7474
return text.slice(0, rangeStart) + rangeFormatted + text.slice(rangeEnd)
7575
}
7676

7777
const ast = parser.parse(text, opts);
7878
const astComments = attachComments(text, ast, opts);
79-
const doc = printAstToDoc(ast, opts, addIndents)
79+
const doc = printAstToDoc(ast, opts, addAlignmentSize)
8080
opts.newLine = guessLineEnding(text);
8181
const str = printDocToString(doc, opts);
8282
ensureAllCommentsPrinted(astComments);
8383
// Remove extra leading newline as well as the added indentation after last newline
84-
if (addIndents > 0) {
84+
if (addAlignmentSize > 0) {
8585
return str.slice(opts.newLine.length).trimRight() + opts.newLine;
8686
}
8787
return str;

src/printer.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,18 +1684,7 @@ function genericPrintNoParens(path, options, print, args) {
16841684
size = getAlignmentSize(value, tabWidth, index + 1);
16851685
}
16861686

1687-
let aligned = removeLines(expressions[i]);
1688-
if (size > 0) {
1689-
// Use indent to add tabs for all the levels of tabs we need
1690-
for (var i = 0; i < Math.floor(size / tabWidth); ++i) {
1691-
aligned = indent(aligned);
1692-
}
1693-
// Use align for all the spaces that are needed
1694-
aligned = align(size % tabWidth, aligned);
1695-
// size is absolute from 0 and not relative to the current
1696-
// indentation, so we use -Infinity to reset the indentation to 0
1697-
aligned = align(-Infinity, aligned);
1698-
}
1687+
let aligned = addAlignmentToDoc(expressions[i], size, tabWidth);
16991688

17001689
parts.push(
17011690
"${",
@@ -4253,8 +4242,8 @@ function removeLines(doc) {
42534242
});
42544243
}
42554244

4256-
function printAstToDoc(ast, options, addIndents) {
4257-
addIndents = addIndents || 0;
4245+
function printAstToDoc(ast, options, addAlignmentSize) {
4246+
addAlignmentSize = addAlignmentSize || 0;
42584247

42594248
function printGenerically(path, args) {
42604249
return comments.printComments(
@@ -4266,20 +4255,29 @@ function printAstToDoc(ast, options, addIndents) {
42664255
}
42674256

42684257
let doc = printGenerically(FastPath.from(ast));
4269-
if (addIndents > 0) {
4258+
if (addAlignmentSize > 0) {
42704259
// Add a hardline to make the indents take effect
42714260
// It should be removed in index.js format()
4272-
doc = addIndentsToDoc(concat([hardline, doc]), addIndents);
4261+
doc = addAlignmentToDoc(concat([hardline, doc]), addAlignmentSize, options.tabWidth);
42734262
}
42744263
docUtils.propagateBreaks(doc);
42754264
return doc;
42764265
}
42774266

4278-
function addIndentsToDoc(doc, numIndents) {
4279-
if (numIndents <= 0) {
4280-
return doc;
4267+
function addAlignmentToDoc(doc, size, tabWidth) {
4268+
let aligned = removeLines(doc);
4269+
if (size > 0) {
4270+
// Use indent to add tabs for all the levels of tabs we need
4271+
for (var i = 0; i < Math.floor(size / tabWidth); ++i) {
4272+
aligned = indent(aligned);
4273+
}
4274+
// Use align for all the spaces that are needed
4275+
aligned = align(size % tabWidth, aligned);
4276+
// size is absolute from 0 and not relative to the current
4277+
// indentation, so we use -Infinity to reset the indentation to 0
4278+
aligned = align(-Infinity, aligned);
42814279
}
4282-
return addIndentsToDoc(indent(doc), numIndents - 1);
4280+
return aligned;
42834281
}
42844282

42854283
module.exports = { printAstToDoc, getAlignmentSize };

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)