SonarSource / eslint-plugin-sonarjs

SonarJS rules for ESLint

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve 'no-nested-template-literals': do not report on multiline literal

vilchik-elena opened this issue · comments

When nesting template is multiline, it's common to nest another template. In this case we should ignore the nesting.

Note that we should ignore when both start and end of nesting template is on different line from nested literal

// this one should be ok
`foo
${ `hello`} 
bar`;

// this one should still be reported
`foo ${ `hello`} 
bar`;

Reported in internal thread https://discuss.sonarsource.com/t/fp-in-ts-sonarcloud-analysis-detecting-nested-template-literal-for-styled-emotion-components/9574/2

Hi, I think I run into a case where a multiline literal should not be allowed (as it doesn't follow the pattern described here) but it is.
I am refactoring a code base to fix all the Sonar CI warnings, and I run into a case that sonarqube reports as a Code Smell and looks like this:

`${id}${
    idSuffix
      ? `-${idSuffix}`
      : ""
  }`

NOTE: the id variable is actually named larger, and the idSuffix is a bit more complex expression, I think that is why the indenting looks like that instead of a single line

In this case the nested template is in a single line, but overall it is not. So I wanted to tackle this in the local eslint to help the team catch this error before it gets to the CI server.

Does this makes sense or is this a problem on another rule?

I can submit a PR with a change for this case if this should be part of the intended behavior.

@demianalonso I didn't get you. Does this code sample trigger the issue or not?

We are not reporting on your case, and if we are, you might need to update your SQ.

I must have misunderstood the exception to the rule. I thought that the code I presented should be reported as it was a 3 lines expression. So the exception is not that the nested template is in its own line, but if it is in a multiline expression.
So if we want to prevent this kind of nested templates, we need to add a rule of our own.

Our SQ installation most likely needs updating as you mention, it is just too many layers away from me to actually tell hehehe

Thanks for the feedback and sorry for the confusion!