[workspace model] Add inspection to check that all fields are immutable in inheritors of WorkspaceEntity interface

GitOrigin-RevId: f9e164b80a98a248b89f5508f0a3bdcaa0c5494c
This commit is contained in:
Mikhail Mazurkevich
2023-02-24 23:26:14 +00:00
committed by intellij-monorepo-bot
parent ab8bb7a7a5
commit 4f159b1c40
4 changed files with 55 additions and 0 deletions
@@ -0,0 +1,9 @@
<html>
<body>
Detects unsupported <code>var</code> fields in the inheritors of <code>WorkspaceEntity</code> interface
<p>
Interface implementing <code>WorkspaceEntity</code> have to have only <code>val</code> fields because it's immutable.
Implementation of <code>WorkspaceEntity.Builder</code> will be generated for the mutation
</p>
</body>
</html>
@@ -30,5 +30,12 @@
enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.devkit.workspaceModel.WorkspaceImplAbsentInspection"
key="inspection.workspace.absent.model.display.name"/>
<localInspection language="kotlin"
groupBundle="messages.DevKitWorkspaceModelBundle"
groupPathKey="inspections.group.path" groupKey="inspections.group.workspace.model"
runForWholeFile="true"
enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.devkit.workspaceModel.WorkspaceEntityMutableFieldInspection"
key="inspection.workspace.mutable.field.display.name"/>
</extensions>
</idea-plugin>
@@ -6,6 +6,8 @@ inspection.workspace.msg.obsolete.implementation=Obsolete entity implementation
inspection.workspace.msg.regenerate.implementation=Regenerate implementation
inspection.workspace.absent.model.display.name=Absent entity implementation
inspection.workspace.msg.generate.implementation=Generate implementation
inspection.workspace.mutable.field.display.name=Unsupported 'var' field in entity
inspection.workspace.msg.change.field.to.val=Change to 'val'
progress.title.generating.code=Generating code
progress.text.removing.old.code=Removing old code
@@ -0,0 +1,37 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.devkit.workspaceModel
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtVisitorVoid
class WorkspaceEntityMutableFieldInspection: LocalInspectionTool() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = object : KtVisitorVoid() {
override fun visitClass(klass: KtClass) {
if (!klass.isWorkspaceEntity()) return
if (klass.name == "Builder") return
klass.getProperties().forEach { property ->
if (property.isVar) {
holder.registerProblem(property, DevKitWorkspaceModelBundle.message("inspection.workspace.mutable.field.display.name"),
ProblemHighlightType.GENERIC_ERROR, MakeFieldImmutableFix())
}
}
}
}
}
private class MakeFieldImmutableFix : LocalQuickFix {
override fun getName() = DevKitWorkspaceModelBundle.message("inspection.workspace.msg.change.field.to.val")
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val psiElement = descriptor.psiElement
if (!psiElement.isValid) return
if (psiElement !is KtProperty) return
val factory = KtPsiFactory(project)
psiElement.valOrVarKeyword.replace(factory.createValKeyword())
}
}