eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rule proposal: `no-template-in-string`

Open DaniFoldi opened this issue 1 year ago • 1 comments

Description

Sometimes when I'm working with error messages, I'll have something like Failed to add user to _something_. In some cases it will be more detailed, but not from the start, so when adding a username, such as ${user.name} to the error message, I forget to convert it to a template string.

I believe that most of the cases when a template pattern is found within a single- or double-quoted string, it's an error, and should be a template string.

Fail

let message = 'Hello, ${user.name}'
greet('${user.firstName} ${user.lastName}')
let message = "Hello, ${user.name}"
greet("${user.firstName} ${user.lastName}")

Pass

let message = `Hello, ${user.name}`
greet(`${user.firstName} ${user.lastName}`)

Additional Info

As this would potentially result in some (albeit arguably rare) strings that require single or double quotes, I wouldn't make it a part of the recommended config.

DaniFoldi avatar Aug 11 '22 07:08 DaniFoldi

https://eslint.org/docs/latest/rules/no-template-curly-in-string

dimaMachina avatar Aug 11 '22 09:08 dimaMachina

Ah, thanks. I guess I didn't expect it to be a core eslint rule, hence searching and opening an issue here.

DaniFoldi avatar Aug 11 '22 10:08 DaniFoldi