diff --git a/rewrite-javascript/rewrite/src/javascript/format.ts b/rewrite-javascript/rewrite/src/javascript/format.ts
index 70fef10637..50e921db5c 100644
--- a/rewrite-javascript/rewrite/src/javascript/format.ts
+++ b/rewrite-javascript/rewrite/src/javascript/format.ts
@@ -1225,16 +1225,7 @@ export class TabsAndIndentsVisitor
extends JavaScriptVisitor
{
}
private get currentIndent(): string {
- const indent = this.cursor.getNearestMessage("indentToUse");
- if (indent == undefined) {
- const enclosingWhitespace = this.cursor.firstEnclosing((x: any): x is J => x.prefix && x.prefix.whitespace.includes("\n"))?.prefix.whitespace;
- if (enclosingWhitespace) {
- return enclosingWhitespace.substring(enclosingWhitespace.lastIndexOf("\n") + 1);
- } else {
- return "";
- }
- }
- return indent;
+ return this.cursor.getNearestMessage("indentToUse") ?? "";
}
private combineIndent(oldWs: string, relativeIndent: string): string {
diff --git a/rewrite-javascript/rewrite/test/javascript/format/blank-lines-visitor.test.ts b/rewrite-javascript/rewrite/test/javascript/format/blank-lines-visitor.test.ts
index 821a98e6d5..16f1442e95 100644
--- a/rewrite-javascript/rewrite/test/javascript/format/blank-lines-visitor.test.ts
+++ b/rewrite-javascript/rewrite/test/javascript/format/blank-lines-visitor.test.ts
@@ -23,8 +23,10 @@ import {Style} from "../../../src";
type StyleCustomizer = (draft: Draft) => void;
-function blankLines(customizer: StyleCustomizer): BlankLinesStyle {
- return produce(IntelliJ.TypeScript.blankLines(), draft => customizer(draft));
+function blankLines(customizer?: StyleCustomizer): BlankLinesStyle {
+ return customizer
+ ? produce(IntelliJ.TypeScript.blankLines(), draft => customizer(draft))
+ : IntelliJ.TypeScript.blankLines();
}
describe('BlankLinesVisitor', () => {
@@ -113,8 +115,7 @@ describe('BlankLinesVisitor', () => {
});
test('simple un-minify', () => {
- spec.recipe = fromVisitor(new BlankLinesVisitor(blankLines(draft => {
- })));
+ spec.recipe = fromVisitor(new BlankLinesVisitor(blankLines()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -130,8 +131,7 @@ describe('BlankLinesVisitor', () => {
});
test('un-minify', () => {
- spec.recipe = fromVisitor(new BlankLinesVisitor(blankLines(draft => {
- })));
+ spec.recipe = fromVisitor(new BlankLinesVisitor(blankLines()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
diff --git a/rewrite-javascript/rewrite/test/javascript/format/format.test.ts b/rewrite-javascript/rewrite/test/javascript/format/format.test.ts
index 8d125e4157..148bba6147 100644
--- a/rewrite-javascript/rewrite/test/javascript/format/format.test.ts
+++ b/rewrite-javascript/rewrite/test/javascript/format/format.test.ts
@@ -68,10 +68,9 @@ describe('AutoformatVisitor', () => {
console.log(\`Number: \` + j);
}
`,
- // TODO the space before export type T2 is excessive
`
type T1 = string;
- export type T2 = string;
+ export type T2 = string;
abstract class L {
}
diff --git a/rewrite-javascript/rewrite/test/javascript/format/spaces-visitor.test.ts b/rewrite-javascript/rewrite/test/javascript/format/spaces-visitor.test.ts
index bb4546a4b4..607adc8a46 100644
--- a/rewrite-javascript/rewrite/test/javascript/format/spaces-visitor.test.ts
+++ b/rewrite-javascript/rewrite/test/javascript/format/spaces-visitor.test.ts
@@ -21,8 +21,10 @@ import {MarkersKind, NamedStyles, randomId, Style} from "../../../src";
type StyleCustomizer = (draft: Draft) => void;
-function spaces(customizer: StyleCustomizer): SpacesStyle {
- return produce(IntelliJ.TypeScript.spaces(), draft => customizer(draft));
+function spaces(customizer?: StyleCustomizer): SpacesStyle {
+ return customizer
+ ? produce(IntelliJ.TypeScript.spaces(), draft => customizer(draft))
+ : IntelliJ.TypeScript.spaces();
}
describe('SpacesVisitor', () => {
@@ -50,8 +52,7 @@ describe('SpacesVisitor', () => {
});
test('spaces after export or import', () => {
- spec.recipe = fromVisitor(new SpacesVisitor(spaces(draft => {
- })));
+ spec.recipe = fromVisitor(new SpacesVisitor(spaces()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -101,8 +102,7 @@ describe('SpacesVisitor', () => {
});
test('await', () => {
- spec.recipe = fromVisitor(new SpacesVisitor(spaces(draft => {
- })));
+ spec.recipe = fromVisitor(new SpacesVisitor(spaces()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -117,8 +117,7 @@ describe('SpacesVisitor', () => {
});
test('types', () => {
- spec.recipe = fromVisitor(new SpacesVisitor(spaces(draft => {
- })));
+ spec.recipe = fromVisitor(new SpacesVisitor(spaces()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
diff --git a/rewrite-javascript/rewrite/test/javascript/format/tabs-and-indents-visitor.test.ts b/rewrite-javascript/rewrite/test/javascript/format/tabs-and-indents-visitor.test.ts
index 2ee85cdaac..70e28aba10 100644
--- a/rewrite-javascript/rewrite/test/javascript/format/tabs-and-indents-visitor.test.ts
+++ b/rewrite-javascript/rewrite/test/javascript/format/tabs-and-indents-visitor.test.ts
@@ -21,16 +21,17 @@ import {Style} from "../../../src";
type StyleCustomizer = (draft: Draft) => void;
-function tabsAndIndents(customizer: StyleCustomizer): TabsAndIndentsStyle {
- return produce(IntelliJ.TypeScript.tabsAndIndents(), draft => customizer(draft));
+function tabsAndIndents(customizer?: StyleCustomizer): TabsAndIndentsStyle {
+ return customizer
+ ? produce(IntelliJ.TypeScript.tabsAndIndents(), draft => customizer(draft))
+ : IntelliJ.TypeScript.tabsAndIndents();
}
describe('TabsAndIndentsVisitor', () => {
test('simple', () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -56,8 +57,7 @@ describe('TabsAndIndentsVisitor', () => {
test('indent', () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -103,8 +103,7 @@ describe('TabsAndIndentsVisitor', () => {
test("not so simple", () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -184,8 +183,7 @@ describe('TabsAndIndentsVisitor', () => {
test('lambda', () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -219,8 +217,7 @@ describe('TabsAndIndentsVisitor', () => {
test("type", () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -241,8 +238,7 @@ describe('TabsAndIndentsVisitor', () => {
test("multi-line callback", () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -263,8 +259,7 @@ describe('TabsAndIndentsVisitor', () => {
test("single-line callback with braces", () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -275,8 +270,7 @@ describe('TabsAndIndentsVisitor', () => {
test("single-line callback without braces", () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
- })));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -287,7 +281,7 @@ describe('TabsAndIndentsVisitor', () => {
test("collapsed if/while", () => {
const spec = new RecipeSpec()
- spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {})));
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
return spec.rewriteRun(
// @formatter:off
//language=typescript
@@ -328,4 +322,24 @@ describe('TabsAndIndentsVisitor', () => {
// @formatter:on
)
})
+
+ test('unify indentation', () => {
+ const spec = new RecipeSpec()
+ spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents()));
+ return spec.rewriteRun(
+ // @formatter:off
+ //language=typescript
+ typescript(`
+ const good = 136;
+ const great = 436;
+ const ideal = 504;
+ `,
+ `
+ const good = 136;
+ const great = 436;
+ const ideal = 504;
+ `)
+ // @formatter:on
+ )
+ });
});