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

UnderscoresInNumericLiterals acceptableDecimalLength is off by one #3972

Merged
merged 2 commits into from
Sep 29, 2021

Conversation

BraisGabin
Copy link
Member

This rule was using the word "decimal" everywhere. It's correct to call a base 10 number "decimal" but it is saying the same thing twice. And the problem with "decimal" is that it have a lot of meanings so it can be missleading. For that reason I changed the documentation a bit removing the word "decimal" (first commit).

After that the only point where "decimal" was used is in the configuration so to avoid the breaking change and because it also improves "readability" I renamed "acceptableDecimalLength" to "acceptableLenght". All this rule is about base 10 numbers so there's is no need to point that out in the configuration too.

But this is a Draft an it's not a ready to merge PR because I found a missing feature in our new configuration system:

private val acceptableLength: Int by configWithFallback("acceptableDecimalLength", 4)

We can't use the value of acceptableDecimalLength as the value of acceptableLenght because it would be off by one. Should we care about this? We were talking about adding a "breaking change" to fix this so, maybe, we should not care.

Fix #3860

@marschwar
Copy link
Contributor

I think we should care and not introduce a breaking change like that.

I agree that the fallback config property is missing some functionality. I was looking into it and ended up with something like

    @Configuration("Length under which decimal base 10 literals are not required to have underscores")
    @Deprecated("Use `acceptableLength` instead. Make sure to read the documentation as the logic has changed.")
    private val acceptableDecimalLength: Int by config(5)

    @Configuration("Maximum number of digits that a number can have and not use underscores")
    private val acceptableLength: Int by configWithFallback(::acceptableDecimalLength, 4)

I know that @chao2zhang was asking about something like this from the beginning but at the time I could not get this to work.

In the implementation of the config property I get the message: Call uses reflection API which is not found in compilation classpath. Make sure you have kotlin-reflect.jar in the classpath I am unsure if it is safe to ignore this warning. I will create a PR to illustrate the solution.

@BraisGabin
Copy link
Member Author

That api doesn't allow me to convert from one value to the other, I mean, do the - 1

@marschwar
Copy link
Contributor

🤔 You could do val acceptableDecimalLength: Int by config(5) { it - 1 }. So whenever the fallback property is used, the value is reduced by one. (I have not tested that yet).

@BraisGabin
Copy link
Member Author

🤔 You could do val acceptableDecimalLength: Int by config(5) { it - 1 }. So whenever the fallback property is used, the value is reduced by one. (I have not tested that yet).

Good point! That have sense.

private val acceptableDecimalLength: Int by config(DEFAULT_ACCEPTABLE_DECIMAL_LENGTH)
@Configuration("Length under which base 10 numbers are not required to have underscores")
@Deprecated("Use acceptableLength instead.")
private val acceptableDecimalLength: Int by config(5) { it - 1 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current implementation of the configWithFallback this will not work. This relies on #3982 in order to work.

@BraisGabin BraisGabin added this to the 1.19.0 milestone Aug 10, 2021
It have multiple meanings:
base 10
decimal point
first digit after the "decimal point"
...

https://en.wikipedia.org/wiki/Decimal_(disambiguation)
@BraisGabin
Copy link
Member Author

Rebased and now it uses the new version of configWithFallback.

@BraisGabin BraisGabin marked this pull request as ready for review September 14, 2021 19:29
@codecov
Copy link

codecov bot commented Sep 14, 2021

Codecov Report

Merging #3972 (e7c5c47) into main (7962979) will increase coverage by 0.00%.
The diff coverage is 83.33%.

❗ Current head e7c5c47 differs from pull request most recent head 27117e6. Consider uploading reports for the commit 27117e6 to get more accurate results
Impacted file tree graph

@@            Coverage Diff            @@
##               main    #3972   +/-   ##
=========================================
  Coverage     83.48%   83.48%           
  Complexity     3195     3195           
=========================================
  Files           461      461           
  Lines          9112     9113    +1     
  Branches       1774     1774           
=========================================
+ Hits           7607     7608    +1     
  Misses          571      571           
  Partials        934      934           
Impacted Files Coverage Δ
...detekt/rules/style/UnderscoresInNumericLiterals.kt 81.57% <83.33%> (+0.49%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7962979...27117e6. Read the comment docs.

@marschwar
Copy link
Contributor

marschwar commented Sep 14, 2021

Please regenerate the default config to use the the property name.

Should we also update the test to use the new config property and keep only one or two with the deprecated value?

@BraisGabin
Copy link
Member Author

Please regenerate the default config to use the the property name.

And not only the default config. The deprecation list too! We merged the PR just in time 😂.

Should we also update the test to use the new config property and keep only one or two with the deprecated value?

You are right! Done :)

@BraisGabin BraisGabin added the migration Marker to add a migration step in the changelog label Sep 29, 2021
@BraisGabin BraisGabin merged commit f920504 into main Sep 29, 2021
@BraisGabin BraisGabin deleted the fix-3860 branch September 29, 2021 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
migration Marker to add a migration step in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UnderscoresInNumericLiterals acceptableDecimalLength is off by one
2 participants