Antonius Bloch
Antonius Bloch

Reputation: 2729

How to tell eslint that you prefer single quotes around your strings

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.

Upvotes: 103

Views: 201721

Answers (7)

pavones
pavones

Reputation: 25

In my case, I had not configured prettier to work with eslint. I was also trying to use jsx-quotes and quotes in eslint which are deprecated.

npm install --save-dev eslint-config-prettier

For my Vite / React project with eslint.config.js, I followed the documentation for eslint-config-prettier:

export default tseslint.config(
    eslintConfigPrettier, //added this line
    { ignores: ['dist'] },
    {
        extends: [js.configs.recommended, ...tseslint.configs.recommended],
        files: ['**/*.{ts,tsx}'],
...

Upvotes: 1

Kir Ter
Kir Ter

Reputation: 1

One more solution:

rules: {
quotes: ['warn', 'single']
}

Upvotes: 0

mtpultz
mtpultz

Reputation: 18268

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

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/quotes.md

Upvotes: 59

Anomaly
Anomaly

Reputation: 1071

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 }]
  }
}

Upvotes: 4

Mo.
Mo.

Reputation: 27475

rules: {
  'prettier/prettier': [
    'warn',
    {
      singleQuote: true,
      semi: true,
    }
  ],
},

In version "eslint": "^7.21.0"

Upvotes: 29

Allan Mwesigwa
Allan Mwesigwa

Reputation: 1298

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.

Upvotes: 6

Antonius Bloch
Antonius Bloch

Reputation: 2729

http://eslint.org/docs/rules/quotes.html

{
  "env": {
    "node": 1
  },
  "rules": {
    "quotes": [2, "single", { "avoidEscape": true }]
  }
}

Upvotes: 145

Related Questions