Skip to content

Commit c8c59ba

Browse files
authored
Replace only spaces starting after newline to tabs (#712)
1 parent 0d9e469 commit c8c59ba

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

src/transforms/v2-to-v3/__fixtures__/misc/tabs.input.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import AWS from "aws-sdk";
44
const client = new AWS.DynamoDB({
55
accessKeyId: "KEY",
66
secretAccessKey: "SECRET"
7-
});
7+
});
8+
9+
// Spaces inside string should not be replaced.
10+
const stringWithFourSpaces = " ";

src/transforms/v2-to-v3/__fixtures__/misc/tabs.output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ const client = new DynamoDB({
66
accessKeyId: "KEY",
77
secretAccessKey: "SECRET"
88
}
9-
});
9+
});
10+
11+
// Spaces inside string should not be replaced.
12+
const stringWithFourSpaces = " ";

src/transforms/v2-to-v3/transformer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
IndentationType,
3636
getMostUsedIndentationType,
3737
getMostUsedStringLiteralQuote,
38+
getValueIndentedWithTabs,
3839
isTypeScriptFile,
3940
} from "./utils";
4041

@@ -116,7 +117,7 @@ const transformer = async (file: FileInfo, api: API) => {
116117

117118
if (useTabs) {
118119
// Refs: https://github.com/benjamn/recast/issues/315
119-
return sourceString.replace(/ {4,}/g, "\t");
120+
return getValueIndentedWithTabs(sourceString);
120121
}
121122

122123
return sourceString;

src/transforms/v2-to-v3/utils/getMostUsedIndentationType.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import { EOL } from "os";
2+
13
export enum IndentationType {
24
TAB = "tab",
35
SPACE = "space",
46
}
57

68
export const getMostUsedIndentationType = (source: string) => {
7-
const tabCount = (source.match(/\t/g) || []).length;
8-
const spaceCount = (source.match(/ {2}/g) || []).length;
9-
console.log({ tabCount, spaceCount });
9+
let tabCount = 0;
10+
let spaceCount = 0;
11+
12+
for (const line of source.split(EOL)) {
13+
if (line.startsWith(" ")) {
14+
spaceCount++;
15+
} else if (line.startsWith("\t")) {
16+
tabCount++;
17+
}
18+
}
1019

1120
if (tabCount > spaceCount) {
1221
return IndentationType.TAB;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { EOL } from "os";
2+
3+
const INDENTATION_REGEX = /^(\t*)( {4})/;
4+
5+
export const getValueIndentedWithTabs = (value: string) => {
6+
const lines: string[] = [];
7+
8+
for (const line of value.split(EOL)) {
9+
const match = line.match(INDENTATION_REGEX);
10+
if (match) {
11+
const numberOfTabs = match[1].length;
12+
const numberOfSpaces = match[2].length;
13+
const numberOfIndents = numberOfTabs + numberOfSpaces / 4;
14+
lines.push("\t".repeat(numberOfIndents) + line.trimLeft());
15+
} else {
16+
lines.push(line);
17+
}
18+
}
19+
20+
return lines.join(EOL);
21+
};

src/transforms/v2-to-v3/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from "./getClientDeepImportPath";
22
export * from "./getClientNewExpression";
33
export * from "./getMostUsedStringLiteralQuote";
44
export * from "./getMostUsedIndentationType";
5+
export * from "./getValueIndentedWithTabs";
56
export * from "./isTypeScriptFile";

0 commit comments

Comments
 (0)