Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 tasks done
rubiesonthesky opened this issue Mar 13, 2024 · 1 comment · May be fixed by #8673
Open
4 tasks done

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

rubiesonthesky opened this issue Mar 13, 2024 · 1 comment · May be fixed by #8673
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@rubiesonthesky
Copy link
Contributor

rubiesonthesky commented Mar 13, 2024

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.

@rubiesonthesky rubiesonthesky added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Mar 13, 2024
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Mar 13, 2024
@bradzacher
Copy link
Member

I'd guess the fixer isn't considering precedence and should be using our utils to conditionally include parentheses around the expression.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Bug: [no-useless-template-literals] Auto fix will change meaning of code Bug: [no-useless-template-expression] Auto fix will change meaning of code Jun 2, 2024
@Josh-Cena Josh-Cena changed the title Bug: [no-useless-template-expression] Auto fix will change meaning of code Bug: [no-unnecessary-template-expression] Auto fix will change meaning of code Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants