Skip to content

Bug: [no-unnecessary-template-expression] Auto fix will change meaning of code #8669

Closed
@rubiesonthesky

Description

@rubiesonthesky

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.3.0-beta&fileType=.tsx&code=MYGwhgzhAECC0G8BQ1oQC5nQS2NAZgPYBOAtlgBQZYCuEAXItAHZikCmjGx2zA5tAA%2B0GswAm7fL3ZjoAXyEjxk6WICUXdD36LREqcxmIUqaMXboaxZmkyWIAfgB0rDtAfQABgBIE1ey5s7ELCAOShcp5OWtikFGrQjHoqhmIA3CZySHJAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGYkACAXAngA4oDG0AlobgLQrzkB2uA9AwPbWzKJLLLW5EAW0LwAhoOr1B0MfFQZIiaNDbRIAXxAagA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYwAviNUA&tokens=false

Repro Code

class A {
  static format(status: { name: string | undefined } | undefined): string | undefined {
    return status?.name ? `${status.name || ''}`.trim() : undefined;
  }
}

ESLint Config

module.exports = {
  "rules": {"@typescript-eslint/no-useless-template-literals": "error"}
}

tsconfig

{
  "compilerOptions": {}
}

Expected Result

class A {
  static format(status: { name: string | undefined } | undefined): string | undefined {
    return status?.name ? (status.name || '').trim() : undefined;
  }
}

I think the autofixer should have added parens when it removed the template literal.

Actual Result

class A {
  static format(status: { name: string | undefined } | undefined): string | undefined {
    return status?.name ? status.name || ''.trim() : undefined;
  }
}

Additional Info

This seems to repro in Playground with or without strictNullChecks. I'm my project, I was not using strictNullChecks.

The autofix highlights that the original code is silly.

I tested this also with minimal eslint config, and it still happens.

Edit: I changed Playground link to be most minimal rule set & tsConfig.

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

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions