Skip to content

Commit

Permalink
#338 | Impex type attribute validation for complex header
Browse files Browse the repository at this point in the history
  • Loading branch information
mlytvyn authored Apr 15, 2023
1 parent c3e48e5 commit 9e4f135
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions resources/META-INF/plugin-release-info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<![CDATA[
<h3>2023.1.5</h3>
<ul>
<li><i>Feature:</i> Added new <strong>Impex</strong> inspection rules
<ul>
<li>Type attribute validation for complex header (<a href="https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/338" target="_blank" rel="nofollow">#338</a>)</li>
</ul>
</li>
<li><i>Feature:</i> Added generate Diagram run line marker for
<ul>
<li>Business Process (<a href="https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/331" target="_blank" rel="nofollow">#331</a>)</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.idea.plugin.hybris.common.utils.HybrisI18NBundleUtils.message
import com.intellij.idea.plugin.hybris.impex.psi.ImpexAnyHeaderParameterName
import com.intellij.idea.plugin.hybris.impex.psi.ImpexMacroUsageDec
import com.intellij.idea.plugin.hybris.impex.psi.*
import com.intellij.idea.plugin.hybris.impex.psi.ImpexTypes.DOCUMENT_ID
import com.intellij.idea.plugin.hybris.impex.psi.ImpexVisitor
import com.intellij.idea.plugin.hybris.impex.utils.ImpexPsiUtils.findHeaderItemTypeName
import com.intellij.idea.plugin.hybris.psi.reference.TSReferenceBase
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.util.PsiTreeUtil

/**
* @author Nosov Aleksandr <[email protected]>
Expand All @@ -41,16 +39,29 @@ class ImpexUnknownTypeAttributeInspection : LocalInspectionTool() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = ImpexHeaderLineVisitor(holder)

private class ImpexHeaderLineVisitor(private val problemsHolder: ProblemsHolder) : ImpexVisitor() {

override fun visitParameter(parameter: ImpexParameter) {
inspect(parameter)
}

override fun visitAnyHeaderParameterName(parameter: ImpexAnyHeaderParameterName) {
if (!isNotMacros(parameter) || !isNotDocumentId(parameter.firstChild)) return
inspect(parameter)
}

private fun inspect(parameter: PsiElement) {
if (parameter.firstChild is ImpexMacroUsageDec || (parameter.firstChild as? LeafPsiElement)?.elementType == DOCUMENT_ID) return

val firstReference = parameter.references.firstOrNull() ?: return

if (firstReference.canonicalText.contains(".")) return
if (firstReference !is TSReferenceBase<*>) return
if (firstReference.multiResolve(false).isNotEmpty()) return

val typeName = findHeaderItemTypeName(parameter)
val typeName = (
PsiTreeUtil.getParentOfType(parameter, ImpexParameter::class.java)
?: PsiTreeUtil.getParentOfType(parameter, ImpexFullHeaderParameter::class.java)
?.anyHeaderParameterName
)
?.text
?: "?"
problemsHolder.registerProblem(
Expand All @@ -59,8 +70,5 @@ class ImpexUnknownTypeAttributeInspection : LocalInspectionTool() {
ProblemHighlightType.ERROR)
}

private fun isNotMacros(parameter: ImpexAnyHeaderParameterName) = parameter.firstChild !is ImpexMacroUsageDec

private fun isNotDocumentId(element: PsiElement) = (element as LeafPsiElement).elementType != DOCUMENT_ID
}
}

0 comments on commit 9e4f135

Please sign in to comment.