How to tell eslint that you prefer single quotes around your strings
node.jsEslintnode.js Problem Overview
I'm new to eslint and it's spewing out a ton of errors telling me to use doublequotes:
error Strings must use doublequote
That's not my preference. I've got an .eslintrc file set up with the basics:
{
"env": {
"node": 1
}
}
I'd like to configure it for single quotes.
node.js Solutions
Solution 1 - node.js
http://eslint.org/docs/rules/quotes.html
{
"env": {
"node": 1
},
"rules": {
"quotes": [2, "single", { "avoidEscape": true }]
}
}
Solution 2 - node.js
If you're using TypeScript/ES6 you might want to include template literals (backticks). This rule prefers single quotes, and allows template literals.
TypeScript Examples
"@typescript-eslint/quotes": [
"error",
"single",
{
"allowTemplateLiterals": true
}
]
Another useful option is to allow single or double quotes as long as the string contains an escapable quote like "lorem ipsum 'donor' eta"
or 'lorem ipsum "donor" eta'
.
"@typescript-eslint/quotes": [
"error",
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
]
References:
ESLint
https://eslint.org/docs/rules/quotes
TypeScript ESLint
Solution 3 - node.js
rules: {
'prettier/prettier': [
'warn',
{
singleQuote: true,
semi: true,
}
],
},
In version "eslint": "^7.21.0"
Solution 4 - node.js
For jsx strings, if you would like to set this rule for all files, create the rule in the eslint config file.
rules: {
'jsx-quotes': [2, 'prefer-single'],
}
Or 'prefer-double' for double quotes.
Solution 5 - node.js
If you also want to allow double quotes if they would avoid escaping a single quote inside the string, and allow template literals (the reasonable defaults, imo) try the following:
{
"env": {
"node": 1
},
"rules": {
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }]
}
}