Skip to content

Commit 46ec2f6

Browse files
Fixing autoformatting for function as the first element (#6253)
1 parent 2517f0a commit 46ec2f6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,8 @@ export class BlankLinesVisitor<P> extends JavaScriptVisitor<P> {
941941
tree = produce(tree as JS.StatementExpression, draft => {
942942
this.ensurePrefixHasNewLine(draft);
943943
});
944-
} else if (tree.kind === J.Kind.MethodDeclaration && this.cursor.value.kind != JS.Kind.StatementExpression) {
944+
} else if (tree.kind === J.Kind.MethodDeclaration && this.cursor.value.kind != JS.Kind.StatementExpression
945+
&& (this.cursor.parent?.value.kind != JS.Kind.CompilationUnit || (this.cursor.parent?.value as JS.CompilationUnit).statements[0].element !== tree)) {
945946
tree = produce(tree as J.MethodDeclaration, draft => {
946947
this.ensurePrefixHasNewLine(draft);
947948
});

rewrite-javascript/rewrite/test/javascript/format/blank-lines-visitor.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,20 @@ describe('BlankLinesVisitor', () => {
158158
// @formatter:on
159159
);
160160
});
161+
162+
test('just a function', () => {
163+
spec.recipe = fromVisitor(new BlankLinesVisitor(blankLines()));
164+
return spec.rewriteRun(
165+
// @formatter:off
166+
//language=typescript
167+
typescript(
168+
`
169+
function foo(x) {
170+
return x;
171+
}
172+
`
173+
)
174+
// @formatter:on
175+
);
176+
});
161177
});

0 commit comments

Comments
 (0)