Skip to content

Commit 08fad55

Browse files
fiskersindresorhus
andauthored
Make string-content have no default patterns (#637)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent f551add commit 08fad55

File tree

4 files changed

+169
-111
lines changed

4 files changed

+169
-111
lines changed

codecov.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 99%
6+
threshold: 1%

docs/rules/string-content.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ This rule ignores the following tagged template literals as they're known to con
1313
- ``svg`…` ``
1414
- ``styled.*`…` ``
1515

16+
**This rule has no effect by default. You need set [`patterns`](#patterns) to check string content.**
17+
1618
## Fail
1719

1820
```js
21+
/*eslint unicorn/string-content: ["error", { "patterns": { "'": "’" } }]*/
1922
const foo = 'Someone\'s coming!';
2023
```
2124

2225
## Pass
2326

2427
```js
28+
/*eslint unicorn/string-content: ["error", { "patterns": { "'": "’" } }]*/
2529
const foo = 'Someone’s coming!';
2630
```
2731

@@ -33,11 +37,8 @@ Type: `object`
3337

3438
Type: `object`
3539

36-
Extend [default patterns](#default-pattern).
37-
3840
The example below:
3941

40-
- Disables the default `'``` replacement.
4142
- Adds a custom `unicorn``🦄` replacement.
4243
- Adds a custom `awesome``😎` replacement and a custom message.
4344
- Adds a custom `cool``😎` replacement, but disables auto fix.
@@ -48,7 +49,6 @@ The example below:
4849
"error",
4950
{
5051
"patterns": {
51-
"'": false,
5252
"unicorn": "🦄",
5353
"awesome": {
5454
"suggest": "😎",
@@ -76,10 +76,9 @@ For example, if you want to enforce `...` → `…`:
7676
}
7777
```
7878

79-
## Default Pattern
79+
## Pattern ideas
8080

81-
```json
82-
{
83-
"'": ""
84-
}
85-
```
81+
- Enforce `` over `'` to avoid escape.
82+
- Enforce `` over `...` for better typography.
83+
- Enforce `` over `->` for better typography.
84+
- Enforce `^https:\\/\\/` over `^http:\\/\\/` to secure your links.

rules/string-content.js

+28-34
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ const quoteString = require('./utils/quote-string');
44
const replaceTemplateElement = require('./utils/replace-template-element');
55
const escapeTemplateElementRaw = require('./utils/escape-template-element-raw');
66

7-
const defaultPatterns = {
8-
'\'': '’'
9-
};
10-
117
const ignoredIdentifier = new Set([
128
'gql',
139
'html',
@@ -43,13 +39,10 @@ const isIgnoredTag = node => {
4339
};
4440

4541
const defaultMessage = 'Prefer `{{suggest}}` over `{{match}}`.';
42+
const SUGGESTION_MESSAGE_ID = 'replace';
4643

4744
function getReplacements(patterns) {
48-
return Object.entries({
49-
...defaultPatterns,
50-
...patterns
51-
})
52-
.filter(([, options]) => options !== false)
45+
return Object.entries(patterns)
5346
.map(([match, options]) => {
5447
if (typeof options === 'string') {
5548
options = {
@@ -84,14 +77,11 @@ const create = context => {
8477
let string;
8578
if (type === 'Literal') {
8679
string = node.value;
87-
if (typeof string !== 'string') {
88-
return;
89-
}
9080
} else if (!isIgnoredTag(node)) {
9181
string = node.value.raw;
9282
}
9383

94-
if (!string) {
84+
if (!string || typeof string !== 'string') {
9585
return;
9686
}
9787

@@ -101,33 +91,39 @@ const create = context => {
10191
return;
10292
}
10393

104-
const {fix, message = defaultMessage, match, suggest} = replacement;
94+
const {fix: autoFix, message = defaultMessage, match, suggest} = replacement;
95+
const messageData = {
96+
match,
97+
suggest
98+
};
10599
const problem = {
106100
node,
107101
message,
108-
data: {
109-
match,
110-
suggest
111-
}
102+
data: messageData
112103
};
113104

114-
if (!fix) {
115-
context.report(problem);
116-
return;
117-
}
118-
119105
const fixed = string.replace(replacement.regex, suggest);
120-
if (type === 'Literal') {
121-
problem.fix = fixer => fixer.replaceText(
106+
const fix = type === 'Literal' ?
107+
fixer => fixer.replaceText(
122108
node,
123109
quoteString(fixed, node.raw[0])
124-
);
125-
} else {
126-
problem.fix = fixer => replaceTemplateElement(
110+
) :
111+
fixer => replaceTemplateElement(
127112
fixer,
128113
node,
129114
escapeTemplateElementRaw(fixed)
130115
);
116+
117+
if (autoFix) {
118+
problem.fix = fix;
119+
} else {
120+
problem.suggest = [
121+
{
122+
messageId: SUGGESTION_MESSAGE_ID,
123+
data: messageData,
124+
fix
125+
}
126+
];
131127
}
132128

133129
context.report(problem);
@@ -143,11 +139,6 @@ const schema = [
143139
type: 'object',
144140
additionalProperties: {
145141
anyOf: [
146-
{
147-
enum: [
148-
false
149-
]
150-
},
151142
{
152143
type: 'string'
153144
},
@@ -186,6 +177,9 @@ module.exports = {
186177
url: getDocumentationUrl(__filename)
187178
},
188179
fixable: 'code',
189-
schema
180+
schema,
181+
messages: {
182+
[SUGGESTION_MESSAGE_ID]: 'Replace `{{match}}` with `{{suggest}}`.'
183+
}
190184
}
191185
};

0 commit comments

Comments
 (0)