diff --git a/src/ASTPrinter.test.ts b/src/ASTPrinter.test.ts index c411c9b..b36b1c9 100644 --- a/src/ASTPrinter.test.ts +++ b/src/ASTPrinter.test.ts @@ -45,14 +45,14 @@ describe("ASTPrinter", () => { } if (shouldInject) { const injectionBefore = `/* INJ_${injectId}_B */`; - codeWithInjections += injectionBefore; + codeWithInjections += " " + injectionBefore + " "; injectedStrings.push(injectionBefore); injectId++; } codeWithInjections += tok.lexeme; if (shouldInject) { const injectionAfter = `/* INJ_${injectId}_A */`; - codeWithInjections += injectionAfter; + codeWithInjections += " " + injectionAfter + " "; injectedStrings.push(injectionAfter); injectId++; } @@ -316,4 +316,54 @@ translate([-5, -5, -10]) cube([10, 10, 10]); `); }); + + test("It preserves comments in files doing division (harness test)", async () => { + doPreserveTest(`translate([distance1-distance1/2,8.5,0]);`); + }); + + it("doesn't break code with comment after ending brace", async () => { + const formatted = doFormat(` +{ +{ + } //end if +}`); + + let [ast, errorCollector] = ParsingHelper.parseFile( + new CodeFile("", formatted) + ); + errorCollector.throwIfAny(); + }); + + it("Adds newlines between stacked closing braces", async () => { + const formatted = doFormat(` +{ +{ + }}`); + expect(formatted).not.toContain("}}"); + }); + + it("does not add newlines near the else keyword in ifs", async () => { + const formatted = doFormat(` +if(true) { +} else { + +}`); + expect(formatted).toContain("} else {"); + }); + + it("doesn't break code with comment after ending brace of an else statement", async () => { + const formatted = doFormat(` +if(true) { +} // hello + else + // hello + { + +}`); + + let [ast, errorCollector] = ParsingHelper.parseFile( + new CodeFile("", formatted) + ); + errorCollector.throwIfAny(); + }); }); diff --git a/src/ASTPrinter.ts b/src/ASTPrinter.ts index eb31b95..8139c88 100644 --- a/src/ASTPrinter.ts +++ b/src/ASTPrinter.ts @@ -456,7 +456,10 @@ export default class ASTPrinter implements ASTVisitor { } source += this.stringifyExtraTokens(n.tokens.secondParen); source += ")"; - if (!(n.child instanceof NoopStmt) && !this.breakBetweenModuleInstantations) { + if ( + !(n.child instanceof NoopStmt) && + !this.breakBetweenModuleInstantations + ) { source += " "; } if (this.breakBetweenModuleInstantations) { @@ -467,8 +470,7 @@ export default class ASTPrinter implements ASTVisitor { c.firstModuleInstantation = false; } this.newLineAfterNextComment("breakBetweenModuleInstantations"); - source += - n.child.accept(c); + source += n.child.accept(c); } else { const c = this.copyWithBreakBetweenModuleInstantations(false); c.firstModuleInstantation = true; @@ -561,7 +563,7 @@ export default class ASTPrinter implements ASTVisitor { } source += "}"; if (!this.doNotAddNewlineAfterBlockStatement) { - source += this.newLine(false, "afterBlockStmt"); + this.newLineAfterNextComment("afterBlockStmt"); } return source; } diff --git a/src/Lexer.test.ts b/src/Lexer.test.ts index fea3fb5..8be5202 100644 --- a/src/Lexer.test.ts +++ b/src/Lexer.test.ts @@ -377,6 +377,27 @@ describe("Lexer", () => { expect(() => lexTokens(`use { + const tokens = lexTokens(` +{ +{ + } //end if +} + `); + expect(tokens.map((t) => t.type)).toEqual([ + TokenType.LeftBrace, + TokenType.LeftBrace, + TokenType.RightBrace, + TokenType.RightBrace, + TokenType.Eot, + ]); + expect(tokens[3].extraTokens).toHaveLength(2); + expect(tokens[3].extraTokens[0]).toBeInstanceOf(SingleLineComment); + expect((tokens[3].extraTokens[0] as SingleLineComment).contents).toEqual( + `end if` + ); + expect(tokens[3].extraTokens[1]).toBeInstanceOf(NewLineExtraToken); + }); it("does not add duplicate extraTokens", () => { const tokens = lexTokens(` module indented() { @@ -407,10 +428,10 @@ describe("Lexer", () => { it("generates start and and in spans", () => { const tokens = lexTokens(`b();`); // 5 spaces expect(tokens.length).not.toBe(0); - tokens.every(t => { + tokens.every((t) => { expect(t.span.start).toBeTruthy(); expect(t.span.end).toBeTruthy(); - }) + }); }); describe.skip("lexing of random files found on the internet", () => { async function lexFile(path: string) {