Skip to content

Commit

Permalink
Revert IdNamingPolicy change of making fun public
Browse files Browse the repository at this point in the history
Add try catch for `RuleId` creation
Add warning msg
  • Loading branch information
atulgpt committed May 19, 2023
1 parent abfbbfa commit e6ca5a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@ internal object IdNamingPolicy {
* Will throw [IllegalArgumentException] on invalid [ruleSetId] name.
*/
internal fun enforceRuleSetIdNaming(ruleSetId: String) =
require(ruleSetId.matches(RULE_SET_ID_REGEX)) {
"No ktlint rule with ruleSet id '$ruleSetId' has been loaded. Please check spelling of the id which has " +
"to match regexp '${RULE_SET_ID_REGEX.pattern}'"
}
require(ruleSetId.matches(RULE_SET_ID_REGEX)) { "RuleSet id '$ruleSetId' must match '${RULE_SET_ID_REGEX.pattern}'" }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pinterest.ktlint.rule.engine.internal

import com.pinterest.ktlint.logger.api.initKtLintKLogger
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
Expand All @@ -8,6 +9,7 @@ import com.pinterest.ktlint.rule.engine.core.util.safeAs
import com.pinterest.ktlint.rule.engine.internal.SuppressionLocatorBuilder.CommentSuppressionHint.Type.BLOCK_END
import com.pinterest.ktlint.rule.engine.internal.SuppressionLocatorBuilder.CommentSuppressionHint.Type.BLOCK_START
import com.pinterest.ktlint.rule.engine.internal.SuppressionLocatorBuilder.CommentSuppressionHint.Type.EOL
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
Expand All @@ -28,6 +30,8 @@ internal object SuppressionLocatorBuilder {
*/
private val NO_SUPPRESSION: SuppressionLocator = { _, _ -> false }

private val LOGGER = KotlinLogging.logger {}.initKtLintKLogger()

/**
* Mapping of non-ktlint annotations to ktlint-annotation so that ktlint rules will be suppressed automatically
* when specific non-ktlint annotations are found. The prevents that developers have to specify multiple annotations
Expand Down Expand Up @@ -207,7 +211,7 @@ internal object SuppressionLocatorBuilder {
// not prefixed with any rule set id.
RuleId.prefixWithStandardRuleSetIdWhenMissing(it)
}
.map { RuleId(it) }
.mapNotNull { createRuleIdOrNull(it) }

private fun <T> List<T>.tail() = this.subList(1, this.size)

Expand Down Expand Up @@ -257,7 +261,7 @@ internal object SuppressionLocatorBuilder {
// Disable specific rule
argumentExpressionText.removePrefix("ktlint:")
.let { RuleId.prefixWithStandardRuleSetIdWhenMissing(it) }
.let { RuleId(it) }
.let { createRuleIdOrNull(it) }
}
else -> {
// Disable specific rule if the annotation value is mapped to a specific rule
Expand All @@ -266,6 +270,17 @@ internal object SuppressionLocatorBuilder {
}
}

private fun createRuleIdOrNull(ruleId: String): RuleId? {
return try {
RuleId(ruleId)
} catch (illegalArgument: IllegalArgumentException) {
LOGGER.warn {
"Can not create rule with ruleId = $ruleId due to following error ${illegalArgument.message}"
}
null
}
}

/**
* @param range zero-based range of lines where lint errors should be suppressed
* @param disabledRuleIds empty set means "all"
Expand Down

0 comments on commit e6ca5a7

Please sign in to comment.