Skip to main content
Version: 1.23.6

Configuration for Compose

Relevant rule sets and their configuration options for Compose styles & usage. The following are being used as reference for Compose usage:

FunctionNaming for Compose

See FunctionNaming.

@Composable functions that return Unit are named using PascalCase. detekt may see this as a violation:

@Composable
fun FooButton(text: String, onClick: () -> Unit) { // Violation for FooButton()

Choose either of the following options:

  • Augment default functionPattern to '[a-zA-Z][a-zA-Z0-9]*' (default is: '[a-z][a-zA-Z0-9]*')
  • Set ignoreAnnotated to ['Composable']

TopLevelPropertyNaming for Compose

See TopLevelPropertyNaming.

Compose guidelines prescribe CamelCase for top-level constants.

Default Style:
private val FOO_PADDING = 16.dp
Compose Style:
private val FooPadding = 16.dp
  • Set constantPattern to '[A-Z][A-Za-z0-9]*' (default is: '[A-Z][_A-Z0-9]*')

LongParameterList for Compose

See LongParameterList.

Composables may boast more than the typical number of function arguments (albeit mostly with default values). For example, see OutlinedTextField.

  • Set functionThreshold to a higher value
  • Additionally, can set ignoreDefaultParameters = true

MagicNumber for Compose

See MagicNumber.

Class/companion object/top-level properties that declare objects such as Color(0xFFEA6D7E) may be considered violations if they don't specify the named parameter (i.e. Color(color = 0xFFEA6D7E)).

val color1 = Color(0xFFEA6D7E) // Violation

class Foo {
val color2 = Color(0xFFEA6D7E) // Violation

companion object {
val color3 = Color(0xFFEA6D7E) // No violation if ignoreCompanionObjectPropertyDeclaration = true by default
}
}
  • Set ignorePropertyDeclaration = true, ignoreCompanionObjectPropertyDeclaration = true (default)

UnusedPrivateMember for Compose

See UnusedPrivateMember.

detekt may see composable preview functions, i.e. those marked with @Preview, as unused.

@Preview
@Composable
private fun FooLazyColumnPreview() { // Violation for FooLazyColumnPreview()
FooLazyColumn()
}
  • Set ignoreAnnotated to ['Preview']

TooManyFunctions for Compose

See TooManyFunctions.

detekt may flag files with many composable preview functions, i.e. those marked with @Preview, as having too many functions. Since preview functions do not contribute to complexity, this might not be desired.

  • Set ignoreAnnotatedFunctions to ['Preview']