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

Logging of file name in DiktatRule is unreliable #948

Merged
merged 2 commits into from
Jul 1, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
configuration.domainName?.let {
domainName = it
if (node.elementType == PACKAGE_DIRECTIVE) {
val filePath = node.getRootNode().getFilePath()
val filePath = node.getFilePath()

// getting all identifiers from existing package name into the list like [org, diktat, project]
val wordsInPackageName = node.findAllDescendantsWithSpecificType(IDENTIFIER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.cqfn.diktat.ruleset.utils.findChildBefore
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.getRootNode
import org.cqfn.diktat.ruleset.utils.isGradleScript
import org.cqfn.diktat.ruleset.utils.moveChildBefore

Expand Down Expand Up @@ -48,7 +47,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
listOf(HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE, HEADER_MISSING_OR_WRONG_COPYRIGHT, HEADER_NOT_BEFORE_PACKAGE,
HEADER_NOT_BEFORE_PACKAGE, HEADER_WRONG_FORMAT, WRONG_COPYRIGHT_YEAR)) {
override fun logic(node: ASTNode) {
if (node.elementType == FILE && !node.getRootNode().getFilePath().isGradleScript()) {
if (node.elementType == FILE && !node.getFilePath().isGradleScript()) {
checkCopyright(node)
if (checkHeaderKdocPosition(node)) {
checkHeaderKdoc(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
*/
override fun logic(node: ASTNode) {
val config = configRules.getCommonConfiguration()
val filePath = node.getRootNode().getFilePath()
val filePath = node.getFilePath()
if (!node.hasTestAnnotation() && !isLocatedInTest(filePath.splitPathToDirs(), config.testAnchors)) {
when (node.elementType) {
FILE -> checkTopLevelDoc(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.cqfn.diktat.ruleset.utils.getBodyLines
import org.cqfn.diktat.ruleset.utils.getFilePath
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.getIdentifierName
import org.cqfn.diktat.ruleset.utils.getRootNode
import org.cqfn.diktat.ruleset.utils.hasChildOfType
import org.cqfn.diktat.ruleset.utils.hasKnownKdocTag
import org.cqfn.diktat.ruleset.utils.hasTestAnnotation
Expand Down Expand Up @@ -76,7 +75,7 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
override fun logic(node: ASTNode) {
if (node.elementType == FUN && node.getFirstChildWithType(MODIFIER_LIST).isAccessibleOutside() && !node.isOverridden()) {
val config = configRules.getCommonConfiguration()
val filePath = node.getRootNode().getFilePath()
val filePath = node.getFilePath()
val isTestMethod = node.hasTestAnnotation() || isLocatedInTest(filePath.splitPathToDirs(), config.testAnchors)
if (!isTestMethod && !node.isStandardMethod() && !node.isSingleLineGetterOrSetter()) {
checkSignatureDescription(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
}
@Suppress("COLLAPSE_IF_STATEMENTS")
override fun logic(node: ASTNode) {
val filePath = node.getRootNode().getFilePath()
val filePath = node.getFilePath()
val config = configRules.getCommonConfiguration()
if (node.elementType == INTEGER_CONSTANT || node.elementType == FLOAT_CONSTANT) {
if (!isLocatedInTest(filePath.splitPathToDirs(), config.testAnchors) || !configuration.isIgnoreTest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AvoidUtilityClass(configRules: List<RulesConfig>) : DiktatRule(
listOf(AVOID_USING_UTILITY_CLASS)) {
override fun logic(node: ASTNode) {
val config = configRules.getCommonConfiguration()
val filePath = node.getRootNode().getFilePath()
val filePath = node.getFilePath()
if (!node.hasTestAnnotation() && !isLocatedInTest(filePath.splitPathToDirs(), config.testAnchors)) {
@Suppress("COLLAPSE_IF_STATEMENTS")
if (node.elementType == OBJECT_DECLARATION || node.elementType == CLASS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class RunInScript(private val configRules: List<RulesConfig>) : Rule("run-script
isFixMode = autoCorrect
emitWarn = emit

if (node.elementType == SCRIPT_INITIALIZER && node.getRootNode().getFilePath().isKotlinScript()) {
if (node.getRootNode().getFilePath().isGradleScript()) {
if (node.elementType == SCRIPT_INITIALIZER && node.getFilePath().isKotlinScript()) {
if (node.getFilePath().isGradleScript()) {
checkGradleNode(node)
} else {
checkScript(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,10 @@ fun ASTNode.calculateLineColByOffset() = buildPositionInTextLocator(text)
*
* @return name of the file [this] node belongs to
*/
fun ASTNode.getFilePath(): String = getUserData(KtLint.FILE_PATH_USER_DATA_KEY).let {
fun ASTNode.getFilePath(): String = getRootNode().also {
require(it.elementType == FILE) { "Root node type is not FILE, but file_path is present in user_data only in FILE nodes" }
}.getUserData(KtLint.FILE_PATH_USER_DATA_KEY).let {
requireNotNull(it) { "File path is not present in user data" }
it
}

/**
Expand Down