@openrewrite/recipes-code-quality 0.1.0-20260409-154017
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/all-branches-identical.d.ts +10 -0
- package/dist/all-branches-identical.d.ts.map +1 -0
- package/dist/all-branches-identical.js +116 -0
- package/dist/all-branches-identical.js.map +1 -0
- package/dist/boolean-checks-not-inverted.d.ts +10 -0
- package/dist/boolean-checks-not-inverted.d.ts.map +1 -0
- package/dist/boolean-checks-not-inverted.js +117 -0
- package/dist/boolean-checks-not-inverted.js.map +1 -0
- package/dist/collapsible-if-statements.d.ts +10 -0
- package/dist/collapsible-if-statements.d.ts.map +1 -0
- package/dist/collapsible-if-statements.js +119 -0
- package/dist/collapsible-if-statements.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/merge-identical-branches.d.ts +10 -0
- package/dist/merge-identical-branches.d.ts.map +1 -0
- package/dist/merge-identical-branches.js +118 -0
- package/dist/merge-identical-branches.js.map +1 -0
- package/dist/remove-duplicate-conditions.d.ts +10 -0
- package/dist/remove-duplicate-conditions.d.ts.map +1 -0
- package/dist/remove-duplicate-conditions.js +141 -0
- package/dist/remove-duplicate-conditions.js.map +1 -0
- package/dist/remove-self-assignment.d.ts +10 -0
- package/dist/remove-self-assignment.d.ts.map +1 -0
- package/dist/remove-self-assignment.js +86 -0
- package/dist/remove-self-assignment.js.map +1 -0
- package/dist/remove-unconditional-value-overwrite.d.ts +10 -0
- package/dist/remove-unconditional-value-overwrite.d.ts.map +1 -0
- package/dist/remove-unconditional-value-overwrite.js +112 -0
- package/dist/remove-unconditional-value-overwrite.js.map +1 -0
- package/dist/simplify-boolean-literal.d.ts +9 -0
- package/dist/simplify-boolean-literal.d.ts.map +1 -0
- package/dist/simplify-boolean-literal.js +179 -0
- package/dist/simplify-boolean-literal.js.map +1 -0
- package/dist/simplify-redundant-logical-expression.d.ts +10 -0
- package/dist/simplify-redundant-logical-expression.d.ts.map +1 -0
- package/dist/simplify-redundant-logical-expression.js +63 -0
- package/dist/simplify-redundant-logical-expression.js.map +1 -0
- package/package.json +39 -0
- package/src/all-branches-identical.ts +133 -0
- package/src/boolean-checks-not-inverted.ts +144 -0
- package/src/collapsible-if-statements.ts +162 -0
- package/src/index.ts +34 -0
- package/src/merge-identical-branches.ts +149 -0
- package/src/remove-duplicate-conditions.ts +165 -0
- package/src/remove-self-assignment.ts +98 -0
- package/src/remove-unconditional-value-overwrite.ts +128 -0
- package/src/simplify-boolean-literal.ts +220 -0
- package/src/simplify-redundant-logical-expression.ts +75 -0
|
@@ -0,0 +1,220 @@
|
|
|
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
|
+
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
17
|
+
import {capture, JavaScriptVisitor, JS, pattern, rewrite, template} from "@openrewrite/rewrite/javascript";
|
|
18
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Simplifies boolean expressions that compare against boolean literals.
|
|
22
|
+
*
|
|
23
|
+
* Comparisons against `true` or `false` are redundant and make code
|
|
24
|
+
* harder to read. A direct boolean expression or its negation is
|
|
25
|
+
* clearer and more idiomatic.
|
|
26
|
+
*
|
|
27
|
+
* Handles both strict (`===`/`!==`) and loose (`==`/`!=`) equality operators.
|
|
28
|
+
*/
|
|
29
|
+
export class SimplifyBooleanLiteral extends Recipe {
|
|
30
|
+
name = "org.openrewrite.javascript.cleanup.SimplifyBooleanLiteral";
|
|
31
|
+
displayName = "Simplify boolean literal comparisons";
|
|
32
|
+
description = "Removes redundant comparisons against boolean literals. " +
|
|
33
|
+
"For example, `x === true` becomes `x` and `x === false` becomes `!x`. " +
|
|
34
|
+
"Handles both strict (`===`/`!==`) and loose (`==`/`!=`) equality.";
|
|
35
|
+
tags = ["RSPEC-S1125"];
|
|
36
|
+
|
|
37
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
38
|
+
// x === true -> x
|
|
39
|
+
const equalsTrue = rewrite(() => {
|
|
40
|
+
const x = capture();
|
|
41
|
+
return {
|
|
42
|
+
before: pattern`${x} === true`,
|
|
43
|
+
after: template`${x}`
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// true === x -> x
|
|
48
|
+
const trueEquals = rewrite(() => {
|
|
49
|
+
const x = capture();
|
|
50
|
+
return {
|
|
51
|
+
before: pattern`true === ${x}`,
|
|
52
|
+
after: template`${x}`
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// x === false -> !x
|
|
57
|
+
const equalsFalse = rewrite(() => {
|
|
58
|
+
const x = capture();
|
|
59
|
+
return {
|
|
60
|
+
before: pattern`${x} === false`,
|
|
61
|
+
after: template`!${x}`
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// false === x -> !x
|
|
66
|
+
const falseEquals = rewrite(() => {
|
|
67
|
+
const x = capture();
|
|
68
|
+
return {
|
|
69
|
+
before: pattern`false === ${x}`,
|
|
70
|
+
after: template`!${x}`
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// x !== true -> !x
|
|
75
|
+
const notEqualsTrue = rewrite(() => {
|
|
76
|
+
const x = capture();
|
|
77
|
+
return {
|
|
78
|
+
before: pattern`${x} !== true`,
|
|
79
|
+
after: template`!${x}`
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// true !== x -> !x
|
|
84
|
+
const trueNotEquals = rewrite(() => {
|
|
85
|
+
const x = capture();
|
|
86
|
+
return {
|
|
87
|
+
before: pattern`true !== ${x}`,
|
|
88
|
+
after: template`!${x}`
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// x !== false -> x
|
|
93
|
+
const notEqualsFalse = rewrite(() => {
|
|
94
|
+
const x = capture();
|
|
95
|
+
return {
|
|
96
|
+
before: pattern`${x} !== false`,
|
|
97
|
+
after: template`${x}`
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// false !== x -> x
|
|
102
|
+
const falseNotEquals = rewrite(() => {
|
|
103
|
+
const x = capture();
|
|
104
|
+
return {
|
|
105
|
+
before: pattern`false !== ${x}`,
|
|
106
|
+
after: template`${x}`
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Loose equality: x == true -> x
|
|
111
|
+
const looseEqualsTrue = rewrite(() => {
|
|
112
|
+
const x = capture();
|
|
113
|
+
return {
|
|
114
|
+
before: pattern`${x} == true`,
|
|
115
|
+
after: template`${x}`
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// true == x -> x
|
|
120
|
+
const trueLooseEquals = rewrite(() => {
|
|
121
|
+
const x = capture();
|
|
122
|
+
return {
|
|
123
|
+
before: pattern`true == ${x}`,
|
|
124
|
+
after: template`${x}`
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// x == false -> !x
|
|
129
|
+
const looseEqualsFalse = rewrite(() => {
|
|
130
|
+
const x = capture();
|
|
131
|
+
return {
|
|
132
|
+
before: pattern`${x} == false`,
|
|
133
|
+
after: template`!${x}`
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// false == x -> !x
|
|
138
|
+
const falseLooseEquals = rewrite(() => {
|
|
139
|
+
const x = capture();
|
|
140
|
+
return {
|
|
141
|
+
before: pattern`false == ${x}`,
|
|
142
|
+
after: template`!${x}`
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// x != true -> !x
|
|
147
|
+
const looseNotEqualsTrue = rewrite(() => {
|
|
148
|
+
const x = capture();
|
|
149
|
+
return {
|
|
150
|
+
before: pattern`${x} != true`,
|
|
151
|
+
after: template`!${x}`
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// true != x -> !x
|
|
156
|
+
const trueLooseNotEquals = rewrite(() => {
|
|
157
|
+
const x = capture();
|
|
158
|
+
return {
|
|
159
|
+
before: pattern`true != ${x}`,
|
|
160
|
+
after: template`!${x}`
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// x != false -> x
|
|
165
|
+
const looseNotEqualsFalse = rewrite(() => {
|
|
166
|
+
const x = capture();
|
|
167
|
+
return {
|
|
168
|
+
before: pattern`${x} != false`,
|
|
169
|
+
after: template`${x}`
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// false != x -> x
|
|
174
|
+
const falseLooseNotEquals = rewrite(() => {
|
|
175
|
+
const x = capture();
|
|
176
|
+
return {
|
|
177
|
+
before: pattern`false != ${x}`,
|
|
178
|
+
after: template`${x}`
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Strict equality rules (=== / !==) match JS.Binary nodes
|
|
183
|
+
const strictRules = equalsTrue
|
|
184
|
+
.orElse(trueEquals)
|
|
185
|
+
.orElse(equalsFalse)
|
|
186
|
+
.orElse(falseEquals)
|
|
187
|
+
.orElse(notEqualsTrue)
|
|
188
|
+
.orElse(trueNotEquals)
|
|
189
|
+
.orElse(notEqualsFalse)
|
|
190
|
+
.orElse(falseNotEquals);
|
|
191
|
+
|
|
192
|
+
// Loose equality rules (== / !=) match J.Binary nodes
|
|
193
|
+
const looseRules = looseEqualsTrue
|
|
194
|
+
.orElse(trueLooseEquals)
|
|
195
|
+
.orElse(looseEqualsFalse)
|
|
196
|
+
.orElse(falseLooseEquals)
|
|
197
|
+
.orElse(looseNotEqualsTrue)
|
|
198
|
+
.orElse(trueLooseNotEquals)
|
|
199
|
+
.orElse(looseNotEqualsFalse)
|
|
200
|
+
.orElse(falseLooseNotEquals);
|
|
201
|
+
|
|
202
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
203
|
+
override async visitBinary(
|
|
204
|
+
binary: J.Binary,
|
|
205
|
+
ctx: ExecutionContext
|
|
206
|
+
): Promise<J | undefined> {
|
|
207
|
+
binary = await super.visitBinary(binary, ctx) as J.Binary;
|
|
208
|
+
return await looseRules.tryOn(this.cursor, binary) || binary;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
override async visitBinaryExtensions(
|
|
212
|
+
binary: JS.Binary,
|
|
213
|
+
ctx: ExecutionContext
|
|
214
|
+
): Promise<J | undefined> {
|
|
215
|
+
binary = await super.visitBinaryExtensions(binary, ctx) as JS.Binary;
|
|
216
|
+
return await strictRules.tryOn(this.cursor, binary) || binary;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
import {ExecutionContext, printer, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
17
|
+
import {JavaScriptVisitor} from "@openrewrite/rewrite/javascript";
|
|
18
|
+
import {Expression, J} from "@openrewrite/rewrite/java";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Simplifies redundant logical expressions where both operands are identical.
|
|
22
|
+
*
|
|
23
|
+
* `x && x` always evaluates to `x`, and `x || x` always evaluates to `x`.
|
|
24
|
+
* The duplicated operand adds noise and may hide a copy-paste bug where
|
|
25
|
+
* the programmer intended a different second condition.
|
|
26
|
+
*
|
|
27
|
+
* Only targets `&&` and `||`. Arithmetic and comparison operators are
|
|
28
|
+
* left alone because `n + n` is `2 * n`, not `n`.
|
|
29
|
+
*/
|
|
30
|
+
export class SimplifyRedundantLogicalExpression extends Recipe {
|
|
31
|
+
name = "org.openrewrite.javascript.cleanup.SimplifyRedundantLogicalExpression";
|
|
32
|
+
displayName = "Simplify redundant logical expressions";
|
|
33
|
+
description = "Replace `x && x` with `x` and `x || x` with `x`. " +
|
|
34
|
+
"Identical operands in a logical expression are redundant " +
|
|
35
|
+
"and often indicate a copy-paste mistake.";
|
|
36
|
+
tags = ["RSPEC-S1764"];
|
|
37
|
+
estimatedEffortPerOccurrence = 2;
|
|
38
|
+
|
|
39
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
40
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
41
|
+
override async visitBinary(
|
|
42
|
+
binary: J.Binary,
|
|
43
|
+
ctx: ExecutionContext
|
|
44
|
+
): Promise<J | undefined> {
|
|
45
|
+
binary = await super.visitBinary(binary, ctx) as J.Binary;
|
|
46
|
+
|
|
47
|
+
const op = binary.operator.element;
|
|
48
|
+
if (op !== J.Binary.Type.And && op !== J.Binary.Type.Or) {
|
|
49
|
+
return binary;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (await expressionsEqual(binary.left as Expression & J, binary.right as Expression & J)) {
|
|
53
|
+
// Replace the binary with just the left operand,
|
|
54
|
+
// preserving the binary's prefix (leading whitespace)
|
|
55
|
+
return {...(binary.left as any), prefix: binary.prefix};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return binary;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function printNode(node: J): Promise<string> {
|
|
65
|
+
const p = printer("org.openrewrite.javascript.tree.JS$CompilationUnit");
|
|
66
|
+
return await p.print(node);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function expressionsEqual(a: Expression & J, b: Expression & J): Promise<boolean> {
|
|
70
|
+
// The printer includes the node's prefix (leading whitespace),
|
|
71
|
+
// so we trim to compare just the expression text.
|
|
72
|
+
const aText = (await printNode(a)).trim();
|
|
73
|
+
const bText = (await printNode(b)).trim();
|
|
74
|
+
return aText === bText;
|
|
75
|
+
}
|