diff --git a/openrewrite/src/java/tree/support_types.ts b/openrewrite/src/java/tree/support_types.ts index d1ab0bf..7dff1d5 100644 --- a/openrewrite/src/java/tree/support_types.ts +++ b/openrewrite/src/java/tree/support_types.ts @@ -113,10 +113,10 @@ export class Space { return new Space(comments, whitespace); } - static format(formatting: string, beginIndex: number, toIndex: number): Space { - if (beginIndex == toIndex) { + static format(formatting: string, beginIndex: number = 0, toIndex: number = formatting.length): Space { + if (beginIndex === toIndex) { return Space.EMPTY; - } else if (toIndex == beginIndex + 1 && formatting[beginIndex] === ' ') { + } else if (toIndex === beginIndex + 1 && formatting[beginIndex] === ' ') { return Space.SINGLE_SPACE; } @@ -131,7 +131,7 @@ export class Space { while (i < toIndex && (formatting[i] === ' ' || formatting[i] === '\t' || formatting[i] === '\n' || formatting[i] === '\r')) { i++; } - let whitespaceEnd = i; // Capture end of leading whitespace + let whitespaceEnd = i; // Capture end of leading whitespace // Step 2: Parse comments while (i < toIndex) { @@ -142,12 +142,12 @@ export class Space { commentStart = i + 2; // Skip the "//" i += 2; while (i < toIndex && formatting[i] !== '\n' && formatting[i] !== '\r') { - i++; // Continue until end of line or end of input + i++; // Continue until end of line or end of input } commentEnd = i; - suffixStart = i; // Capture newline as suffix - while (i < toIndex && (formatting[i] === '\n' || formatting[i] === '\r')) { - i++; + suffixStart = i; // Start of suffix + while (i < toIndex && (formatting[i] === ' ' || formatting[i] === '\t' || formatting[i] === '\n' || formatting[i] === '\r')) { + i++; // Capture any trailing whitespace after comment } const commentText = formatting.slice(commentStart, commentEnd); const suffix = formatting.slice(suffixStart, i); @@ -158,20 +158,20 @@ export class Space { commentStart = i + 2; // Skip the "/*" i += 2; while (i + 1 < toIndex && !(formatting[i] === '*' && formatting[i + 1] === '/')) { - i++; // Continue until "*/" or end of input + i++; // Continue until "*/" or end of input } commentEnd = i; // Position before */ - i += 2; // Skip the closing "*/" + i += 2; // Skip the closing "*/" suffixStart = i; while (i < toIndex && (formatting[i] === ' ' || formatting[i] === '\t' || formatting[i] === '\n' || formatting[i] === '\r')) { - i++; // Capture any trailing whitespace after comment + i++; // Capture any trailing whitespace after comment } const commentText = formatting.slice(commentStart, commentEnd); const suffix = formatting.slice(suffixStart, i); comments.push(new TextComment(true, commentText, suffix, Markers.EMPTY)); } else { - i++; // Skip non-comment characters + i++; // Skip non-comment characters } } diff --git a/openrewrite/test/.editorconfig b/openrewrite/test/.editorconfig index 25fd23d..a71b0e2 100644 --- a/openrewrite/test/.editorconfig +++ b/openrewrite/test/.editorconfig @@ -1,5 +1,11 @@ root = true +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +indent_style = space + [*.ts] indent_size = 4 ij_continuation_indent_size = 2 diff --git a/openrewrite/test/java/tree.test.ts b/openrewrite/test/java/tree.test.ts index 5c5ed02..a5b6757 100644 --- a/openrewrite/test/java/tree.test.ts +++ b/openrewrite/test/java/tree.test.ts @@ -1,23 +1,35 @@ -import {Space} from "../../src/java/tree"; +import {Space, TextComment} from "../../src/java/tree"; describe('Space parsing', () => { - test('parse space', () => { - let str = ' /* c1*/ /*c2 */ '; - console.log(Space.format(str, 0, str.length)); + test('multi-line comments', () => { + let space = Space.format(' /* c1*/ /*c2 */ '); + expect(space.whitespace).toBe(' '); + expect(space.comments).toHaveLength(2); + expect((space.comments[0] as TextComment).text).toBe(' c1'); + expect(space.comments[0].suffix).toBe(' '); + expect((space.comments[1] as TextComment).text).toBe('c2 '); + expect(space.comments[1].suffix).toBe(' '); }); - test('parse space 2', () => { - let str = ' // c1 \n // c2\n//c3'; - console.log(Space.format(str, 0, str.length)); + test('single-line comments', () => { + let space = Space.format(' // c1 \n // c2\n//c3'); + expect(space.whitespace).toBe(' '); + expect(space.comments).toHaveLength(3); + expect((space.comments[0] as TextComment).text).toBe(' c1 '); + expect(space.comments[0].suffix).toBe('\n '); + expect((space.comments[1] as TextComment).text).toBe(' c2'); + expect(space.comments[1].suffix).toBe('\n'); + expect((space.comments[2] as TextComment).text).toBe('c3'); + expect(space.comments[2].suffix).toBe(''); }); test('parse empty space', () => { - let str = ''; - console.log(Space.format(str, 0, str.length)); + let space = Space.format(''); + expect(space).toBe(Space.EMPTY); }); test('parse single space', () => { - let str = ' '; - console.log(Space.format(str, 0, str.length)); + let space = Space.format(' '); + expect(space).toBe(Space.SINGLE_SPACE); }); });