Skip to content

Commit 697f603

Browse files
committed
perf: no-unnecessary-boolean-literal-compare: only build fixes when needed
1 parent 0a23a4f commit 697f603

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

internal/rules/no_unnecessary_boolean_literal_compare/no_unnecessary_boolean_literal_compare.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,37 @@ var NoUnnecessaryBooleanLiteralCompareRule = rule.Rule{
175175
msg = buildDirectMessage()
176176
}
177177

178-
parent := node.Parent
179-
for ast.IsParenthesizedExpression(parent) {
180-
parent = parent.Parent
181-
}
182-
isUnaryNegation := ast.IsPrefixUnaryExpression(parent) && parent.AsPrefixUnaryExpression().Operator == ast.KindExclamationToken
178+
ctx.ReportNodeWithFixes(node, msg, func() []rule.RuleFix {
179+
parent := node.Parent
180+
for ast.IsParenthesizedExpression(parent) {
181+
parent = parent.Parent
182+
}
183+
isUnaryNegation := ast.IsPrefixUnaryExpression(parent) && parent.AsPrefixUnaryExpression().Operator == ast.KindExclamationToken
183184

184-
shouldNegate := comparison.negated != comparison.literalBooleanInComparison
185+
shouldNegate := comparison.negated != comparison.literalBooleanInComparison
185186

186-
mutatedNode := node
187-
if isUnaryNegation {
188-
mutatedNode = parent
189-
}
187+
mutatedNode := node
188+
if isUnaryNegation {
189+
mutatedNode = parent
190+
}
190191

191-
fixes := make([]rule.RuleFix, 0, 6)
192+
fixes := make([]rule.RuleFix, 0, 6)
192193

193-
fixes = append(fixes, rule.RuleFixReplace(ctx.SourceFile, mutatedNode, ctx.SourceFile.Text()[comparison.expression.Pos():comparison.expression.End()]))
194+
fixes = append(fixes, rule.RuleFixReplace(ctx.SourceFile, mutatedNode, ctx.SourceFile.Text()[comparison.expression.Pos():comparison.expression.End()]))
194195

195-
if shouldNegate == isUnaryNegation {
196-
fixes = append(fixes, rule.RuleFixInsertBefore(ctx.SourceFile, mutatedNode, "!"))
196+
if shouldNegate == isUnaryNegation {
197+
fixes = append(fixes, rule.RuleFixInsertBefore(ctx.SourceFile, mutatedNode, "!"))
197198

198-
if !utils.IsStrongPrecedenceNode(comparison.expression) {
199-
fixes = append(fixes, rule.RuleFixInsertBefore(ctx.SourceFile, mutatedNode, "("), rule.RuleFixInsertAfter(mutatedNode, ")"))
199+
if !utils.IsStrongPrecedenceNode(comparison.expression) {
200+
fixes = append(fixes, rule.RuleFixInsertBefore(ctx.SourceFile, mutatedNode, "("), rule.RuleFixInsertAfter(mutatedNode, ")"))
201+
}
200202
}
201-
}
202-
203-
if comparison.expressionIsNullableBoolean && !comparison.literalBooleanInComparison {
204-
fixes = append(fixes, rule.RuleFixInsertBefore(ctx.SourceFile, mutatedNode, "("), rule.RuleFixInsertAfter(mutatedNode, " ?? true)"))
205-
}
206203

207-
ctx.ReportNodeWithFixes(node, msg, func() []rule.RuleFix { return fixes })
204+
if comparison.expressionIsNullableBoolean && !comparison.literalBooleanInComparison {
205+
fixes = append(fixes, rule.RuleFixInsertBefore(ctx.SourceFile, mutatedNode, "("), rule.RuleFixInsertAfter(mutatedNode, " ?? true)"))
206+
}
207+
return fixes
208+
})
208209
},
209210
}
210211
},

0 commit comments

Comments
 (0)