Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit a5bba61

Browse files
FP no-duplicate-string: ignore JS directives (#141)
1 parent 94eb063 commit a5bba61

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

docs/rules/no-duplicate-string.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Duplicated string literals make the process of refactoring error-prone, since yo
33
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
44

55
## Exceptions
6-
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.
6+
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';`
77

88
## Configuration
99

@@ -14,4 +14,4 @@ Number of times a literal must be duplicated to trigger an issue. Default is 3.
1414
"no-duplicate-string": "error",
1515
"no-duplicate-string": ["error", 5]
1616
}
17-
```
17+
```

ruling/snapshots/no-duplicate-string

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ src/jest/packages/jest-util/src/fake_timers.js: 176
167167
src/jquery/external/npo/npo.js: 5
168168
src/jquery/external/qunit/qunit.js: 986,1939,2533,2535,2581,2756,2856,3014,3100
169169
src/jquery/external/requirejs/require.js: 358,359
170-
src/jquery/external/sinon/sinon.js: 37,538,1355,2394,3129,3146,4665
170+
src/jquery/external/sinon/sinon.js: 538,1355,2394,3129,3146,4665
171171
src/jquery/src/deferred.js: 59
172172
src/react-native/IntegrationTests/ImageCachePolicyTest.js: 32,32,66
173173
src/react-native/Libraries/Components/Keyboard/Keyboard.js: 111
@@ -181,7 +181,6 @@ src/react-native/Libraries/StyleSheet/TransformPropTypes.js: 86
181181
src/react-native/Libraries/Utilities/BackAndroid.js: 25
182182
src/react-native/Libraries/Utilities/PerformanceLogger.js: 74
183183
src/react-native/Libraries/polyfills/Object.es6.js: 24
184-
src/react-native/Libraries/polyfills/String.prototype.es6.js: 20
185184
src/react-native/Libraries/vendor/core/toIterator.js: 27
186185
src/react-native/RNTester/js/AlertExample.js: 37,49
187186
src/react-native/RNTester/js/AlertIOSExample.js: 68,113

src/rules/no-duplicate-string.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const rule: Rule.RuleModule = {
4242
return {
4343
Literal: (node: Node) => {
4444
const literal = node as SimpleLiteral;
45-
if (typeof literal.value === "string") {
45+
const parent = getParent(context);
46+
if (typeof literal.value === "string" && (parent && parent.type !== "ExpressionStatement")) {
4647
const stringContent = literal.value.trim();
4748

4849
if (

tests/rules/no-duplicate-string.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ ruleTester.run("no-duplicate-string", rule, {
133133
"some property": 1
134134
};`,
135135
},
136+
{
137+
code: `
138+
'use strict';
139+
'use strict';
140+
'use strict';
141+
`,
142+
},
136143
],
137144
invalid: [
138145
{

0 commit comments

Comments
 (0)