Skip to content

Commit e1c993e

Browse files

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

index.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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;
67
const printDocToString = require("./src/doc-printer").printDocToString;
78
const normalizeOptions = require("./src/options").normalize;
89
const parser = require("./src/parser");
@@ -62,7 +63,7 @@ function format(text, opts, addIndents) {
6263
// so this is just to get the non-range tests passing for now.
6364
if (opts.rangeEnd !== Infinity && (0 < rangeStart || rangeEnd < text.length)) {
6465
const rangeString = text.substring(rangeStart, rangeEnd)
65-
const numIndents = countIndents(rangeString, opts);
66+
const numIndents = Math.floor(getAlignmentSize(rangeString.slice(0, rangeString.search(/[^ \t]/)), opts.tabWidth) / opts.tabWidth);
6667

6768
const rangeFormatted = format(rangeString, Object.assign({}, opts, {
6869
rangeStart: 0,
@@ -86,13 +87,6 @@ function format(text, opts, addIndents) {
8687
return str;
8788
}
8889

89-
function countIndents(text, opts) {
90-
const tabSpaces = ' '.repeat(opts.tabWidth);
91-
const indentation = text.match(/^([ \t]*)/)[1].replace(/\t/g, tabSpaces);
92-
const numIndents = Math.floor(indentation.length / opts.tabWidth);
93-
return numIndents;
94-
}
95-
9690
function formatWithShebang(text, opts) {
9791
if (!text.startsWith("#!")) {
9892
return format(text, opts);

src/printer.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ 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+
5271
function genericPrint(path, options, printPath, args) {
5372
assert.ok(path instanceof FastPath);
5473

@@ -1662,17 +1681,7 @@ function genericPrintNoParens(path, options, print, args) {
16621681
const index = value.lastIndexOf('\n');
16631682
const tabWidth = options.tabWidth;
16641683
if (index !== -1) {
1665-
for (let i = index + 1; i < value.length; ++i) {
1666-
if (value[i] === '\t') {
1667-
// Tabs behave in a way that they are aligned to the nearest
1668-
// multiple of tabWidth:
1669-
// 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
1670-
// 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
1671-
size = size + tabWidth - size % tabWidth;
1672-
} else {
1673-
size++;
1674-
}
1675-
}
1684+
size = getAlignmentSize(value, tabWidth, index + 1);
16761685
}
16771686

16781687
let aligned = removeLines(expressions[i]);
@@ -4273,4 +4282,4 @@ function addIndentsToDoc(doc, numIndents) {
42734282
return addIndentsToDoc(indent(doc), numIndents - 1);
42744283
}
42754284

4276-
module.exports = { printAstToDoc };
4285+
module.exports = { printAstToDoc, getAlignmentSize };

0 commit comments

Comments
 (0)