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
1 Answer
Reset to default 5This 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
版权声明:本文标题:javascript - Automatically convert string concatenation to template literals - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745203419a2647509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论