kotlindetekt

detekt snake case package


I added detekt to my app and it is complaining about my package names conventions.

I use the package snake_cased and the class CamelCased.

For example:

package com.my_package

class MyClass

And the output from detekt is the following:

NamingConventionViolation - [MyClass.kt] at com/my_package/MyClass.kt:1:1

I have the following as my naming conventions configuration:

NamingConventionViolation:
    active: true
    variablePattern: '^(_)?[a-z$][a-zA-Z$0-9]*$'
    constantPattern: '^([A-Z_]*|serialVersionUID)$'
    methodPattern: '^[a-z$][a-zA-Z$0-9]*$'
    classPattern: '[A-Za-z$][a-zA-Z_.$]*'
    enumEntryPattern: '^[A-Z$][a-zA-Z_$]*$'

I changed the default class Pattern to add the possibility to start with lower case and have _ in the class name because I understood that detekt is validating the FQN and not only the name.

So, my question is: how can I set the pattern that detekt uses for package names?


Solution

  • You can set your own pattern for package names with the packagePattern parameter on the NamingConventionViolation rule.

    e.g.:

    NamingConventionViolation:
        active: true
        variablePattern: '^(_)?[a-z$][a-zA-Z$0-9]*$'
        constantPattern: '^([A-Z_]*|serialVersionUID)$'
        methodPattern: '^[a-z$][a-zA-Z$0-9]*$'
        classPattern: '[A-Za-z$][a-zA-Z_.$]*'
        enumEntryPattern: '^[A-Z$][a-zA-Z_$]*$'
        packagePattern: '[a-zA-Z_$]*'