admin管理员组

文章数量:1415673

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;
// bees:
const a = `b ${c}`;

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

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;
// bees:
const a = `b ${c}`;

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

Share Improve this question edited Jul 25, 2017 at 16:58 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked Jul 25, 2017 at 16:30 Landon KuhnLandon Kuhn 78.6k45 gold badges110 silver badges130 bronze badges 4
  • 1 ESLint should be able to do that. – idmean Commented Jul 25, 2017 at 16:33
  • Do you mean to say that you want a script to parse your JS code and detect and replace with template literals wherever you'd had joined strings using +? – Aakash Verma Commented Jul 25, 2017 at 16:36
  • @Aakash Verma yes – Landon Kuhn Commented Jul 25, 2017 at 16:43
  • @idmean thank you, this is the rule I was looking for: eslint/docs/rules/prefer-template – Landon Kuhn Commented Jul 25, 2017 at 16:46
Add a ment  | 

1 Answer 1

Reset to default 5

This can be done with eslint. See rule: http://eslint/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

本文标签: javascriptAutomatically convert string concatenation to template literalsStack Overflow