Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upEnable "Convert to template string" on expressions that don't start with a string #40671
Comments
The error/warning is coming from your linter, not VS Code. Also, I've update the issue title so this issue only tracks enabling |
Self contained example: const obj = { prop: '123' };
const z = obj.prop + 'b' + 'c'
|
Not that familiar with the code, but sounds like this would be a matter of checking for other node kinds, (e.g. Identifier, PropertyAccess, ElementAccess, ParenthesizedExpressions), skipping those nodes until you reach something else, and checking if that node is a |
Perhaps worth clarifying (as the subject changed and is not 100% accurate from my observations), the refactoring to template literal will work if the first part of the expression is anything other than a dotted object.property reference. In addition to string literals, simple variables, and numeric literals all work -- not just bare "strings".
It is only obj.prop at the beginning that will cause it to just shrug and make you do it manually. Thanks @mjbvz for pointing out that it is tslint raising the refactoring suggestion. I understand tslint is being deprecated for eslint, but I've had no luck converting a large project over to eslint despite following the migration guide. I'm unable to test if eslint has this same issue. If not, perhaps the fix lies there. And if tslint v1.2.3 is still being maintained by Microsoft, it would be nice if clicking the |
Is this a good issue to get into the TypeScript codebase? Unwound line to contribute to TypeScript during Hacktoberfest and if it makes sense would start with this issue :) |
TS Template added by @mjbvz
TypeScript Version: 4.1.0-dev.20200916
Search Terms
(see comment for condensed repro of the problem)
Issue Type: Bug
Please consider the following JavaScript example:
The editor will raise a suggestion that the concatenation expression should be a template literal. But when you peek the problem in expectation of finding a quick fix for "Convert to template string", none appears.
However, if the first part of the expression is anything other than an object reference, it works as expected:
Additionally, Quick Fix interactions, depending on how you engage them, have inconsistent behaviors:
If you click on the Quick Fix link...
...this is all you get:
But if you type Control + . (Period) you get the menu with the "Convert to template" option:
Similarly, if you click on the lightbulb icon, you also get the option:
VS Code version: Code 1.49.1 (58bb7b2331731bf72587010e943852e13e6fd3cf, 2020-09-16T23:27:51.792Z)
OS version: Windows_NT x64 10.0.19041
System Info
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: disabled_off
opengl: enabled_on
protected_video_decode: enabled
rasterization: enabled
skia_renderer: disabled_off_ok
video_decode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
Extensions (20)
(1 theme extensions excluded)