Skip to content

Commit

Permalink
refactor: update code style and detekt rules
Browse files Browse the repository at this point in the history
Signed-off-by: Art Shendrik <[email protected]>
  • Loading branch information
amal committed Apr 27, 2024
1 parent 0c84daa commit b687b55
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 60 deletions.
64 changes: 43 additions & 21 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
# https://editorconfig.org
# https://github.com/facebook/ktfmt/tree/f3c2964/docs/editorconfig
# https://github.com/JetBrains/intellij-community/blob/ee13959/.editorconfig
# https://github.com/JetBrains/kotlin-wrappers/blob/6c09973/.editorconfig
# https://github.com/pinterest/ktlint/blob/7087585/.editorconfig

root = true

[*]
charset = utf-8
indent_style = space
end_of_line = lf
indent_size = 4
tab_width = 4
max_line_length = 140
indent_size = 2
tab_width = 2
ij_continuation_indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,yml,xml,html,css,md,pro}]
indent_size = 2
tab_width = 2
ij_any_else_on_new_line = false
ij_any_catch_on_new_line = false
ij_any_finally_on_new_line = false
ij_any_block_comment_at_first_column = true
ij_any_line_comment_at_first_column = true

[{*.kt,*.kts}]
max_line_length = 140
ij_continuation_indent_size = 4
[.{idea,run}/*.xml]
insert_final_newline = false

[*.{kt,kts,java,scala,groovy}]
max_line_length = 100
indent_size = 4
tab_width = 4

[*.{kt,kts}]
ktlint_code_style = ktlint_official
ktlint_standard = enabled
ktlint_experimental = enabled
ktlint_standard_no-consecutive-blank-lines = disabled
ktlint_standard_trailing-comma = disabled
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_java_names_count_to_use_import_on_demand = 9999
ij_kotlin_align_in_columns_case_branch = false
ij_kotlin_align_multiline_binary_operation = false
ij_kotlin_align_multiline_extends_list = false
ij_kotlin_align_multiline_method_parentheses = false
ij_kotlin_align_multiline_parameters = true
ij_kotlin_align_multiline_parameters_in_calls = false
ij_kotlin_allow_trailing_comma = false
ij_kotlin_allow_trailing_comma_on_call_site = false
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_assignment_wrap = normal
ij_kotlin_blank_lines_after_class_header = 0
ij_kotlin_blank_lines_around_block_when_branches = 0
ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
ij_kotlin_block_comment_at_first_column = true
ij_kotlin_call_parameters_new_line_after_left_paren = true
ij_kotlin_call_parameters_right_paren_on_new_line = true
ij_kotlin_call_parameters_right_paren_on_new_line = false
ij_kotlin_call_parameters_wrap = on_every_item
ij_kotlin_catch_on_new_line = false
ij_kotlin_class_annotation_wrap = split_into_lines
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_continuation_indent_for_chained_calls = false
ij_kotlin_continuation_indent_for_expression_bodies = false
ij_kotlin_continuation_indent_in_argument_lists = false
ij_kotlin_continuation_indent_in_elvis = false
ij_kotlin_continuation_indent_for_chained_calls = true
ij_kotlin_continuation_indent_for_expression_bodies = true
ij_kotlin_continuation_indent_in_argument_lists = true
ij_kotlin_continuation_indent_in_elvis = true
ij_kotlin_continuation_indent_in_if_conditions = false
ij_kotlin_continuation_indent_in_parameter_lists = false
ij_kotlin_continuation_indent_in_supertype_lists = false
Expand All @@ -47,9 +69,9 @@ ij_kotlin_enum_constants_wrap = off
ij_kotlin_extends_list_wrap = normal
ij_kotlin_field_annotation_wrap = split_into_lines
ij_kotlin_finally_on_new_line = false
ij_kotlin_if_rparen_on_new_line = true
ij_kotlin_if_rparen_on_new_line = false
ij_kotlin_import_nested_classes = false
ij_kotlin_imports_layout = *, java.**, javax.**, kotlin.**, ^
ij_kotlin_imports_layout = unset
ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
ij_kotlin_keep_blank_lines_before_right_brace = 0
ij_kotlin_keep_blank_lines_in_code = 2
Expand All @@ -65,9 +87,9 @@ ij_kotlin_method_call_chain_wrap = normal
ij_kotlin_method_parameters_new_line_after_left_paren = true
ij_kotlin_method_parameters_right_paren_on_new_line = true
ij_kotlin_method_parameters_wrap = on_every_item
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_packages_to_use_import_on_demand = ^
ij_kotlin_name_count_to_use_star_import = 999
ij_kotlin_name_count_to_use_star_import_for_members = 999
ij_kotlin_packages_to_use_import_on_demand = unset
ij_kotlin_parameter_annotation_wrap = off
ij_kotlin_space_after_comma = true
ij_kotlin_space_after_extend_colon = true
Expand Down
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* text=auto eol=lf

*.bat text eol=crlf
*.gpg binary
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.iml
.fleet/
.idea/
.kotlin/
projectFilesBackup*/

# Gradle
Expand Down
42 changes: 42 additions & 0 deletions detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# https://detekt.github.io/detekt/configurations.html
# https://github.com/detekt/detekt/blob/main/detekt-core/src/main/resources/default-detekt-config.yml
# https://github.com/detekt/detekt/blob/main/detekt-formatting/src/main/resources/config/config.yml

config:
validation: true
warningsAsErrors: false
checkExhaustiveness: false

output-reports:
active: true
exclude:
- 'TxtOutputReport'
- 'XmlOutputReport'
- 'HtmlOutputReport'
- 'MdOutputReport'

console-reports:
active: true
exclude: [ ]

exceptions:
TooGenericExceptionCaught:
active: false

complexity:
LongParameterList:
ignoreDefaultParameters: true
TooManyFunctions:
ignoreDeprecated: true
ignoreOverridden: true

style:
LoopWithTooManyJumpStatements:
maxJumpCount: 2
MaxLineLength:
maxLineLength: 100

naming:
NoNameShadowing:
# False positives with SAM receiver usage
active: false
9 changes: 9 additions & 0 deletions fluxo-bcv-js/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
<SmellBaseline>
<ManuallySuppressedIssues/>
<CurrentIssues>
<ID>MaxLineLength:ConfigureJsApiTasks.kt$"Checks signatures of public API against the golden value in API folder for :${t.project.name}"</ID>
<ID>MaxLineLength:ConfigureJsApiTasks.kt$"Removed Kotlin/JS API file for compatibility with '$bcvCheckTaskName' task: {}"</ID>
<ID>MaxLineLength:ConfigureJsApiTasks.kt$logger.lifecycle(" >> compilation ${compilation.name}: $compilation // ${compilation.javaClass}")</ID>
<ID>MaxLineLength:ConfigureJsApiTasks.kt$return extension == null || projectName !in extension.ignoredProjects &amp;&amp; !extension.validationDisabled</ID>
<ID>MaxLineLength:FluxoBcvJsPlugin.kt$FluxoBcvJsPlugin$"There is no Kotlin/JS API to provide stability for. Fluxo-BCV-JS does nothing. \n"</ID>
<ID>MaxLineLength:KotlinJsApiBuildTask.kt$KotlinJsApiBuildTask$"Collects built Kotlin TS definitions as API for 'js' compilations of :${project.name}. "</ID>
<ID>MaxLineLength:KotlinJsApiBuildTask.kt$KotlinJsApiBuildTask$"https://kotlinlang.org/docs/whatsnew1820.html#opt-in-for-generation-of-typescript-definition-files"</ID>
<ID>MaxLineLength:KotlinJsApiCompareTask.kt$KotlinJsApiCompareTask$"File ${apiBuildFile.name} is missing from ${projectApiFile.relativeDirPath()}, please run "</ID>
<ID>MaxLineLength:KotlinJsApiCompareTask.kt$KotlinJsApiCompareTask$"Please ensure that ':apiDump' was executed in order to get API dump to compare the build against"</ID>
<ID>WildcardImport:KotlinJsApiBuildTask.kt$import org.gradle.api.tasks.*</ID>
</CurrentIssues>
</SmellBaseline>
92 changes: 63 additions & 29 deletions fluxo-bcv-js/src/main/kotlin/fluxo/bcvjs/ConfigureJsApiTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@

package fluxo.bcvjs

import java.io.File
import java.lang.reflect.AccessibleObject
import kotlinx.validation.ApiValidationExtension
import kotlinx.validation.KotlinApiCompareTask
import org.gradle.api.*
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.TaskProvider
Expand All @@ -28,9 +34,11 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBinaryMode
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.targets.js.ir.*
import java.io.File
import java.lang.reflect.AccessibleObject
import org.jetbrains.kotlin.gradle.targets.js.ir.JsBinary
import org.jetbrains.kotlin.gradle.targets.js.ir.JsIrBinary
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsBinaryContainer
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget


private const val DBG = 0
Expand Down Expand Up @@ -93,7 +101,7 @@ internal fun Project.configureJsApiTasks() {
}

// Common BCV tasks for multiplatform
// Create own ones (for the raw Kotlin/JS module)
// Create the own ones (for the raw Kotlin/JS module)
val commonApiDump = "$API$SUFFIX_DUMP".let { tasks.findByName(it) ?: tasks.create(it) }
val commonApiCheck = "$API$SUFFIX_CHECK".let {
tasks.findByName(it)
Expand Down Expand Up @@ -136,12 +144,14 @@ internal fun Project.configureJsApiTasks() {
}

val linkTasksFromBinaries = binaries.mapTo(LinkedHashSet()) { it.linkTask.get() }
val linkTasksCollection = target.project.tasks.withType(KotlinJsIrLink::class.java).matching {
it.mode == KotlinJsBinaryMode.PRODUCTION &&
!it.name.contains("Test", ignoreCase = true) &&
it.name.contains(target.name, ignoreCase = true)
}
val linkTasks: Provider<Set<KotlinJsIrLink>> = project.provider { linkTasksCollection + linkTasksFromBinaries }
val linkTasksCollection =
target.project.tasks.withType(KotlinJsIrLink::class.java).matching {
it.mode == KotlinJsBinaryMode.PRODUCTION &&
!it.name.contains("Test", ignoreCase = true) &&
it.name.contains(target.name, ignoreCase = true)
}
val linkTasks: Provider<Set<KotlinJsIrLink>> =
project.provider { linkTasksCollection + linkTasksFromBinaries }

if (DBG > 0) {
linkTasks.get().forEach {
Expand All @@ -150,7 +160,13 @@ internal fun Project.configureJsApiTasks() {
}

val targetConfig = TargetConfig(target.project, target.name, dirConfig)
configureKotlinCompilation(extension, targetConfig, commonApiDump, commonApiCheck, linkTasks)
configureKotlinCompilation(
extension,
targetConfig,
commonApiDump,
commonApiCheck,
linkTasks,
)
}
}

Expand Down Expand Up @@ -181,7 +197,15 @@ private fun Project.configureKotlinCompilation(

it.outputFile.fileProvider(buildFile)
}
project.configureCheckTasks(buildDir, buildFile, apiBuild, extension, targetConfig, commonApiDump, commonApiCheck)
project.configureCheckTasks(
buildDir,
buildFile,
apiBuild,
extension,
targetConfig,
commonApiDump,
commonApiCheck,
)
}

@Suppress("LongParameterList", "CyclomaticComplexMethod")
Expand All @@ -206,9 +230,11 @@ private fun Project.configureCheckTasks(
val apiCheck: TaskProvider<out DefaultTask>

fun configureApiCheckTask(t: DefaultTask) {
t.isEnabled = apiCheckEnabled(t.project.name, extension) && apiBuild.map { it.enabled }.getOrElse(true)
t.isEnabled = apiCheckEnabled(t.project.name, extension) && apiBuild.map { it.enabled }
.getOrElse(true)
t.group = "verification"
t.description = "Checks signatures of public API against the golden value in API folder for :${t.project.name}"
t.description =
"Checks signatures of public API against the golden value in API folder for :${t.project.name}"
t.dependsOn(apiBuild)
}

Expand All @@ -220,7 +246,8 @@ private fun Project.configureCheckTasks(
}

val apiDump = task<Sync>(apiDumpTaskName) { t ->
t.isEnabled = apiCheckEnabled(project.name, extension) && apiBuild.map { it.enabled }.getOrElse(true)
t.isEnabled = apiCheckEnabled(project.name, extension) && apiBuild.map { it.enabled }
.getOrElse(true)
t.group = "other"
t.description = "Syncs API from build dir to ${config.apiDir} dir for :${project.name}"
t.dependsOn(apiBuild)
Expand All @@ -233,7 +260,10 @@ private fun Project.configureCheckTasks(
val dirConfig = config.dirConfig?.get()
if (dirConfig is DirConfig.COMMON && dirConfig.bcvTargetName != null) {
val bcvTargetName = dirConfig.bcvTargetName
logger.info("Special handling for BCV 'DirConfig.COMMON' strategy (bcvTargetName={})", bcvTargetName)
logger.info(
"Special handling for BCV 'DirConfig.COMMON' strategy (bcvTargetName={})",
bcvTargetName,
)

val bcvCheckTaskName = apiTaskName(bcvTargetName, SUFFIX_CHECK)
val bcvBuild = tasks.named(apiTaskName(bcvTargetName, SUFFIX_BUILD))
Expand All @@ -246,7 +276,10 @@ private fun Project.configureCheckTasks(
t.doLast {
val file = buildFile.get()
if (file.delete()) {
logger.info("Removed Kotlin/JS API file for compatibility with '$bcvCheckTaskName' task: {}", file)
logger.info(
"Removed Kotlin/JS API file for compatibility with '$bcvCheckTaskName' task: {}",
file,
)
}
}
}
Expand Down Expand Up @@ -393,19 +426,20 @@ private val Project.apiValidationExtensionOrNull: ApiValidationExtension?

/** @see KotlinJsCompilation.binaries */
@Suppress("IdentifierGrammar")
private val KotlinJsCompilationBinariesCaller: (KotlinJsCompilation.() -> KotlinJsBinaryContainer) = run {
val clazz = KotlinJsCompilation::class.java
safe<Unit> {
val method = clazz.methods.firstOrNull { it.name.startsWith("getBinaries") }
?: clazz.methods.firstOrNull { "getBinaries" in it.name }
if (method != null) {
return@run { method.invoke(this) as KotlinJsBinaryContainer }
private val KotlinJsCompilationBinariesCaller: (KotlinJsCompilation.() -> KotlinJsBinaryContainer) =
run {
val clazz = KotlinJsCompilation::class.java
safe<Unit> {
val method = clazz.methods.firstOrNull { it.name.startsWith("getBinaries") }
?: clazz.methods.firstOrNull { "getBinaries" in it.name }
if (method != null) {
return@run { method.invoke(this) as KotlinJsBinaryContainer }
}
}
val field = JsIrBinary::class.java.getDeclaredField("binaries")
setAccessible(field)
return@run { field.get(this) as KotlinJsBinaryContainer }
}
val field = JsIrBinary::class.java.getDeclaredField("binaries")
setAccessible(field)
return@run { field.get(this) as KotlinJsBinaryContainer }
}

/** @see KotlinJsCompilation.binaries */
private val KotlinJsCompilation.binariesCompat: KotlinJsBinaryContainer?
Expand Down
11 changes: 7 additions & 4 deletions fluxo-bcv-js/src/main/kotlin/fluxo/bcvjs/KotlinJsApiBuildTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ internal abstract class KotlinJsApiBuildTask : DefaultTask() {

init {
// No 'group' to hide it from ./gradlew tasks.
description = "Collects built Kotlin TS definitions as API for 'js' compilations of :${project.name}. " +
"Complementary task and shouldn't be called manually"
description =
"Collects built Kotlin TS definitions as API for 'js'" +
" compilations of :${project.name}. " +
"Complementary task and shouldn't be called manually"
}

@TaskAction
Expand All @@ -58,8 +60,9 @@ internal abstract class KotlinJsApiBuildTask : DefaultTask() {
}
if (files.size > 1) {
logger.error(
"Ambigous generated TS definitions, taking only first:" +
" \n ${files.joinToString("\n ")}"
"Ambigous generated TS definitions" +
", taking only first:" +
" \n ${files.joinToString("\n ")}",
)
}

Expand Down
Loading

0 comments on commit b687b55

Please sign in to comment.