Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions rewrite-javascript/rewrite/src/javascript/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,16 +1225,7 @@ export class TabsAndIndentsVisitor<P> extends JavaScriptVisitor<P> {
}

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import {Style} from "../../../src";

type StyleCustomizer<T extends Style> = (draft: Draft<T>) => void;

function blankLines(customizer: StyleCustomizer<BlankLinesStyle>): BlankLinesStyle {
return produce(IntelliJ.TypeScript.blankLines(), draft => customizer(draft));
function blankLines(customizer?: StyleCustomizer<BlankLinesStyle>): BlankLinesStyle {
return customizer
? produce(IntelliJ.TypeScript.blankLines(), draft => customizer(draft))
: IntelliJ.TypeScript.blankLines();
}

describe('BlankLinesVisitor', () => {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import {MarkersKind, NamedStyles, randomId, Style} from "../../../src";

type StyleCustomizer<T extends Style> = (draft: Draft<T>) => void;

function spaces(customizer: StyleCustomizer<SpacesStyle>): SpacesStyle {
return produce(IntelliJ.TypeScript.spaces(), draft => customizer(draft));
function spaces(customizer?: StyleCustomizer<SpacesStyle>): SpacesStyle {
return customizer
? produce(IntelliJ.TypeScript.spaces(), draft => customizer(draft))
: IntelliJ.TypeScript.spaces();
}

describe('SpacesVisitor', () => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ import {Style} from "../../../src";

type StyleCustomizer<T extends Style> = (draft: Draft<T>) => void;

function tabsAndIndents(customizer: StyleCustomizer<TabsAndIndentsStyle>): TabsAndIndentsStyle {
return produce(IntelliJ.TypeScript.tabsAndIndents(), draft => customizer(draft));
function tabsAndIndents(customizer?: StyleCustomizer<TabsAndIndentsStyle>): 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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
)
});
});