5

I have a code base with many strings built via string concatenation. Is there a automated method for replacing all instances of string concatenation with templates? For example:

const a = 'b ' + c;
// becomes:
const a = `b ${c}`;

A script-based solution would be awesome. An editor plugin would be even better. (I am using Visual Studio Code.)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130

1 Answers1

5

This can be done with eslint. See rule: http://eslint.org/docs/rules/prefer-template.

The following will run a single rule on the src directory and fix any errors. The rule is a string encoded JSON-like value. The values in the array are 0 - ignore, 1 - warn, 2 - error. eslint ./src --rule '{prefer-template:[2]}' --fix

Similarly, if you are using TypeScript tslint can do something similar, although it doesn't seem to be able to just have a single rule specified: tslint --config ./tslint.json --project ./tsconfig.json --fix

Nigel
  • 1,203
  • 2
  • 11
  • 20
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
  • Glad it worked out for you! It'd be great if you could help beginners like me by elaborating on how to use it. Thanks! – Aakash Verma Jul 25 '17 at 16:54