sindresorhus / eslint-plugin-unicorn

More than 100 powerful ESLint rules
MIT License
3.98k stars 360 forks source link

Rule proposal: warn about missing dollar sign surrounding curly braces in template string #2150

Open cmdcolin opened 1 year ago

cmdcolin commented 1 year ago

Description

I migrated some code from React to template strings, but missed adding the $ sign around the curly braces so I had

Hello {name} instead of Hello ${name}

I did not see any existing eslint rules that warn against this. I also wasn't sure what the best rule set to propose this rule to, but I like eslint-plugin-unicorn a lot and wasn't sure this quite had the 'universality' that it would be a core eslint rule.

Somewhat similar rule in eslint core rule set https://eslint.org/docs/latest/rules/no-template-curly-in-string#examples

They say when proposing a new rule to eslint core to justify e.g. "Why should this rule be in the core instead of a plugin?

In general, we prefer that rules be implemented in plugins where they can be tailored to your specific use case.". not sure this rule has enough justification?

Fail

var replace = `Hello {name}`;

Pass

const replace = `Hello ${name}`;

Additional Info

No response

sindresorhus commented 11 months ago

Can you also propose a rule name?

cmdcolin commented 11 months ago

unicorn/missing-template-dollar-sign maybe?

sindresorhus commented 11 months ago

How about unicorn/no-incorrect-template-string-interpolation?

fisker commented 11 months ago

no-mismatched-template-literal-curly?

fisker commented 11 months ago

We can also check

var replace = `Hello $name}`;
var replace = `Hello ${name`;
sindresorhus commented 11 months ago

no-mismatched-template-literal-curly?

I think no-incorrect-template-string-interpolation clearer as the intent is to catch mistakes in template string interpolation.