0

I'm using the CodeNarc plug for Gradle with CodeNarc 1.1. I am using the DuplicateStringLiteral rule and passing in ignoreStrings: ['1', '2'] in the config file but the rule is not respecting the values I'm passing in. It's a groovy config file, section looks like:

DuplicateStringLiteral (
        ignoreStrings: ['1', '2']
)

I am also using DuplicateNumberLiteral and defined the ignoreNumbers property the same way and that seems to be working fine, so I don't think I have the syntax wrong. The report seems to pick up the ignored values as well as it shows this line in the definition for the rule:

The ignoreStrings property ([1, 2]) can optionally specify a comma-separated list of Strings to ignore.

Anyone see what I'm doing wrong here?

nateha1984
  • 99
  • 3
  • 14

1 Answers1

2

The documentation shows the default value of ignoreStrings to be an empty String... not an empty List. This makes me think that when it says The optional comma-separated list of Strings that should be ignored (i.e., not cause a violation)., it means to put the list (note lack of capitalization) of comma-separated strings in a single String value for the property.

TL;DR Try:

DuplicateStringLiteral (
        ignoreStrings: '1,2'
)
billjamesdev
  • 14,554
  • 6
  • 53
  • 76
  • That was it. Interesting, I would have thought that would be treated as one string. Must have to escape the comma somehow if you want your string. I tried a few variations and was unable to get a string that had a comma in it ignored. Thanks for your help! – nateha1984 Jun 08 '18 at 12:54
  • You might consider *not* populating this rule (we don't). If you have classes that just need to have duplicate literals, consider using `@SuppressWarnings('DuplicateStringLiteral')` annotating the class/methods involved. This allows the rule to still work, and gives you an annotation to look for and justify during code reviews. – billjamesdev Jun 08 '18 at 15:51