eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Rule proposal: `no-template-in-string`
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.
https://eslint.org/docs/latest/rules/no-template-curly-in-string
Ah, thanks. I guess I didn't expect it to be a core eslint rule, hence searching and opening an issue here.