@openrewrite/rewrite 8.68.0-20251127-184755 → 8.68.0-20251127-190342

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -62,13 +62,13 @@ function activate(registry) {
62
62
  const { OrderImports } = yield import("./recipe/index.js");
63
63
  const { ModernizeOctalEscapeSequences, ModernizeOctalLiterals, RemoveDuplicateObjectKeys } = yield import("./javascript/migrate/es6/index.js");
64
64
  const { ExportAssignmentToExportDefault } = yield import("./javascript/migrate/typescript/index.js");
65
- const { SimplifyObjectBindingProperties } = yield import("./javascript/cleanup/index.js");
65
+ const { UseObjectPropertyShorthand } = yield import("./javascript/cleanup/index.js");
66
66
  registry.register(ExportAssignmentToExportDefault);
67
67
  registry.register(OrderImports);
68
68
  registry.register(ModernizeOctalEscapeSequences);
69
69
  registry.register(ModernizeOctalLiterals);
70
70
  registry.register(RemoveDuplicateObjectKeys);
71
- registry.register(SimplifyObjectBindingProperties);
71
+ registry.register(UseObjectPropertyShorthand);
72
72
  });
73
73
  }
74
74
  rpc_1.RpcCodecs.registerCodec(markers_1.MarkersKind.ParseExceptionResult, {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,4BAYC;AAhDD;;;;;;;;;;;;;;GAcG;AACH,+BAA+D;AAC/D,iCAAsD;AACtD,uCAA4D;AAG5D,+CAA6B;AAC7B,8CAA4B;AAC5B,4CAA0B;AAC1B,0CAAwB;AACxB,0CAAwB;AACxB,yCAAuB;AACvB,4CAA0B;AAC1B,2CAAyB;AACzB,gDAA8B;AAC9B,kDAAgC;AAChC,yCAAuB;AACvB,yCAAuB;AACvB,2CAAyB;AACzB,wCAAsB;AAEtB,uCAAuC;AACvC,SAAsB,QAAQ,CAAC,QAAwB;;QACnD,MAAM,EAAC,YAAY,EAAC,GA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7D,MAAM,KAAK,GAAgC,IAAA,mBAAW,EAAC,MAAM,CAAC,CAAC;YAC/D,KAAK,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YACtC,KAAK,CAAC,UAAU,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YACtD,KAAK,CAAC,aAAa,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;YAC5D,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAChD,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAClD,OAAO,IAAA,mBAAW,EAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;KAAA;CACJ,CAAC,CAAC"}
@@ -1,2 +1,2 @@
1
- export { SimplifyObjectBindingProperties } from "./simplify-object-binding-properties";
1
+ export { UseObjectPropertyShorthand } from "./use-object-property-shorthand";
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/javascript/cleanup/index.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAC,+BAA+B,EAAC,MAAM,sCAAsC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/javascript/cleanup/index.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAC,0BAA0B,EAAC,MAAM,iCAAiC,CAAC"}
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.SimplifyObjectBindingProperties = void 0;
19
- var simplify_object_binding_properties_1 = require("./simplify-object-binding-properties");
20
- Object.defineProperty(exports, "SimplifyObjectBindingProperties", { enumerable: true, get: function () { return simplify_object_binding_properties_1.SimplifyObjectBindingProperties; } });
18
+ exports.UseObjectPropertyShorthand = void 0;
19
+ var use_object_property_shorthand_1 = require("./use-object-property-shorthand");
20
+ Object.defineProperty(exports, "UseObjectPropertyShorthand", { enumerable: true, get: function () { return use_object_property_shorthand_1.UseObjectPropertyShorthand; } });
21
21
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/javascript/cleanup/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,2FAAqF;AAA7E,qJAAA,+BAA+B,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/javascript/cleanup/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,iFAA2E;AAAnE,2IAAA,0BAA0B,OAAA"}
@@ -0,0 +1,22 @@
1
+ import { Recipe } from "../../recipe";
2
+ import { TreeVisitor } from "../../visitor";
3
+ import { ExecutionContext } from "../../execution";
4
+ /**
5
+ * Simplifies object properties where the key and value have the same name,
6
+ * in both destructuring patterns and object literals.
7
+ *
8
+ * Destructuring examples:
9
+ * - `const { x: x } = obj` becomes `const { x } = obj`
10
+ * - `function({ ref: ref, ...props })` becomes `function({ ref, ...props })`
11
+ *
12
+ * Object literal examples:
13
+ * - `{ x: x }` becomes `{ x }`
14
+ * - `{ foo: foo, bar: bar }` becomes `{ foo, bar }`
15
+ */
16
+ export declare class UseObjectPropertyShorthand extends Recipe {
17
+ name: string;
18
+ displayName: string;
19
+ description: string;
20
+ editor(): Promise<TreeVisitor<any, ExecutionContext>>;
21
+ }
22
+ //# sourceMappingURL=use-object-property-shorthand.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-object-property-shorthand.d.ts","sourceRoot":"","sources":["../../../src/javascript/cleanup/use-object-property-shorthand.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAC,gBAAgB,EAAC,MAAM,iBAAiB,CAAC;AAMjD;;;;;;;;;;;GAWG;AACH,qBAAa,0BAA2B,SAAQ,MAAM;IAClD,IAAI,SAAsE;IAC1E,WAAW,SAAmC;IAC9C,WAAW,SAA+L;IAEpM,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC;CAgH9D"}
@@ -24,55 +24,55 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
24
24
  });
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.SimplifyObjectBindingProperties = void 0;
27
+ exports.UseObjectPropertyShorthand = void 0;
28
28
  const recipe_1 = require("../../recipe");
29
29
  const visitor_1 = require("../visitor");
30
30
  const java_1 = require("../../java");
31
31
  const tree_1 = require("../tree");
32
32
  const immer_1 = require("immer");
33
33
  /**
34
- * Simplifies object binding properties where the key and value have the same name.
34
+ * Simplifies object properties where the key and value have the same name,
35
+ * in both destructuring patterns and object literals.
35
36
  *
36
- * Examples:
37
+ * Destructuring examples:
38
+ * - `const { x: x } = obj` becomes `const { x } = obj`
39
+ * - `function({ ref: ref, ...props })` becomes `function({ ref, ...props })`
40
+ *
41
+ * Object literal examples:
37
42
  * - `{ x: x }` becomes `{ x }`
38
- * - `{ ref: ref, ...props }` becomes `{ ref, ...props }`
39
43
  * - `{ foo: foo, bar: bar }` becomes `{ foo, bar }`
40
44
  */
41
- class SimplifyObjectBindingProperties extends recipe_1.Recipe {
45
+ class UseObjectPropertyShorthand extends recipe_1.Recipe {
42
46
  constructor() {
43
47
  super(...arguments);
44
- this.name = "org.openrewrite.javascript.cleanup.simplify-object-binding-properties";
45
- this.displayName = "Simplify object binding properties";
46
- this.description = "Simplifies object destructuring patterns where the property name and variable name are the same (e.g., `{ x: x }` becomes `{ x }`).";
48
+ this.name = "org.openrewrite.javascript.cleanup.use-object-property-shorthand";
49
+ this.displayName = "Use object property shorthand";
50
+ this.description = "Simplifies object properties where the property name and value/variable name are the same (e.g., `{ x: x }` becomes `{ x }`). Applies to both destructuring patterns and object literals.";
47
51
  }
48
52
  editor() {
49
53
  return __awaiter(this, void 0, void 0, function* () {
50
54
  return new class extends visitor_1.JavaScriptVisitor {
55
+ /**
56
+ * Handle object binding patterns (destructuring): const { x: x } = obj
57
+ */
51
58
  visitObjectBindingPattern(pattern, p) {
52
59
  const _super = Object.create(null, {
53
60
  visitObjectBindingPattern: { get: () => super.visitObjectBindingPattern }
54
61
  });
55
62
  return __awaiter(this, void 0, void 0, function* () {
56
- // First, recursively visit children
57
63
  const visited = yield _super.visitObjectBindingPattern.call(this, pattern, p);
58
64
  let hasChanges = false;
59
- // Simplify any properties where key === value
60
65
  const simplifiedBindings = visited.bindings.elements.map(right => {
61
66
  var _a, _b;
62
67
  const element = right.element;
63
- // Check if this is a binding element (e.g., { x: x })
64
68
  if (element.kind === tree_1.JS.Kind.BindingElement) {
65
69
  const binding = element;
66
- // Check if the property name is an identifier
67
70
  if (((_a = binding.propertyName) === null || _a === void 0 ? void 0 : _a.element.kind) === java_1.J.Kind.Identifier) {
68
71
  const propName = binding.propertyName.element.simpleName;
69
- // Check if the binding name is also an identifier with the same name
70
72
  if (((_b = binding.name) === null || _b === void 0 ? void 0 : _b.kind) === java_1.J.Kind.Identifier) {
71
73
  const bindingName = binding.name.simpleName;
72
- // If they match, simplify to shorthand syntax
73
74
  if (propName === bindingName) {
74
75
  hasChanges = true;
75
- // Remove propertyName and transfer its prefix to the name
76
76
  return Object.assign(Object.assign({}, right), { element: Object.assign(Object.assign({}, binding), { propertyName: undefined, name: Object.assign(Object.assign({}, binding.name), { prefix: binding.propertyName.element.prefix }) }) });
77
77
  }
78
78
  }
@@ -83,15 +83,62 @@ class SimplifyObjectBindingProperties extends recipe_1.Recipe {
83
83
  if (!hasChanges) {
84
84
  return visited;
85
85
  }
86
- // Return the pattern with simplified bindings
87
86
  return (0, immer_1.produce)(visited, draft => {
88
87
  draft.bindings.elements = simplifiedBindings;
89
88
  });
90
89
  });
91
90
  }
91
+ /**
92
+ * Handle object literals: { x: x }
93
+ * Object literals are represented as J.NewClass with a body containing JS.PropertyAssignment statements.
94
+ */
95
+ visitNewClass(newClass, ctx) {
96
+ const _super = Object.create(null, {
97
+ visitNewClass: { get: () => super.visitNewClass }
98
+ });
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ var _a;
101
+ const visited = yield _super.visitNewClass.call(this, newClass, ctx);
102
+ // Only process object literals (NewClass with body but no class or arguments)
103
+ if (!visited.body || visited.class || (((_a = visited.arguments) === null || _a === void 0 ? void 0 : _a.elements) && visited.arguments.elements.length > 0)) {
104
+ return visited;
105
+ }
106
+ const statements = visited.body.statements;
107
+ if (!statements || statements.length === 0) {
108
+ return visited;
109
+ }
110
+ let hasChanges = false;
111
+ const simplifiedStatements = statements.map(stmt => {
112
+ var _a;
113
+ if (stmt.element.kind === tree_1.JS.Kind.PropertyAssignment) {
114
+ const prop = stmt.element;
115
+ // Check if the property name is an identifier
116
+ if (prop.name.element.kind === java_1.J.Kind.Identifier) {
117
+ const propName = prop.name.element.simpleName;
118
+ // Check if the initializer is also an identifier with the same name
119
+ if (((_a = prop.initializer) === null || _a === void 0 ? void 0 : _a.kind) === java_1.J.Kind.Identifier) {
120
+ const initName = prop.initializer.simpleName;
121
+ if (propName === initName) {
122
+ hasChanges = true;
123
+ // Remove the initializer to use shorthand syntax
124
+ return Object.assign(Object.assign({}, stmt), { element: Object.assign(Object.assign({}, prop), { initializer: undefined }) });
125
+ }
126
+ }
127
+ }
128
+ }
129
+ return stmt;
130
+ });
131
+ if (!hasChanges) {
132
+ return visited;
133
+ }
134
+ return (0, immer_1.produce)(visited, draft => {
135
+ draft.body.statements = simplifiedStatements;
136
+ });
137
+ });
138
+ }
92
139
  };
93
140
  });
94
141
  }
95
142
  }
96
- exports.SimplifyObjectBindingProperties = SimplifyObjectBindingProperties;
97
- //# sourceMappingURL=simplify-object-binding-properties.js.map
143
+ exports.UseObjectPropertyShorthand = UseObjectPropertyShorthand;
144
+ //# sourceMappingURL=use-object-property-shorthand.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-object-property-shorthand.js","sourceRoot":"","sources":["../../../src/javascript/cleanup/use-object-property-shorthand.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;AAEH,yCAAoC;AAGpC,wCAA6C;AAC7C,qCAA6B;AAC7B,kCAA2B;AAC3B,iCAA8B;AAE9B;;;;;;;;;;;GAWG;AACH,MAAa,0BAA2B,SAAQ,eAAM;IAAtD;;QACI,SAAI,GAAG,kEAAkE,CAAC;QAC1E,gBAAW,GAAG,+BAA+B,CAAC;QAC9C,gBAAW,GAAG,2LAA2L,CAAC;IAkH9M,CAAC;IAhHS,MAAM;;YACR,OAAO,IAAI,KAAM,SAAQ,2BAAmC;gBAExD;;mBAEG;gBACa,yBAAyB,CAAC,OAAgC,EAAE,CAAmB;;;;;wBAC3F,MAAM,OAAO,GAAG,MAAM,OAAM,yBAAyB,YAAC,OAAO,EAAE,CAAC,CAA4B,CAAC;wBAE7F,IAAI,UAAU,GAAG,KAAK,CAAC;wBAEvB,MAAM,kBAAkB,GAAG,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;;4BAC7D,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;4BAE9B,IAAI,OAAO,CAAC,IAAI,KAAK,SAAE,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;gCAC1C,MAAM,OAAO,GAAG,OAA4B,CAAC;gCAE7C,IAAI,CAAA,MAAA,OAAO,CAAC,YAAY,0CAAE,OAAO,CAAC,IAAI,MAAK,QAAC,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;oCAC3D,MAAM,QAAQ,GAAI,OAAO,CAAC,YAAY,CAAC,OAAwB,CAAC,UAAU,CAAC;oCA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AC;oBACP,CAAC;iBAAA;aACJ,CAAC;QACN,CAAC;KAAA;CACJ;AArHD,gEAqHC"}
package/dist/version.txt CHANGED
@@ -1 +1 @@
1
- 8.68.0-20251127-184755
1
+ 8.68.0-20251127-190342
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openrewrite/rewrite",
3
- "version": "8.68.0-20251127-184755",
3
+ "version": "8.68.0-20251127-190342",
4
4
  "license": "Moderne Source Available License",
5
5
  "description": "OpenRewrite JavaScript.",
6
6
  "homepage": "https://github.com/openrewrite/rewrite",
package/src/index.ts CHANGED
@@ -38,14 +38,14 @@ export async function activate(registry: RecipeRegistry): Promise<void> {
38
38
  const {OrderImports} = await import("./recipe/index.js");
39
39
  const {ModernizeOctalEscapeSequences, ModernizeOctalLiterals, RemoveDuplicateObjectKeys} = await import("./javascript/migrate/es6/index.js");
40
40
  const {ExportAssignmentToExportDefault} = await import("./javascript/migrate/typescript/index.js");
41
- const {SimplifyObjectBindingProperties} = await import("./javascript/cleanup/index.js");
41
+ const {UseObjectPropertyShorthand} = await import("./javascript/cleanup/index.js");
42
42
 
43
43
  registry.register(ExportAssignmentToExportDefault);
44
44
  registry.register(OrderImports);
45
45
  registry.register(ModernizeOctalEscapeSequences);
46
46
  registry.register(ModernizeOctalLiterals);
47
47
  registry.register(RemoveDuplicateObjectKeys);
48
- registry.register(SimplifyObjectBindingProperties);
48
+ registry.register(UseObjectPropertyShorthand);
49
49
  }
50
50
 
51
51
  RpcCodecs.registerCodec(MarkersKind.ParseExceptionResult, {
@@ -14,4 +14,4 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- export {SimplifyObjectBindingProperties} from "./simplify-object-binding-properties";
17
+ export {UseObjectPropertyShorthand} from "./use-object-property-shorthand";
@@ -0,0 +1,154 @@
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 {JavaScriptVisitor} from "../visitor";
21
+ import {J} from "../../java";
22
+ import {JS} from "../tree";
23
+ import {produce} from "immer";
24
+
25
+ /**
26
+ * Simplifies object properties where the key and value have the same name,
27
+ * in both destructuring patterns and object literals.
28
+ *
29
+ * Destructuring examples:
30
+ * - `const { x: x } = obj` becomes `const { x } = obj`
31
+ * - `function({ ref: ref, ...props })` becomes `function({ ref, ...props })`
32
+ *
33
+ * Object literal examples:
34
+ * - `{ x: x }` becomes `{ x }`
35
+ * - `{ foo: foo, bar: bar }` becomes `{ foo, bar }`
36
+ */
37
+ export class UseObjectPropertyShorthand extends Recipe {
38
+ name = "org.openrewrite.javascript.cleanup.use-object-property-shorthand";
39
+ displayName = "Use object property shorthand";
40
+ description = "Simplifies object properties where the property name and value/variable name are the same (e.g., `{ x: x }` becomes `{ x }`). Applies to both destructuring patterns and object literals.";
41
+
42
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
43
+ return new class extends JavaScriptVisitor<ExecutionContext> {
44
+
45
+ /**
46
+ * Handle object binding patterns (destructuring): const { x: x } = obj
47
+ */
48
+ protected async visitObjectBindingPattern(pattern: JS.ObjectBindingPattern, p: ExecutionContext): Promise<J | undefined> {
49
+ const visited = await super.visitObjectBindingPattern(pattern, p) as JS.ObjectBindingPattern;
50
+
51
+ let hasChanges = false;
52
+
53
+ const simplifiedBindings = visited.bindings.elements.map(right => {
54
+ const element = right.element;
55
+
56
+ if (element.kind === JS.Kind.BindingElement) {
57
+ const binding = element as JS.BindingElement;
58
+
59
+ if (binding.propertyName?.element.kind === J.Kind.Identifier) {
60
+ const propName = (binding.propertyName.element as J.Identifier).simpleName;
61
+
62
+ if (binding.name?.kind === J.Kind.Identifier) {
63
+ const bindingName = (binding.name as J.Identifier).simpleName;
64
+
65
+ if (propName === bindingName) {
66
+ hasChanges = true;
67
+ return {
68
+ ...right,
69
+ element: {
70
+ ...binding,
71
+ propertyName: undefined,
72
+ name: {
73
+ ...binding.name,
74
+ prefix: binding.propertyName.element.prefix
75
+ }
76
+ }
77
+ } as J.RightPadded<JS.BindingElement>;
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ return right;
84
+ });
85
+
86
+ if (!hasChanges) {
87
+ return visited;
88
+ }
89
+
90
+ return produce(visited, draft => {
91
+ draft.bindings.elements = simplifiedBindings;
92
+ });
93
+ }
94
+
95
+ /**
96
+ * Handle object literals: { x: x }
97
+ * Object literals are represented as J.NewClass with a body containing JS.PropertyAssignment statements.
98
+ */
99
+ protected async visitNewClass(newClass: J.NewClass, ctx: ExecutionContext): Promise<J | undefined> {
100
+ const visited = await super.visitNewClass(newClass, ctx) as J.NewClass;
101
+
102
+ // Only process object literals (NewClass with body but no class or arguments)
103
+ if (!visited.body || visited.class || (visited.arguments?.elements && visited.arguments.elements.length > 0)) {
104
+ return visited;
105
+ }
106
+
107
+ const statements = visited.body.statements;
108
+ if (!statements || statements.length === 0) {
109
+ return visited;
110
+ }
111
+
112
+ let hasChanges = false;
113
+
114
+ const simplifiedStatements = statements.map(stmt => {
115
+ if (stmt.element.kind === JS.Kind.PropertyAssignment) {
116
+ const prop = stmt.element as JS.PropertyAssignment;
117
+
118
+ // Check if the property name is an identifier
119
+ if (prop.name.element.kind === J.Kind.Identifier) {
120
+ const propName = (prop.name.element as J.Identifier).simpleName;
121
+
122
+ // Check if the initializer is also an identifier with the same name
123
+ if (prop.initializer?.kind === J.Kind.Identifier) {
124
+ const initName = (prop.initializer as J.Identifier).simpleName;
125
+
126
+ if (propName === initName) {
127
+ hasChanges = true;
128
+ // Remove the initializer to use shorthand syntax
129
+ return {
130
+ ...stmt,
131
+ element: {
132
+ ...prop,
133
+ initializer: undefined
134
+ }
135
+ } as J.RightPadded<JS.PropertyAssignment>;
136
+ }
137
+ }
138
+ }
139
+ }
140
+
141
+ return stmt;
142
+ });
143
+
144
+ if (!hasChanges) {
145
+ return visited;
146
+ }
147
+
148
+ return produce(visited, draft => {
149
+ draft.body!.statements = simplifiedStatements;
150
+ });
151
+ }
152
+ };
153
+ }
154
+ }
@@ -1,18 +0,0 @@
1
- import { Recipe } from "../../recipe";
2
- import { TreeVisitor } from "../../visitor";
3
- import { ExecutionContext } from "../../execution";
4
- /**
5
- * Simplifies object binding properties where the key and value have the same name.
6
- *
7
- * Examples:
8
- * - `{ x: x }` becomes `{ x }`
9
- * - `{ ref: ref, ...props }` becomes `{ ref, ...props }`
10
- * - `{ foo: foo, bar: bar }` becomes `{ foo, bar }`
11
- */
12
- export declare class SimplifyObjectBindingProperties extends Recipe {
13
- name: string;
14
- displayName: string;
15
- description: string;
16
- editor(): Promise<TreeVisitor<any, ExecutionContext>>;
17
- }
18
- //# sourceMappingURL=simplify-object-binding-properties.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"simplify-object-binding-properties.d.ts","sourceRoot":"","sources":["../../../src/javascript/cleanup/simplify-object-binding-properties.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAC,gBAAgB,EAAC,MAAM,iBAAiB,CAAC;AAMjD;;;;;;;GAOG;AACH,qBAAa,+BAAgC,SAAQ,MAAM;IACvD,IAAI,SAA2E;IAC/E,WAAW,SAAwC;IACnD,WAAW,SAAyI;IAE9I,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC;CA0D9D"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"simplify-object-binding-properties.js","sourceRoot":"","sources":["../../../src/javascript/cleanup/simplify-object-binding-properties.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;AAEH,yCAAoC;AAGpC,wCAA6C;AAC7C,qCAA6B;AAC7B,kCAA2B;AAC3B,iCAA8B;AAE9B;;;;;;;GAOG;AACH,MAAa,+BAAgC,SAAQ,eAAM;IAA3D;;QACI,SAAI,GAAG,uEAAuE,CAAC;QAC/E,gBAAW,GAAG,oCAAoC,CAAC;QACnD,gBAAW,GAAG,qIAAqI,CAAC;IA4DxJ,CAAC;IA1DS,MAAM;;YACR,OAAO,IAAI,KAAM,SAAQ,2BAAmC;gBACxC,yBAAyB,CAAC,OAAgC,EAAE,CAAmB;;;;;wBAC3F,oCAAoC;wBACpC,MAAM,OAAO,GAAG,MAAM,OAAM,yBAAyB,YAAC,OAAO,EAAE,CAAC,CAA4B,CAAC;wBAE7F,IAAI,UAAU,GAAG,KAAK,CAAC;wBAEvB,8CAA8C;wBAC9C,MAAM,kBAAkB,GAAG,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;;4BAC7D,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;4BAE9B,sDAAsD;4BACtD,IAAI,OAAO,CAAC,IAAI,KAAK,SAAE,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;gCAC1C,MAAM,OAAO,GAAG,OAA4B,CAAC;gCAE7C,8CAA8C;gCAC9C,IAAI,CAAA,MAAA,OAAO,CAAC,YAAY,0CAAE,OAAO,CAAC,IAAI,MAAK,QAAC,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;oCAC3D,MAAM,QAAQ,GAAI,OAAO,CAAC,YAAY,CAAC,OAAwB,CAAC,UAAU,CAAC;oCAE3E,qEAAqE;oCACrE,IAAI,CAAA,MAAA,OAAO,CAAC,IAAI,0CAAE,IAAI,MAAK,QAAC,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;wCAC3C,MAAM,WAAW,GAAI,OAAO,CAAC,IAAqB,CAAC,UAAU,CAAC;wCAE9D,8CAA8C;wCAC9C,IAAI,QAAQ,KAAK,WAAW,EAAE,CAAC;4CAC3B,UAAU,GAAG,IAAI,CAAC;4CAClB,0DAA0D;4CAC1D,OAAO,gCACA,KAAK,KACR,OAAO,kCACA,OAAO,KACV,YAAY,EAAE,SAAS,EACvB,IAAI,kCACG,OAAO,CAAC,IAAI,KACf,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,SAGlB,CAAC;wCAC1C,CAAC;oCACL,CAAC;gCACL,CAAC;4BACL,CAAC;4BAED,OAAO,KAAK,CAAC;wBACjB,CAAC,CAAC,CAAC;wBAEH,IAAI,CAAC,UAAU,EAAE,CAAC;4BACd,OAAO,OAAO,CAAC;wBACnB,CAAC;wBAED,8CAA8C;wBAC9C,OAAO,IAAA,eAAO,EAAC,OAAO,EAAE,KAAK,CAAC,EAAE;4BAC5B,KAAK,CAAC,QAAQ,CAAC,QAAQ,GAAG,kBAAkB,CAAC;wBACjD,CAAC,CAAC,CAAC;oBACP,CAAC;iBAAA;aACJ,CAAC;QACN,CAAC;KAAA;CACJ;AA/DD,0EA+DC"}
@@ -1,96 +0,0 @@
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 {JavaScriptVisitor} from "../visitor";
21
- import {J} from "../../java";
22
- import {JS} from "../tree";
23
- import {produce} from "immer";
24
-
25
- /**
26
- * Simplifies object binding properties where the key and value have the same name.
27
- *
28
- * Examples:
29
- * - `{ x: x }` becomes `{ x }`
30
- * - `{ ref: ref, ...props }` becomes `{ ref, ...props }`
31
- * - `{ foo: foo, bar: bar }` becomes `{ foo, bar }`
32
- */
33
- export class SimplifyObjectBindingProperties extends Recipe {
34
- name = "org.openrewrite.javascript.cleanup.simplify-object-binding-properties";
35
- displayName = "Simplify object binding properties";
36
- description = "Simplifies object destructuring patterns where the property name and variable name are the same (e.g., `{ x: x }` becomes `{ x }`).";
37
-
38
- async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
39
- return new class extends JavaScriptVisitor<ExecutionContext> {
40
- protected async visitObjectBindingPattern(pattern: JS.ObjectBindingPattern, p: ExecutionContext): Promise<J | undefined> {
41
- // First, recursively visit children
42
- const visited = await super.visitObjectBindingPattern(pattern, p) as JS.ObjectBindingPattern;
43
-
44
- let hasChanges = false;
45
-
46
- // Simplify any properties where key === value
47
- const simplifiedBindings = visited.bindings.elements.map(right => {
48
- const element = right.element;
49
-
50
- // Check if this is a binding element (e.g., { x: x })
51
- if (element.kind === JS.Kind.BindingElement) {
52
- const binding = element as JS.BindingElement;
53
-
54
- // Check if the property name is an identifier
55
- if (binding.propertyName?.element.kind === J.Kind.Identifier) {
56
- const propName = (binding.propertyName.element as J.Identifier).simpleName;
57
-
58
- // Check if the binding name is also an identifier with the same name
59
- if (binding.name?.kind === J.Kind.Identifier) {
60
- const bindingName = (binding.name as J.Identifier).simpleName;
61
-
62
- // If they match, simplify to shorthand syntax
63
- if (propName === bindingName) {
64
- hasChanges = true;
65
- // Remove propertyName and transfer its prefix to the name
66
- return {
67
- ...right,
68
- element: {
69
- ...binding,
70
- propertyName: undefined,
71
- name: {
72
- ...binding.name,
73
- prefix: binding.propertyName.element.prefix
74
- }
75
- }
76
- } as J.RightPadded<JS.BindingElement>;
77
- }
78
- }
79
- }
80
- }
81
-
82
- return right;
83
- });
84
-
85
- if (!hasChanges) {
86
- return visited;
87
- }
88
-
89
- // Return the pattern with simplified bindings
90
- return produce(visited, draft => {
91
- draft.bindings.elements = simplifiedBindings;
92
- });
93
- }
94
- };
95
- }
96
- }