0

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • Hi, this seems pretty cool. Where do you define such patterns? (I want that too :) ). Is that some code-style plugin? – guenhter Aug 24 '17 at 05:36
  • @guenhter yes, this a gradle plugin for statis analysis: https://github.com/arturbosch/detekt :) – jonathanrz Aug 24 '17 at 13:51

1 Answers1

2

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_$]*'
Mauin
  • 483
  • 3
  • 12
  • You just missed the dot from the package names. The updated regex is the following: packagePattern: '[a-zA-Z_$]*' – jonathanrz Aug 23 '17 at 20:12