Skip to content

Commit

Permalink
FP no-duplicate-string: ignore JS directives (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource authored and vilchik-elena committed Nov 7, 2019
1 parent 94eb063 commit a5bba61
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/rules/no-duplicate-string.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Duplicated string literals make the process of refactoring error-prone, since yo
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

## Exceptions
To prevent generating some false-positives, literals having less than 10 characters are excluded as well as literals matching /^\w*$/. String literals inside import/export statements are also ignored.
To prevent generating some false-positives, literals having less than 10 characters are excluded as well as literals matching /^\w*$/. String literals inside import/export statements are also ignored. The same goes for statement-like string literals, e.g. `'use strict';`

## Configuration

Expand All @@ -14,4 +14,4 @@ Number of times a literal must be duplicated to trigger an issue. Default is 3.
"no-duplicate-string": "error",
"no-duplicate-string": ["error", 5]
}
```
```
3 changes: 1 addition & 2 deletions ruling/snapshots/no-duplicate-string
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ src/jest/packages/jest-util/src/fake_timers.js: 176
src/jquery/external/npo/npo.js: 5
src/jquery/external/qunit/qunit.js: 986,1939,2533,2535,2581,2756,2856,3014,3100
src/jquery/external/requirejs/require.js: 358,359
src/jquery/external/sinon/sinon.js: 37,538,1355,2394,3129,3146,4665
src/jquery/external/sinon/sinon.js: 538,1355,2394,3129,3146,4665
src/jquery/src/deferred.js: 59
src/react-native/IntegrationTests/ImageCachePolicyTest.js: 32,32,66
src/react-native/Libraries/Components/Keyboard/Keyboard.js: 111
Expand All @@ -181,7 +181,6 @@ src/react-native/Libraries/StyleSheet/TransformPropTypes.js: 86
src/react-native/Libraries/Utilities/BackAndroid.js: 25
src/react-native/Libraries/Utilities/PerformanceLogger.js: 74
src/react-native/Libraries/polyfills/Object.es6.js: 24
src/react-native/Libraries/polyfills/String.prototype.es6.js: 20
src/react-native/Libraries/vendor/core/toIterator.js: 27
src/react-native/RNTester/js/AlertExample.js: 37,49
src/react-native/RNTester/js/AlertIOSExample.js: 68,113
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-duplicate-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const rule: Rule.RuleModule = {
return {
Literal: (node: Node) => {
const literal = node as SimpleLiteral;
if (typeof literal.value === "string") {
const parent = getParent(context);
if (typeof literal.value === "string" && (parent && parent.type !== "ExpressionStatement")) {
const stringContent = literal.value.trim();

if (
Expand Down
7 changes: 7 additions & 0 deletions tests/rules/no-duplicate-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ ruleTester.run("no-duplicate-string", rule, {
"some property": 1
};`,
},
{
code: `
'use strict';
'use strict';
'use strict';
`,
},
],
invalid: [
{
Expand Down

0 comments on commit a5bba61

Please sign in to comment.