Skip to content

Commit 2ea8f2b

Browse files
committed
JavaScript: Add simple auto-format recipe
1 parent e367533 commit 2ea8f2b

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

rewrite-javascript/rewrite/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function activate(registry: RecipeRegistry): Promise<void> {
3838
const {ModernizeOctalEscapeSequences, ModernizeOctalLiterals, RemoveDuplicateObjectKeys} = await import("./javascript/migrate/es6/index.js");
3939
const {ExportAssignmentToExportDefault} = await import("./javascript/migrate/typescript/index.js");
4040
const {UseObjectPropertyShorthand, PreferOptionalChain, AddParseIntRadix} = await import("./javascript/cleanup/index.js");
41-
const {AsyncCallbackInSyncArrayMethod, UpgradeDependencyVersion, OrderImports, ChangeImport} = await import("./javascript/recipes/index.js");
41+
const {AsyncCallbackInSyncArrayMethod, AutoFormat, UpgradeDependencyVersion, OrderImports, ChangeImport} = await import("./javascript/recipes/index.js");
4242
const {FindDependency} = await import("./javascript/search/index.js");
4343

4444
registry.register(ExportAssignmentToExportDefault);
@@ -52,6 +52,7 @@ export async function activate(registry: RecipeRegistry): Promise<void> {
5252
registry.register(PreferOptionalChain);
5353
registry.register(AddParseIntRadix);
5454
registry.register(AsyncCallbackInSyncArrayMethod);
55+
registry.register(AutoFormat);
5556
registry.register(UpgradeDependencyVersion);
5657
}
5758

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {Recipe} from "../../recipe";
18+
import {TreeVisitor} from "../../visitor";
19+
import {ExecutionContext} from "../../execution";
20+
import {AutoformatVisitor} from "../format";
21+
22+
/**
23+
* Formats JavaScript/TypeScript code using a comprehensive set of formatting rules.
24+
*
25+
* This recipe applies the following formatting:
26+
* - Normalizes whitespace
27+
* - Ensures minimum viable spacing
28+
* - Applies blank line rules
29+
* - Applies wrapping and braces rules
30+
* - Applies spacing rules
31+
* - Applies tabs and indentation rules
32+
*
33+
* The formatting rules are determined by the style settings attached to the source file,
34+
* or defaults to IntelliJ IDEA style if no custom style is specified.
35+
*/
36+
export class AutoFormat extends Recipe {
37+
readonly name = "org.openrewrite.javascript.format.auto-format";
38+
readonly displayName = "Auto-format JavaScript/TypeScript code";
39+
readonly description = "Format JavaScript and TypeScript code using a comprehensive set of formatting rules based on the project's style settings.";
40+
41+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
42+
return new AutoformatVisitor();
43+
}
44+
}

rewrite-javascript/rewrite/src/javascript/recipes/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
export * from "./async-callback-in-sync-array-method";
18+
export * from "./auto-format";
1819
export * from "./upgrade-dependency-version";
1920
export * from "./order-imports";
2021
export * from "./change-import";

rewrite-javascript/rewrite/src/javascript/style.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export namespace IntelliJ {
234234
additive: true,
235235
multiplicative: true,
236236
shift: true,
237-
unary: true,
237+
unary: false,
238238
arrowFunction: true,
239239
beforeUnaryNotAndNotNull: false,
240240
afterUnaryNotAndNotNull: false
@@ -378,7 +378,7 @@ export namespace IntelliJ {
378378
additive: true,
379379
multiplicative: true,
380380
shift: true,
381-
unary: true,
381+
unary: false,
382382
arrowFunction: true,
383383
beforeUnaryNotAndNotNull: false,
384384
afterUnaryNotAndNotNull: false

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ describe('AutoformatVisitor', () => {
237237
)
238238
});
239239

240+
test('unary negative should not have space', () => {
241+
return spec.rewriteRun(
242+
// @formatter:off
243+
//language=typescript
244+
typescript(
245+
// Should not change -1 to - 1
246+
`if (obj === -1) {
247+
}`,
248+
)
249+
// @formatter:on
250+
)
251+
});
252+
240253
test('nested method invocation preserves indentation when formatting subtree', () => {
241254
// This test simulates what happens when the templating system replaces a node
242255
// and calls maybeAutoFormat() on just that subtree

0 commit comments

Comments
 (0)