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

Rule proposal: Prevent / Warns if the code attempts to interpolate functions directly into string template #5600

Closed
6 tasks done
chenxinyanc opened this issue Sep 5, 2022 · 1 comment
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@chenxinyanc
Copy link

chenxinyanc commented Sep 5, 2022

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

This rule prevents potential erroneous implicit Function to string conversion in string interpolation expressions. Converting a function (or class itself) into string will cause function body being inserted into the string template, which could be undesirable in most of the cases.

I think we can leverage TypeScript typing information for the "Function" type checking. We don't need to cover the case where the interpolated value does not have Function type at compile-time.

Fail Cases

const formatErrorMessage = (error: unknown) => String(error);
// `formatErrorMessage` is of function type. Converting it into string results in function body being printed.
const message = "Error is {formatErrorMessage}";

Pass Cases

const formatErrorMessage = (error: unknown) => String(Error);
// Call the function to return the string result.
const message1 = "Error is {formatErrorMessage(err)}.";
// Or if you are converting function into string by design.
const message2 = "Error is {String(formatErrorMessage)}.";

Additional Info

No response

@chenxinyanc chenxinyanc added enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Sep 5, 2022
@bradzacher
Copy link
Member

Try our rule restrict-template-expressions
https://typescript-eslint.io/rules/restrict-template-expressions

it does exactly this - and more!

playground

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2022
@bradzacher bradzacher added duplicate This issue or pull request already exists wontfix This will not be worked on and removed triage Waiting for maintainers to take a look enhancement: new plugin rule New rule request for eslint-plugin labels Sep 5, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants