[uast-inspection] IDEA-350483 New inspection LoggingGuardedByConditionInspection also deletes comments

- support comments

GitOrigin-RevId: b5add2dcbe56eaca7d8c90319734bacda3e68923
This commit is contained in:
Mikhail Pyltsin
2024-04-03 11:41:20 +00:00
committed by intellij-monorepo-bot
parent 992e3b7bdc
commit e84bc31482
3 changed files with 121 additions and 1 deletions
@@ -11,6 +11,7 @@ import com.intellij.modcommand.ModPsiUpdater
import com.intellij.modcommand.PsiUpdateModCommandQuickFix
import com.intellij.openapi.project.Project
import com.intellij.profile.codeInspection.InspectionProjectProfileManager
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiWhiteSpace
@@ -144,11 +145,44 @@ class LoggingGuardedByConditionInspection : AbstractBaseUastLocalInspectionTool(
var currentParent = ifStatementSourcePsi.parent
var after = ifStatementSourcePsi
var nextExpression = expressions[0].sourcePsi
val lastExpression = expressions.last().sourcePsi ?: return
var lastExpression = expressions.last().sourcePsi
while (nextExpression?.parent.toUElement()?.sourcePsi == expressions[0].sourcePsi) {
nextExpression = nextExpression?.parent
}
while (lastExpression?.parent.toUElement()?.sourcePsi == expressions.last().sourcePsi) {
lastExpression = lastExpression?.parent
}
while (true) {
if (nextExpression?.prevSibling is PsiComment) {
nextExpression = nextExpression.prevSibling
continue
}
if(nextExpression?.prevSibling is PsiWhiteSpace && nextExpression.prevSibling?.prevSibling is PsiComment) {
nextExpression = nextExpression.prevSibling.prevSibling
continue
}
break
}
while (true) {
if (lastExpression?.nextSibling is PsiComment) {
lastExpression = lastExpression.nextSibling
continue
}
if (lastExpression?.nextSibling is PsiWhiteSpace && lastExpression.nextSibling?.nextSibling is PsiComment) {
lastExpression = lastExpression.nextSibling.nextSibling
continue
}
break
}
if (nextExpression == null) return
if (lastExpression == null) return
while (true) {
if (nextExpression == null) break
var newAdded: PsiElement = currentParent.addAfter(nextExpression.copy(), after)
@@ -188,4 +188,43 @@ class JavaLoggingGuardedByConditionInspectionTest : LoggingGuardedByConditionIns
hint = JvmAnalysisBundle.message("jvm.inspection.log.guarded.fix.family.name")
)
}
fun `test slf4j with comment fix`() {
myFixture.testQuickFix(
testPreview = true,
lang = JvmLanguage.JAVA,
before = """
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class X {
private static final Logger LOG = LoggerFactory.getLogger(X.class);
void n(String arg) {
if(<caret>LOG.isDebugEnabled()) {//comment1
//comment2
LOG.debug("test" + arg);
//comment3
//comment4
}
}
}
""".trimIndent(),
after = """
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class X {
private static final Logger LOG = LoggerFactory.getLogger(X.class);
void n(String arg) {
//comment1
//comment2
LOG.debug("test" + arg);
//comment3
//comment4
}
}
""".trimIndent(),
hint = JvmAnalysisBundle.message("jvm.inspection.log.guarded.fix.family.name")
)
}
}
@@ -238,4 +238,51 @@ class KotlinLoggingGuardedByConditionInspectionTest : LoggingGuardedByConditionI
hint = JvmAnalysisBundle.message("jvm.inspection.log.guarded.fix.family.name")
)
}
fun `test slf4j with comment fix`() {
myFixture.testQuickFix(
testPreview = true,
lang = JvmLanguage.KOTLIN,
before = """
import org.slf4j.Logger
import org.slf4j.LoggerFactory
internal class X {
fun n(arg: String) {
if (LOG.<caret>isDebugEnabled) {//comment1
//comment2
LOG.debug("test" + arg)
//comment3
//comment4
}
}
companion object {
private val LOG: Logger = LoggerFactory.getLogger()
}
}
""".trimIndent(),
after = """
import org.slf4j.Logger
import org.slf4j.LoggerFactory
internal class X {
fun n(arg: String) {
//comment1
//comment2
LOG.debug("test" + arg)
//comment3
//comment4
}
companion object {
private val LOG: Logger = LoggerFactory.getLogger()
}
}
""".trimIndent(),
hint = JvmAnalysisBundle.message("jvm.inspection.log.guarded.fix.family.name")
)
}
}