Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impex type attribute validation for complex header #338

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}