Skip to content

Bug: [no-unnecessary-template-expression] has a few bugs around comment detection #10684

@kirkwaiblinger

Description

@kirkwaiblinger

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.7.2&fileType=.tsx&code=MYewdgzgLgBBAWICuAbAJgORFAYighgOYCMMAvDAAZj5gBcMAJAN4D0AVDAJYC2ADiABOUWrHasYGfBgC%2BlAFChIsBMnRZcBQgCZyVJGDQBTAGZcwRtAxYdu-ISLBiJB42Yto5i8NDiJUmNh4RADMepTm7lxQAJ7WbJy8AsKiMOIwAJJgUbFyQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK18iALbF4QifQAexaIMocm6KAHcRG8GAC%2BIA0A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

const shouldNotFlag1 = `nan: ${/* important */ NaN}`
const shouldNotFlag2 = `undefined: ${/* important */ undefined}`
const shouldNotFlag3 = `infinity: ${/* important */ Infinity}`

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/no-unnecessary-template-expression": "warn"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

I expected none of these lines to flag

Actual Result

These lines all flag

Additional Info

This is just a matter of the comment check should come first:

const fixableExpressionsReversed = node.expressions
.map((expression, index) => ({
expression,
nextQuasi: node.quasis[index + 1],
prevQuasi: node.quasis[index],
}))
.filter(({ expression, nextQuasi, prevQuasi }) => {
if (
isUndefinedIdentifier(expression) ||
isInfinityIdentifier(expression) ||
isNaNIdentifier(expression)
) {
return true;
}
// allow expressions that include comments
if (hasCommentsBetweenQuasi(prevQuasi, nextQuasi)) {
return false;
}
if (isLiteral(expression)) {
// allow trailing whitespace literal
if (startsWithNewLine(nextQuasi.value.raw)) {
return !(
typeof expression.value === 'string' &&
isWhitespace(expression.value)
);
}
return true;
}
if (isTemplateLiteral(expression)) {
// allow trailing whitespace literal
if (startsWithNewLine(nextQuasi.value.raw)) {
return !(
expression.quasis.length === 1 &&
isWhitespace(expression.quasis[0].value.raw)
);
}
return true;
}
return false;
})
.reverse();

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy