SonarTS icon indicating copy to clipboard operation
SonarTS copied to clipboard

Rule Idea: console.log using spaces in arguments

Open vilchik-elena opened this issue 5 years ago • 2 comments

These 2 lines doing the same thing, while second line should be preferred

console.log("Hello " + name);  // Noncompliant
console.log(name + " is my friend");  // Noncompliant

// OK
console.log("Hello", name);
console.log("Hello ${name}"); 
console.log(name, " is my friend");

// should this be Noncompliant as well?
console.log("Hello ", name);  // 2 spaces in output

vilchik-elena avatar Sep 04 '18 14:09 vilchik-elena

Sounds like what you want to do id forbid the + operator when the left hand is a string. What about string interpolation? Is it allowed? Preferred?

alexeieleusis avatar Sep 09 '18 05:09 alexeieleusis

There is tslint prefer-template which forces developer to prefer string interpolation over concatenation. This rule is only about string concatenation inside console methods. Indeed, one might prefer console.log("Hello ${name}"); to console.log("Hello", name);, but that's a matter of taste and btw there is property allow-single-concat to prefer-template.

I also thinking about case like that which might be considered as noncompliant for this rule: console.log("Hello ", name);. There will be 2 spaces in output.

Note I updated first comment with more examples

vilchik-elena avatar Sep 10 '18 10:09 vilchik-elena