Skip to content

Commit fbcd4fc

Browse files
Adding ValidateWhitespaceVisitor (#5734)
1 parent 492ba41 commit fbcd4fc

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

rewrite-javascript/rewrite/src/test/rewrite-test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {SnowflakeId} from "@akashrajpurohit/snowflake-id";
2525
import {mapAsync} from "../util";
2626
import {ParseErrorKind} from "../parse-error";
2727
import {MarkersKind, ParseExceptionResult} from "../markers";
28+
import {JavaScriptVisitor} from "../javascript";
29+
import {J} from "../java";
2830

2931
export interface SourceSpec<T extends SourceFile> {
3032
kind: string,
@@ -73,6 +75,7 @@ export class RecipeSpec {
7375
const specs = specsByKind[kind];
7476
const parsed = await this.parse(specs);
7577
await this.expectNoParseFailures(parsed);
78+
await this.expectWhitespaceNotToContainNonwhitespaceCharacters(parsed);
7679
this.checkParsePrintIdempotence && await this.expectParsePrintIdempotence(parsed);
7780
const changeset = (await scheduleRun(this.recipe,
7881
parsed.map(([_, sourceFile]) => sourceFile),
@@ -144,6 +147,7 @@ export class RecipeSpec {
144147

145148
private async expectAfter(spec: SourceSpec<any>, after?: SourceFile) {
146149
expect(after).toBeDefined();
150+
await new ValidateWhitespaceVisitor().visit(after!, this.executionContext);
147151
const actualAfter = await TreePrinters.print(after!);
148152
const afterSource = typeof spec.after === "function" ?
149153
(spec.after as (actual: string) => string)(actualAfter) : spec.after as string;
@@ -182,6 +186,21 @@ export class RecipeSpec {
182186
return [spec, sourceFile];
183187
});
184188
}
189+
190+
private async expectWhitespaceNotToContainNonwhitespaceCharacters(parsed: [SourceSpec<any>, SourceFile][]) {
191+
const validator = new ValidateWhitespaceVisitor();
192+
for (const [_, sourceFile] of parsed) {
193+
await validator.visit(sourceFile, this.executionContext);
194+
}
195+
}
196+
}
197+
198+
class ValidateWhitespaceVisitor extends JavaScriptVisitor<ExecutionContext> {
199+
protected override async visitSpace(space: J.Space, p: ExecutionContext): Promise<J.Space> {
200+
const ret = super.visitSpace(space, p);
201+
expect(space.whitespace).toMatch(/^\s*$/);
202+
return ret;
203+
}
185204
}
186205

187206
class NoopRecipe extends Recipe {

0 commit comments

Comments
 (0)