Skip to content

Commit 0777942

Browse files
Fixing spaces for type declarations in Javascript Autoformatting (#5486)
* Fixing spaces for type declarations * Fixing spaces for type declarations
1 parent 9730e52 commit 0777942

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

rewrite-javascript/rewrite/src/javascript/format.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ export class SpacesVisitor<P> extends JavaScriptVisitor<P> {
342342
});
343343
}
344344

345+
protected async visitTypeDeclaration(typeDeclaration: JS.TypeDeclaration, p: P): Promise<J | undefined> {
346+
const ret = await super.visitTypeDeclaration(typeDeclaration, p) as JS.TypeDeclaration;
347+
return produce(ret, draft => {
348+
if (draft.modifiers.length > 0) {
349+
draft.name.before.whitespace = " ";
350+
}
351+
draft.name.element.prefix.whitespace = " ";
352+
draft.initializer.before.whitespace = this.style.aroundOperators.assignment ? " " : "";
353+
draft.initializer.element.prefix.whitespace = this.style.aroundOperators.assignment ? " " : "";
354+
});
355+
}
356+
345357
protected async visitTypeInfo(typeInfo: JS.TypeInfo, p: P): Promise<J | undefined> {
346358
const ret = await super.visitTypeInfo(typeInfo, p) as JS.TypeInfo;
347359
return produceAsync(ret, async draft => {
@@ -794,6 +806,16 @@ export class MinimumViableSpacingVisitor<P> extends JavaScriptVisitor<P> {
794806
});
795807
}
796808

809+
protected async visitTypeDeclaration(typeDeclaration: JS.TypeDeclaration, p: P): Promise<J | undefined> {
810+
const ret = await super.visitTypeDeclaration(typeDeclaration, p) as JS.TypeDeclaration;
811+
return produce(ret, draft => {
812+
if (draft.modifiers.length > 0) {
813+
draft.name.before.whitespace = " ";
814+
}
815+
draft.name.element.prefix.whitespace = " ";
816+
});
817+
}
818+
797819
protected async visitVariableDeclarations(v: J.VariableDeclarations, p: P): Promise<J | undefined> {
798820
let ret = await super.visitVariableDeclarations(v, p) as J.VariableDeclarations;
799821
let first = ret.leadingAnnotations.length === 0;

rewrite-javascript/rewrite/test/javascript/format/format.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ describe('AutoformatVisitor', () => {
2727
// @formatter:off
2828
//language=typescript
2929
typescript(`
30+
type T1=string;
31+
export type T2 = string;
3032
class L {}
3133
class K extends L{
3234
constructor ( ){
@@ -65,6 +67,9 @@ describe('AutoformatVisitor', () => {
6567
}
6668
`,
6769
`
70+
type T1 = string;
71+
export type T2 = string;
72+
6873
class L {
6974
}
7075

rewrite-javascript/rewrite/test/javascript/format/minimum-viable-space-visitor.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,16 @@ describe('MinimumViableSpacingVisitor', () => {
6161
// @formatter:on
6262
))
6363
});
64+
65+
test('type', () => {
66+
return spec.rewriteRun(
67+
// @formatter:off
68+
//language=typescript
69+
typescript(
70+
`type T1 = string;`,
71+
`type T1=string;`
72+
// @formatter:on
73+
))
74+
});
6475
});
6576

0 commit comments

Comments
 (0)