Skip to content

Commit 149e18c

Browse files
committed
Move addAlignmentToDoc() from printer to doc-builders
This addresses #1609 (comment)
1 parent db0ec7a commit 149e18c

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

src/doc-builders.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ function join(sep, arr) {
100100
return concat(res);
101101
}
102102

103+
function addAlignmentToDoc(doc, size, tabWidth) {
104+
let aligned = doc;
105+
if (size > 0) {
106+
// Use indent to add tabs for all the levels of tabs we need
107+
for (let i = 0; i < Math.floor(size / tabWidth); ++i) {
108+
aligned = indent(aligned);
109+
}
110+
// Use align for all the spaces that are needed
111+
aligned = align(size % tabWidth, aligned);
112+
// size is absolute from 0 and not relative to the current
113+
// indentation, so we use -Infinity to reset the indentation to 0
114+
aligned = align(-Infinity, aligned);
115+
}
116+
return aligned;
117+
}
118+
103119
module.exports = {
104120
concat,
105121
join,
@@ -115,5 +131,6 @@ module.exports = {
115131
breakParent,
116132
ifBreak,
117133
indent,
118-
align
134+
align,
135+
addAlignmentToDoc
119136
};

src/printer.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const fill = docBuilders.fill;
2121
const ifBreak = docBuilders.ifBreak;
2222
const breakParent = docBuilders.breakParent;
2323
const lineSuffixBoundary = docBuilders.lineSuffixBoundary;
24+
const addAlignmentToDoc = docBuilders.addAlignmentToDoc;
2425

2526
const docUtils = require("./doc-utils");
2627
const willBreak = docUtils.willBreak;
@@ -1668,7 +1669,11 @@ function genericPrintNoParens(path, options, print, args) {
16681669
size = util.getAlignmentSize(value, tabWidth, index + 1);
16691670
}
16701671

1671-
let aligned = addAlignmentToDoc(expressions[i], size, tabWidth);
1672+
let aligned = addAlignmentToDoc(
1673+
removeLines(expressions[i]),
1674+
size,
1675+
tabWidth
1676+
);
16721677

16731678
parts.push(
16741679
"${",
@@ -4246,26 +4251,14 @@ function printAstToDoc(ast, options, addAlignmentSize) {
42464251
if (addAlignmentSize > 0) {
42474252
// Add a hardline to make the indents take effect
42484253
// It should be removed in index.js format()
4249-
doc = addAlignmentToDoc(concat([hardline, doc]), addAlignmentSize, options.tabWidth);
4254+
doc = addAlignmentToDoc(
4255+
removeLines(concat([hardline, doc])),
4256+
addAlignmentSize,
4257+
options.tabWidth
4258+
);
42504259
}
42514260
docUtils.propagateBreaks(doc);
42524261
return doc;
42534262
}
42544263

4255-
function addAlignmentToDoc(doc, size, tabWidth) {
4256-
let aligned = removeLines(doc);
4257-
if (size > 0) {
4258-
// Use indent to add tabs for all the levels of tabs we need
4259-
for (let i = 0; i < Math.floor(size / tabWidth); ++i) {
4260-
aligned = indent(aligned);
4261-
}
4262-
// Use align for all the spaces that are needed
4263-
aligned = align(size % tabWidth, aligned);
4264-
// size is absolute from 0 and not relative to the current
4265-
// indentation, so we use -Infinity to reset the indentation to 0
4266-
aligned = align(-Infinity, aligned);
4267-
}
4268-
return aligned;
4269-
}
4270-
42714264
module.exports = { printAstToDoc };

0 commit comments

Comments
 (0)