Skip to content

Commit e0596a2

Browse files
committed
Updated code format tests to be OS-agnostic
- Test results for formatCode.ts are now designed to match irrespective of the system's line separators: The previously used multi-line template strings created UNIX-style line separators, which do not match on Windows systems. Therefore, line endings are now explicitly written into the expected output string, unfortunately affecting readability. - Inside `formatCode`, input strings are now split into lines by any valid line separator. Previously, only the system's line separators have been considered, which may produce some unexpected results when receiving line endings from other systems.
1 parent af01569 commit e0596a2

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

src/utils/formatCode.spec.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {EOL} from 'os';
12
import { formatCode } from './formatCode';
23

34
const input1 = `{ foo: true }`;
@@ -13,20 +14,14 @@ foo: true,
1314
bar: 123
1415
}`;
1516

16-
const output3 = `{
17-
\tfoo: true,
18-
\tbar: 123
19-
}`;
17+
const output3 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`;
2018

2119
const input4 = `{
2220
\t\t\t\tfoo: true,
2321
\t\t\t\tbar: 123
2422
}`;
2523

26-
const output4 = `{
27-
\tfoo: true,
28-
\tbar: 123
29-
}`;
24+
const output4 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`;
3025

3126
describe('format', () => {
3227
it('should produce correct result', () => {

src/utils/formatCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EOL } from 'os';
22

33
export const formatCode = (s: string): string => {
44
let indent: number = 0;
5-
let lines = s.split(EOL);
5+
let lines = s.split(/[\r\n]+/);
66
lines = lines.map(line => {
77
line = line.trim().replace(/^\*/g, ' *');
88
let i = indent;

0 commit comments

Comments
 (0)