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

Make DiktatRuleSetProvider#get() lazy #1553

Merged
merged 1 commit into from
Nov 7, 2022
Merged
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 @@ -109,12 +109,15 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
yield(resolveConfigFileFromSystemProperty())
}

@Suppress(
"LongMethod",
"TOO_LONG_FUNCTION",
"OVERRIDE_DEPRECATION",
)
override fun get(): RuleSet {
/**
* As of _KtLint_ **0.47**, each rule is expected to have a state and is executed
* twice per file, and a new `Rule` instance is created per each file checked.
*
* Diktat rules have no mutable state yet and use the deprecated _KtLint_
* API, so we initialize them only _once_ for performance reasons and also
* to avoid redundant logging.
*/
private val ruleSet: RuleSet by lazy {
log.debug("Will run $DIKTAT_RULE_SET_ID with $diktatConfigFile" +
" (it can be placed to the run directory or the default file from resources will be used)")
val configPath = possibleConfigs
Expand Down Expand Up @@ -226,12 +229,18 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
.map {
it.invoke(configRules)
}
return RuleSet(
RuleSet(
DIKTAT_RULE_SET_ID,
rules = rules.toTypedArray()
).ordered()
}

@Deprecated(
"Marked for removal in KtLint 0.48. See changelog or KDoc for more information.",
)
override fun get(): RuleSet =
ruleSet

private fun validate(config: RulesConfig) =
require(config.name == DIKTAT_COMMON || config.name in Warnings.names) {
val closestMatch = Warnings.names.minByOrNull { Levenshtein.distance(it, config.name) }
Expand Down