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

Migration to Kotlin's imports #1659

Merged
merged 22 commits into from
Apr 12, 2023
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
2 changes: 0 additions & 2 deletions diktat-rules/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ dependencies {
api(projects.diktatApi)
implementation(libs.kotlin.stdlib.jdk8)
implementation(libs.kotlin.compiler.embeddable)
// a temporary solution to avoid a lot of changes in diktat-rules
implementation(projects.diktatKtlintEngine)
// guava is used for string case utils
implementation(libs.guava)
implementation(libs.kotlin.logging)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.cqfn.diktat.ruleset.rules
import org.cqfn.diktat.api.DiktatErrorEmitter
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.isRuleEnabled
import org.cqfn.diktat.ruleset.utils.getFilePath
import org.cqfn.diktat.ruleset.utils.getFilePathSafely

import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
Expand Down Expand Up @@ -68,7 +68,7 @@ abstract class DiktatRule(
log.error(
"""Internal error has occurred in rule [$id]. Please make an issue on this bug at https://github.com/saveourtool/diKTat/.
As a workaround you can disable these inspections in yml config: <$inspections>.
Root cause of the problem is in [${node.getFilePath()}] file.
Root cause of the problem is in [${node.getFilePathSafely()}] file.
""".trimIndent(), internalError
)
// we are very sorry for throwing common Error here, but unfortunately we are not able to throw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.isPascalCase
import org.cqfn.diktat.util.isKotlinScript

import com.pinterest.ktlint.core.ast.ElementType.CLASS
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import org.jetbrains.kotlin.KtNodeTypes.CLASS
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER
import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType

import java.io.File

Expand All @@ -34,7 +34,7 @@ class FileNaming(configRules: List<RulesConfig>) : DiktatRule(
private lateinit var filePath: String

override fun logic(node: ASTNode) {
if (node.elementType == FILE) {
if (node.elementType == KtFileElementType.INSTANCE) {
filePath = node.getFilePath()
if (!filePath.isKotlinScript()) {
checkFileNaming(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,33 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.*
import org.cqfn.diktat.ruleset.utils.search.findAllVariablesWithUsages

import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.CATCH
import com.pinterest.ktlint.core.ast.ElementType.CATCH_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.CLASS
import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION
import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_TYPE
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.KDOC
import com.pinterest.ktlint.core.ast.ElementType.OBJECT_DECLARATION
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.TYPE_PARAMETER
import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevCodeSibling
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtNodeTypes.CATCH
import org.jetbrains.kotlin.KtNodeTypes.CLASS
import org.jetbrains.kotlin.KtNodeTypes.DESTRUCTURING_DECLARATION
import org.jetbrains.kotlin.KtNodeTypes.DESTRUCTURING_DECLARATION_ENTRY
import org.jetbrains.kotlin.KtNodeTypes.FUNCTION_TYPE
import org.jetbrains.kotlin.KtNodeTypes.OBJECT_DECLARATION
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.TYPE_PARAMETER
import org.jetbrains.kotlin.KtNodeTypes.TYPE_REFERENCE
import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER_LIST
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens.KDOC
import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.lexer.KtTokens.CATCH_KEYWORD
import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isPrivate
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType

import java.util.Locale

Expand Down Expand Up @@ -102,17 +101,17 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
// isVariable is used as a workaround to check corner case with variables that have length == 1
val (identifierNodes, isVariable) = when (node.elementType) {
// covers interface, class, enum class and annotation class names
ElementType.CLASS -> Pair(checkClassNamings(node), false)
KtNodeTypes.CLASS -> Pair(checkClassNamings(node), false)
// covers "object" code blocks
ElementType.OBJECT_DECLARATION -> Pair(checkObjectNaming(node), false)
KtNodeTypes.OBJECT_DECLARATION -> Pair(checkObjectNaming(node), false)
// covers variables (val/var), constants (const val) and parameters for lambdas
ElementType.PROPERTY, ElementType.VALUE_PARAMETER -> Pair(checkVariableName(node), true)
KtNodeTypes.PROPERTY, KtNodeTypes.VALUE_PARAMETER -> Pair(checkVariableName(node), true)
// covers case of enum values
ElementType.ENUM_ENTRY -> Pair(checkEnumValues(node), false)
KtNodeTypes.ENUM_ENTRY -> Pair(checkEnumValues(node), false)
// covers global functions, extensions and class methods
ElementType.FUN -> Pair(checkFunctionName(node), false)
KtNodeTypes.FUN -> Pair(checkFunctionName(node), false)
// covers case of typeAlias values
ElementType.TYPEALIAS -> Pair(checkTypeAliases(node), false)
KtNodeTypes.TYPEALIAS -> Pair(checkTypeAliases(node), false)
else -> Pair(null, false)
}

Expand All @@ -130,7 +129,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
// node is a symbol declaration with present identifier
val identifierText = identifier.text
if (identifierText.startsWith('`') && identifierText.endsWith('`')) {
val isTestFun = node.elementType == ElementType.FUN && node.hasTestAnnotation()
val isTestFun = node.elementType == KtNodeTypes.FUN && node.hasTestAnnotation()
if (!isTestFun) {
BACKTICKS_PROHIBITED.warn(configRules, emitWarn, isFixMode, identifierText, identifier.startOffset, identifier)
}
Expand All @@ -155,7 +154,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
var namesOfVariables = extractVariableIdentifiers(node)
// Only local private properties will be autofix in order not to break code if there are usages in other files.
// Destructuring declarations are only allowed for local variables/values, so we don't need to calculate `isFix` for every node in `namesOfVariables`
val isPublicOrNonLocalProperty = if (node.elementType == ElementType.PROPERTY) (node.psi as KtProperty).run { !isLocal && !isPrivate() } else false
val isPublicOrNonLocalProperty = if (node.elementType == KtNodeTypes.PROPERTY) (node.psi as KtProperty).run { !isLocal && !isPrivate() } else false
val isNonPrivatePrimaryConstructorParameter = (node.psi as? KtParameter)?.run {
hasValOrVar() && getParentOfType<KtPrimaryConstructor>(true)?.valueParameters?.contains(this) == true && !isPrivate()
} ?: false
Expand All @@ -169,7 +168,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
}
// check if identifier of a property has a confusing name
if (confusingIdentifierNames.contains(variableName.text) && !isValidCatchIdentifier(variableName) &&
node.elementType == ElementType.PROPERTY
node.elementType == KtNodeTypes.PROPERTY
) {
warnConfusingName(variableName)
}
Expand All @@ -187,7 +186,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
// FixMe: cover fixes with tests
val correctVariableName = variableName.text.toLowerCamelCase()
variableName
.parent({ it.elementType == FILE })
.parent { it.elementType == KtFileElementType.INSTANCE }
?.findAllVariablesWithUsages { it.name == variableName.text }
?.flatMap { it.value.toList() }
?.forEach { (it.node.firstChildNode as LeafPsiElement).rawReplaceWithText(correctVariableName) }
Expand Down Expand Up @@ -299,7 +298,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
val classNameNode = node.getIdentifierName() ?: return
// getting super class name
val superClassName: String? = node
.getFirstChildWithType(ElementType.SUPER_TYPE_LIST)
.getFirstChildWithType(KtNodeTypes.SUPER_TYPE_LIST)
?.findLeafWithSpecificType(TYPE_REFERENCE)
?.text

Expand Down Expand Up @@ -334,7 +333,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
* to check all variables will need to check all IDENTIFIERS in ENUM_ENTRY
*/
private fun checkEnumValues(node: ASTNode): List<ASTNode> {
val enumValues: List<ASTNode> = node.getChildren(null).filter { it.elementType == ElementType.IDENTIFIER }
val enumValues: List<ASTNode> = node.getChildren(null).filter { it.elementType == KtTokens.IDENTIFIER }
enumValues.forEach { value ->
val configuration = IdentifierNamingConfiguration(
configRules.getRuleConfig(ENUM_VALUE)?.configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,25 @@ import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PREFIX
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_SYMBOLS
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_MISSING
import org.cqfn.diktat.ruleset.rules.DiktatRule

import org.cqfn.diktat.ruleset.utils.*
import org.cqfn.diktat.util.isKotlinScript

import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.FILE_ANNOTATION_LIST
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.KDOC
import com.pinterest.ktlint.core.ast.ElementType.PACKAGE_DIRECTIVE
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.isLeaf
import mu.KotlinLogging
import org.jetbrains.kotlin.KtNodeTypes.DOT_QUALIFIED_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.FILE_ANNOTATION_LIST
import org.jetbrains.kotlin.KtNodeTypes.PACKAGE_DIRECTIVE
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens.KDOC
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.lexer.KtTokens.BLOCK_COMMENT
import org.jetbrains.kotlin.lexer.KtTokens.EOL_COMMENT
import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER
import org.jetbrains.kotlin.lexer.KtTokens.PACKAGE_KEYWORD
import org.jetbrains.kotlin.lexer.KtTokens.WHITE_SPACE
import org.jetbrains.kotlin.psi.psiUtil.children

import java.util.concurrent.atomic.AtomicInteger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType
import org.cqfn.diktat.ruleset.utils.getFilePath
import org.cqfn.diktat.ruleset.utils.prevSibling

import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.prevSibling
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.TokenType
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.lexer.KtTokens.BLOCK_COMMENT
import org.jetbrains.kotlin.lexer.KtTokens.EOL_COMMENT
import org.jetbrains.kotlin.lexer.KtTokens.WHITE_SPACE
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType
import org.jetbrains.kotlin.resolve.ImportPath

/**
Expand All @@ -34,7 +34,7 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(

override fun logic(node: ASTNode) {
ktPsiFactory = KtPsiFactory(node.psi.project, false) // regarding markGenerated see KDoc in Kotlin sources
if (node.elementType == FILE) {
if (node.elementType == KtFileElementType.INSTANCE) {
checkCommentedCode(node)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.cqfn.diktat.ruleset.utils.getFilePath
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.isGradleScript
import org.cqfn.diktat.ruleset.utils.isWhiteSpace
import org.cqfn.diktat.ruleset.utils.moveChildBefore

import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.IMPORT_LIST
import com.pinterest.ktlint.core.ast.ElementType.KDOC
import com.pinterest.ktlint.core.ast.ElementType.PACKAGE_DIRECTIVE
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isWhiteSpace
import mu.KotlinLogging
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtNodeTypes.IMPORT_LIST
import org.jetbrains.kotlin.KtNodeTypes.PACKAGE_DIRECTIVE
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens.KDOC
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.lexer.KtTokens.BLOCK_COMMENT
import org.jetbrains.kotlin.lexer.KtTokens.WHITE_SPACE
import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType

import java.time.LocalDate

Expand All @@ -49,7 +50,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
HEADER_NOT_BEFORE_PACKAGE, HEADER_WRONG_FORMAT, WRONG_COPYRIGHT_YEAR),
) {
override fun logic(node: ASTNode) {
if (node.elementType == FILE && !node.getFilePath().isGradleScript()) {
if (node.elementType == KtFileElementType.INSTANCE && !node.getFilePath().isGradleScript()) {
checkCopyright(node)
if (checkHeaderKdocPosition(node)) {
checkHeaderKdoc(node)
Expand All @@ -68,8 +69,8 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
}
}
?: run {
val numDeclaredClassesAndObjects = node.getAllChildrenWithType(ElementType.CLASS).size +
node.getAllChildrenWithType(ElementType.OBJECT_DECLARATION).size
val numDeclaredClassesAndObjects = node.getAllChildrenWithType(KtNodeTypes.CLASS).size +
node.getAllChildrenWithType(KtNodeTypes.OBJECT_DECLARATION).size
if (numDeclaredClassesAndObjects != 1) {
HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE.warn(configRules, emitWarn, isFixMode,
"there are $numDeclaredClassesAndObjects declared classes and/or objects", node.startOffset, node)
Expand All @@ -88,7 +89,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
private fun checkHeaderKdocPosition(node: ASTNode): Boolean {
val firstKdoc = node.findChildAfter(IMPORT_LIST, KDOC)
// if `firstKdoc.treeParent` is File then it's a KDoc not bound to any other structures
if (node.findChildBefore(PACKAGE_DIRECTIVE, KDOC) == null && firstKdoc != null && firstKdoc.treeParent.elementType == FILE) {
if (node.findChildBefore(PACKAGE_DIRECTIVE, KDOC) == null && firstKdoc != null && firstKdoc.treeParent.elementType == KtFileElementType.INSTANCE) {
HEADER_NOT_BEFORE_PACKAGE.warnAndFix(configRules, emitWarn, isFixMode, "header KDoc is located after package or imports", firstKdoc.startOffset, firstKdoc) {
node.moveChildBefore(firstKdoc, node.getFirstChildWithType(PACKAGE_DIRECTIVE), true)
// ensure there is no empty line between copyright and header kdoc
Expand Down Expand Up @@ -155,7 +156,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
!isHeaderCommentContainText(headerComment, copyrightWithCorrectYear)

val isMissingCopyright = headerComment == null && configuration.isCopyrightMandatory()
val isCopyrightInsideKdoc = (node.getAllChildrenWithType(KDOC) + node.getAllChildrenWithType(ElementType.EOL_COMMENT))
val isCopyrightInsideKdoc = (node.getAllChildrenWithType(KDOC) + node.getAllChildrenWithType(KtTokens.EOL_COMMENT))
.any { commentNode ->
copyrightWords.any { commentNode.text.contains(it, ignoreCase = true) }
}
Expand Down
Loading