Enforce certain things about the contents of strings. For example, you can enforce using ’
instead of '
to avoid escaping. Or you could block some words. The possibilities are endless.
This rule is fixable.
It only reports one pattern per AST node at the time.
This rule ignores the following tagged template literals as they're known to contain code:
gql`…`
html`…`
svg`…`
styled.*`…`
This rule has no effect by default. You need set patterns
to check string content.
/*eslint unicorn/string-content: ["error", { "patterns": { "'": "’" } }]*/
const foo = 'Someone\'s coming!';
/*eslint unicorn/string-content: ["error", { "patterns": { "'": "’" } }]*/
const foo = 'Someone’s coming!';
Type: object
Type: object
The example below:
- Adds a custom
unicorn
→🦄
replacement. - Adds a custom
awesome
→😎
replacement and a custom message. - Adds a custom
cool
→😎
replacement, but disables auto fix.
{
"unicorn/string-content": [
"error",
{
"patterns": {
"unicorn": "🦄",
"awesome": {
"suggest": "😎",
"message": "Please use `😎` instead of `awesome`."
},
"cool": {
"suggest": "😎",
"fix": false
}
}
}
]
}
The key of patterns
is treated as a regex, so you must escape special characters.
For example, if you want to enforce ...
→ …
:
{
"patterns": {
"\\.\\.\\.": "…"
}
}
- Enforce
’
over'
to avoid escape. - Enforce
…
over...
for better typography. - Enforce
→
over->
for better typography. - Enforce
^https:\\/\\/
over^http:\\/\\/
to secure your links.