Skip to content

Commit db0ec7a

Browse files
committed
Move getAlignmentSize() from printer to util
This addresses #1609 (comment)
1 parent 990a18c commit db0ec7a

3 files changed

Lines changed: 24 additions & 23 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const comments = require("./src/comments");
44
const version = require("./package.json").version;
55
const printAstToDoc = require("./src/printer").printAstToDoc;
6-
const getAlignmentSize = require("./src/printer").getAlignmentSize;
6+
const getAlignmentSize = require("./src/util").getAlignmentSize;
77
const printDocToString = require("./src/doc-printer").printDocToString;
88
const normalizeOptions = require("./src/options").normalize;
99
const parser = require("./src/parser");

src/printer.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,6 @@ function shouldPrintComma(options, level) {
4949
}
5050
}
5151

52-
function getAlignmentSize(value, tabWidth, startIndex) {
53-
startIndex = startIndex || 0;
54-
55-
let size = 0;
56-
for (let i = startIndex; i < value.length; ++i) {
57-
if (value[i] === '\t') {
58-
// Tabs behave in a way that they are aligned to the nearest
59-
// multiple of tabWidth:
60-
// 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
61-
// 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
62-
size = size + tabWidth - size % tabWidth;
63-
} else {
64-
size++;
65-
}
66-
}
67-
68-
return size;
69-
}
70-
7152
function genericPrint(path, options, printPath, args) {
7253
assert.ok(path instanceof FastPath);
7354

@@ -1684,7 +1665,7 @@ function genericPrintNoParens(path, options, print, args) {
16841665
const index = value.lastIndexOf('\n');
16851666
const tabWidth = options.tabWidth;
16861667
if (index !== -1) {
1687-
size = getAlignmentSize(value, tabWidth, index + 1);
1668+
size = util.getAlignmentSize(value, tabWidth, index + 1);
16881669
}
16891670

16901671
let aligned = addAlignmentToDoc(expressions[i], size, tabWidth);
@@ -4287,4 +4268,4 @@ function addAlignmentToDoc(doc, size, tabWidth) {
42874268
return aligned;
42884269
}
42894270

4290-
module.exports = { printAstToDoc, getAlignmentSize };
4271+
module.exports = { printAstToDoc };

src/util.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,25 @@ function isBlockComment(comment) {
320320
return comment.type === "Block" || comment.type === "CommentBlock";
321321
}
322322

323+
function getAlignmentSize(value, tabWidth, startIndex) {
324+
startIndex = startIndex || 0;
325+
326+
let size = 0;
327+
for (let i = startIndex; i < value.length; ++i) {
328+
if (value[i] === '\t') {
329+
// Tabs behave in a way that they are aligned to the nearest
330+
// multiple of tabWidth:
331+
// 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
332+
// 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
333+
size = size + tabWidth - size % tabWidth;
334+
} else {
335+
size++;
336+
}
337+
}
338+
339+
return size;
340+
}
341+
323342
module.exports = {
324343
getPrecedence,
325344
isExportDeclaration,
@@ -341,5 +360,6 @@ module.exports = {
341360
setLocEnd,
342361
startsWithNoLookaheadToken,
343362
hasBlockComments,
344-
isBlockComment
363+
isBlockComment,
364+
getAlignmentSize
345365
};

0 commit comments

Comments
 (0)