Skip to content

Commit 0292ea4

Browse files
JavaScript - fix a few issues in autoformatting logic (#5728)
* Handling ES/SE prefix and markers * Fixing handling of spaces before parens in method invocations * Handling prefixes of ExpressionStatements * Enabling the test again
1 parent 4c870fa commit 0292ea4

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export class SpacesVisitor<P> extends JavaScriptVisitor<P> {
290290
const ret = await super.visitMethodInvocation(methodInv, p) as J.MethodInvocation;
291291
return produceAsync(ret, async draft => {
292292
if (draft.select) {
293-
draft.select = await this.spaceAfterRightPadded(draft.select, this.style.beforeParentheses.functionCallParentheses);
293+
draft.arguments = await this.spaceBeforeContainer(draft.arguments, this.style.beforeParentheses.functionCallParentheses);
294294
}
295295
if (ret.arguments.elements.length > 0 && ret.arguments.elements[0].element.kind != J.Kind.Empty) {
296296
draft.arguments.elements = await Promise.all(draft.arguments.elements.map(async (arg, index) => {
@@ -1030,7 +1030,11 @@ export class BlankLinesVisitor<P> extends JavaScriptVisitor<P> {
10301030

10311031
private ensurePrefixHasNewLine<T extends J>(node: Draft<J>) {
10321032
if (node.prefix && !node.prefix.whitespace.includes("\n")) {
1033-
node.prefix.whitespace = "\n" + node.prefix.whitespace;
1033+
if (node.kind === JS.Kind.ExpressionStatement) {
1034+
this.ensurePrefixHasNewLine((node as JS.ExpressionStatement).expression);
1035+
} else {
1036+
node.prefix.whitespace = "\n" + node.prefix.whitespace;
1037+
}
10341038
}
10351039
}
10361040

rewrite-javascript/rewrite/src/javascript/print.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ export class JavaScriptPrinter extends JavaScriptVisitor<PrintOutputCapture> {
7575
}
7676

7777
override async visitExpressionStatement(statement: JS.ExpressionStatement, p: PrintOutputCapture): Promise<J | undefined> {
78-
// has no markers or prefix
78+
await this.visitSpace(statement.prefix, p);
79+
await this.visitMarkers(statement.markers, p);
7980
await this.visit(statement.expression, p);
8081
return statement;
8182
}
8283

8384
override async visitStatementExpression(statementExpression: JS.StatementExpression, p: PrintOutputCapture): Promise<J | J | undefined> {
84-
// has no markers or prefix
85+
await this.visitSpace(statementExpression.prefix, p);
86+
await this.visitMarkers(statementExpression.markers, p);
8587
await this.visit(statementExpression.statement, p);
8688
return statementExpression;
8789
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ describe('AutoformatVisitor', () => {
2222
const spec = new RecipeSpec()
2323
spec.recipe = fromVisitor(new AutoformatVisitor());
2424

25-
// FIXME enable again once we've fixed `StatementExpression` and `ExpressionStatement` handling
26-
test.skip('everything', () => {
25+
test('everything', () => {
2726
return spec.rewriteRun(
2827
// TODO there should be no newline after the default case in switch
2928
// TODO not sure if there should be a newline after the if and after the finally

0 commit comments

Comments
 (0)