From e0596a22a0a0525507235759da27909f7484c643 Mon Sep 17 00:00:00 2001 From: Cajetan Grill <99743209+krokettenkoal@users.noreply.github.com> Date: Fri, 28 Jul 2023 11:08:46 +0200 Subject: [PATCH] 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. --- src/utils/formatCode.spec.ts | 11 +++-------- src/utils/formatCode.ts | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/utils/formatCode.spec.ts b/src/utils/formatCode.spec.ts index df46263b..49a4665f 100644 --- a/src/utils/formatCode.spec.ts +++ b/src/utils/formatCode.spec.ts @@ -1,3 +1,4 @@ +import {EOL} from 'os'; import { formatCode } from './formatCode'; const input1 = `{ foo: true }`; @@ -13,20 +14,14 @@ foo: true, bar: 123 }`; -const output3 = `{ -\tfoo: true, -\tbar: 123 -}`; +const output3 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`; const input4 = `{ \t\t\t\tfoo: true, \t\t\t\tbar: 123 }`; -const output4 = `{ -\tfoo: true, -\tbar: 123 -}`; +const output4 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`; describe('format', () => { it('should produce correct result', () => { diff --git a/src/utils/formatCode.ts b/src/utils/formatCode.ts index 42cfd5cf..9d0e8e69 100644 --- a/src/utils/formatCode.ts +++ b/src/utils/formatCode.ts @@ -2,7 +2,7 @@ import { EOL } from 'os'; export const formatCode = (s: string): string => { let indent: number = 0; - let lines = s.split(EOL); + let lines = s.split(/[\r\n]+/); lines = lines.map(line => { line = line.trim().replace(/^\*/g, ' *'); let i = indent;