Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically convert string concatenation to template literals

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.)

like image 723
Landon Kuhn Avatar asked Jul 25 '17 16:07

Landon Kuhn


1 Answers

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

like image 97
Landon Kuhn Avatar answered Sep 28 '22 07:09

Landon Kuhn