sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-useless-interpolation`

Chiusa

#1261 aperta il 11 mag 2021

 (16 commenti) (29 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

Hi there! 👋 First of all, thanks again for this plugin, super helpful, the things available in this plugin.

I wanted to propose a new rule to address a pattern that I have seen from a lot of our students at @upleveled - writing overly complex patterns with unnecessary interpolations in template strings (eg. a single string variable, or a string literal, or multiple string literals).

Fail

const withString = `${str}`;
const withStringLiteral = `${'abc'}`;
const withMultipleStringLiterals = `${'abc'}${'def'}`;

Pass

const withString = str;
const withStringLiteral = 'abc';
const withMultipleStringLiterals = 'abcdef';

Implementation ideas

Looking into the ASTExplorer, I guess the simple version of the first failure case below would be:

  • only 1 element in TemplateLiteral.expressions (an Identifier)
  • only 2 elements in TemplateLiteral.quasis, all empty strings

For the literals, I guess no limit to the quasis, but every element in expressions should be a Literal.

Guida contributor