Skip to content

Commit

Permalink
Make string-content have no default patterns (#637)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus authored Mar 30, 2020
1 parent f551add commit 08fad55
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 111 deletions.
6 changes: 6 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage:
status:
project:
default:
target: 99%
threshold: 1%
19 changes: 9 additions & 10 deletions docs/rules/string-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ This rule ignores the following tagged template literals as they're known to con
- ``svg`…` ``
- ``styled.*`…` ``

**This rule has no effect by default. You need set [`patterns`](#patterns) to check string content.**

## Fail

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

## Pass

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

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

Type: `object`

Extend [default patterns](#default-pattern).

The example below:

- Disables the default `'``` replacement.
- Adds a custom `unicorn``🦄` replacement.
- Adds a custom `awesome``😎` replacement and a custom message.
- Adds a custom `cool``😎` replacement, but disables auto fix.
Expand All @@ -48,7 +49,6 @@ The example below:
"error",
{
"patterns": {
"'": false,
"unicorn": "🦄",
"awesome": {
"suggest": "😎",
Expand Down Expand Up @@ -76,10 +76,9 @@ For example, if you want to enforce `...` → `…`:
}
```

## Default Pattern
## Pattern ideas

```json
{
"'": ""
}
```
- Enforce `` over `'` to avoid escape.
- Enforce `` over `...` for better typography.
- Enforce `` over `->` for better typography.
- Enforce `^https:\\/\\/` over `^http:\\/\\/` to secure your links.
62 changes: 28 additions & 34 deletions rules/string-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ const quoteString = require('./utils/quote-string');
const replaceTemplateElement = require('./utils/replace-template-element');
const escapeTemplateElementRaw = require('./utils/escape-template-element-raw');

const defaultPatterns = {
'\'': '’'
};

const ignoredIdentifier = new Set([
'gql',
'html',
Expand Down Expand Up @@ -43,13 +39,10 @@ const isIgnoredTag = node => {
};

const defaultMessage = 'Prefer `{{suggest}}` over `{{match}}`.';
const SUGGESTION_MESSAGE_ID = 'replace';

function getReplacements(patterns) {
return Object.entries({
...defaultPatterns,
...patterns
})
.filter(([, options]) => options !== false)
return Object.entries(patterns)
.map(([match, options]) => {
if (typeof options === 'string') {
options = {
Expand Down Expand Up @@ -84,14 +77,11 @@ const create = context => {
let string;
if (type === 'Literal') {
string = node.value;
if (typeof string !== 'string') {
return;
}
} else if (!isIgnoredTag(node)) {
string = node.value.raw;
}

if (!string) {
if (!string || typeof string !== 'string') {
return;
}

Expand All @@ -101,33 +91,39 @@ const create = context => {
return;
}

const {fix, message = defaultMessage, match, suggest} = replacement;
const {fix: autoFix, message = defaultMessage, match, suggest} = replacement;
const messageData = {
match,
suggest
};
const problem = {
node,
message,
data: {
match,
suggest
}
data: messageData
};

if (!fix) {
context.report(problem);
return;
}

const fixed = string.replace(replacement.regex, suggest);
if (type === 'Literal') {
problem.fix = fixer => fixer.replaceText(
const fix = type === 'Literal' ?
fixer => fixer.replaceText(
node,
quoteString(fixed, node.raw[0])
);
} else {
problem.fix = fixer => replaceTemplateElement(
) :
fixer => replaceTemplateElement(
fixer,
node,
escapeTemplateElementRaw(fixed)
);

if (autoFix) {
problem.fix = fix;
} else {
problem.suggest = [
{
messageId: SUGGESTION_MESSAGE_ID,
data: messageData,
fix
}
];
}

context.report(problem);
Expand All @@ -143,11 +139,6 @@ const schema = [
type: 'object',
additionalProperties: {
anyOf: [
{
enum: [
false
]
},
{
type: 'string'
},
Expand Down Expand Up @@ -186,6 +177,9 @@ module.exports = {
url: getDocumentationUrl(__filename)
},
fixable: 'code',
schema
schema,
messages: {
[SUGGESTION_MESSAGE_ID]: 'Replace `{{match}}` with `{{suggest}}`.'
}
}
};
Loading

0 comments on commit 08fad55

Please sign in to comment.