π Enforce better string content.
π« This rule is disabled in the following configs: β
recommended, βοΈ unopinionated.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
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.
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!';
// β
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.