Skip to content

Commit 4cc8e27

Browse files
committed
Fix unindenting inconsistently indented lines cutting off strings
1 parent be86710 commit 4cc8e27

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

__tests__/utils/string.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('unindentText', () => {
100100
[' hello\n world', 'hello\nworld'],
101101
[' hello\n world', 'hello\nworld'],
102102
['\thello\n\tworld', 'hello\nworld'],
103-
['\thello\n\t world', 'hello\n world'],
103+
['\thello\n\t world\nyo', 'hello\n world\nyo'],
104104
['\t\thello\n\t\tworld', 'hello\nworld'],
105105
])('should unindent text by the given amount', (input, expected) => {
106106
expect(unindentText(input)).toBe(expected);

src/utils/string.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,14 @@ export function unindentText(text: string, indent: number = 0) {
126126
const firstLineIndent = lines[0].search(/\S/);
127127
const unindentAmount = indent > 0 ? indent : firstLineIndent;
128128

129-
return lines.map(line => line.slice(unindentAmount)).join('\n');
129+
// Only unindent the line if it has at least the same amount of indent as the first line.
130+
return lines.map(line => {
131+
const lineIndent = line.search(/\S/);
132+
133+
if (lineIndent >= unindentAmount) {
134+
return line.slice(unindentAmount);
135+
} else {
136+
return line;
137+
}
138+
}).join('\n');
130139
}

0 commit comments

Comments
 (0)