自动将字符串连接转换为模板文字

Lan*_*uhn 5 javascript code-conversion ecmascript-6 template-strings template-literals

我有一个代码库,其中包含许多通过字符串连接构建的字符串。是否有用模板替换所有字符串连接实例的自动化方法?例如:

const a = 'b ' + c;
// becomes:
const a = `b ${c}`;
Run Code Online (Sandbox Code Playgroud)

基于脚本的解决方案会很棒。一个编辑器插件会更好。(我正在使用 Visual Studio 代码。)

Lan*_*uhn 5

这可以用 eslint 来完成。参见规则:http : //eslint.org/docs/rules/prefer-template

以下将在src目录上运行单个规则并修复任何错误。该规则是一个字符串编码的类似 JSON 的值。数组中的值是 0 - 忽略,1 - 警告,2 - 错误。 eslint ./src --rule '{prefer-template:[2]}' --fix

同样,如果您使用 TypeScripttslint可以做类似的事情,尽管它似乎不能只指定一个规则: tslint --config ./tslint.json --project ./tsconfig.json --fix