Skip to content

Commit da2e4dd

Browse files
Not adding newline in TabsAndIndents block end handling (#5501)
1 parent 32e8ece commit da2e4dd

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ export class TabsAndIndentsVisitor<P> extends JavaScriptVisitor<P> {
10981098
}
10991099
if (draft.kind === J.Kind.Block) {
11001100
const block = draft as Draft<J> as Draft<J.Block>;
1101-
block.end.whitespace = this.newline + relativeIndent;
1101+
block.end.whitespace = this.combineIndent(block.end.whitespace, relativeIndent);
11021102
}
11031103
});
11041104
}

rewrite-javascript/rewrite/test/javascript/format/tabs-and-indents-visitor.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ describe('TabsAndIndentsVisitor', () => {
155155
switch( s ){
156156
case "apple" :
157157
return 1;
158-
159158
}
160159
return 0;
161160
}
@@ -239,4 +238,50 @@ describe('TabsAndIndentsVisitor', () => {
239238
// @formatter:on
240239
)
241240
})
241+
242+
test("multi-line callback", () => {
243+
const spec = new RecipeSpec()
244+
spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
245+
})));
246+
return spec.rewriteRun(
247+
// @formatter:off
248+
//language=typescript
249+
typescript(
250+
`
251+
[1, 2, 3].forEach( x => {
252+
console.log(x);
253+
});
254+
`,
255+
`
256+
[1, 2, 3].forEach( x => {
257+
console.log(x);
258+
});
259+
`)
260+
// @formatter:on
261+
)
262+
})
263+
264+
test("single-line callback with braces", () => {
265+
const spec = new RecipeSpec()
266+
spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
267+
})));
268+
return spec.rewriteRun(
269+
// @formatter:off
270+
//language=typescript
271+
typescript(`[1, 2, 3].forEach(x => {console.log(x)});`)
272+
// @formatter:on
273+
)
274+
})
275+
276+
test("single-line callback without braces", () => {
277+
const spec = new RecipeSpec()
278+
spec.recipe = fromVisitor(new TabsAndIndentsVisitor(tabsAndIndents(draft => {
279+
})));
280+
return spec.rewriteRun(
281+
// @formatter:off
282+
//language=typescript
283+
typescript(`[1, 2, 3].forEach(x => console.log(x));`)
284+
// @formatter:on
285+
)
286+
})
242287
});

0 commit comments

Comments
 (0)