-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-useless-template-literals] rename to no-useless-template-expression
(deprecate no-useless-template-literals
)
#8821
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
Changes from all commits
12182f5
11c599b
0bd126f
6420c69
9f56e21
3c4ab99
5431911
e347499
4829797
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,87 @@ | ||||||||||
--- | ||||||||||
description: 'Disallow unnecessary template expressions.' | ||||||||||
--- | ||||||||||
|
||||||||||
import Tabs from '@theme/Tabs'; | ||||||||||
import TabItem from '@theme/TabItem'; | ||||||||||
|
||||||||||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||||||||||
> | ||||||||||
> See **https://typescript-eslint.io/rules/no-useless-template-expression** for documentation. | ||||||||||
|
||||||||||
This rule reports template literals that contain substitution expressions (also variously referred to as embedded expressions or string interpolations) that are unnecessary and can be simplified. | ||||||||||
|
||||||||||
:::info[Migration from `no-useless-template-literals`] | ||||||||||
|
||||||||||
This rule was formerly known as [`no-useless-template-literals`](./no-useless-template-literals.mdx). | ||||||||||
We encourage users to migrate to the new name, `no-useless-template-expression`, as the old name will be removed in a future major version of typescript-eslint. | ||||||||||
|
||||||||||
The new name is a drop-in replacement with identical functionality. | ||||||||||
|
||||||||||
::: | ||||||||||
|
||||||||||
## Examples | ||||||||||
|
||||||||||
<Tabs> | ||||||||||
<TabItem value="❌ Incorrect"> | ||||||||||
|
||||||||||
```ts | ||||||||||
// Static values can be incorporated into the surrounding template. | ||||||||||
|
||||||||||
const ab1 = `${'a'}${'b'}`; | ||||||||||
const ab2 = `a${'b'}`; | ||||||||||
|
||||||||||
const stringWithNumber = `${'1 + 1 = '}${2}`; | ||||||||||
|
||||||||||
const stringWithBoolean = `${'true is '}${true}`; | ||||||||||
|
||||||||||
// Some simple expressions that are already strings | ||||||||||
// can be rewritten without a template at all. | ||||||||||
|
||||||||||
const text = 'a'; | ||||||||||
const wrappedText = `${text}`; | ||||||||||
|
||||||||||
declare const intersectionWithString: string & { _brand: 'test-brand' }; | ||||||||||
const wrappedIntersection = `${intersectionWithString}`; | ||||||||||
``` | ||||||||||
|
||||||||||
</TabItem> | ||||||||||
<TabItem value="✅ Correct"> | ||||||||||
|
||||||||||
```ts | ||||||||||
// Static values can be incorporated into the surrounding template. | ||||||||||
|
||||||||||
const ab1 = `ab`; | ||||||||||
const ab2 = `ab`; | ||||||||||
|
||||||||||
const stringWithNumber = `1 + 1 = 2`; | ||||||||||
|
||||||||||
const stringWithBoolean = `true is true`; | ||||||||||
|
||||||||||
// Some simple expressions that are already strings | ||||||||||
// can be rewritten without a template at all. | ||||||||||
|
||||||||||
const text = 'a'; | ||||||||||
const wrappedText = text; | ||||||||||
|
||||||||||
declare const intersectionWithString: string & { _brand: 'test-brand' }; | ||||||||||
const wrappedIntersection = intersectionWithString; | ||||||||||
``` | ||||||||||
|
||||||||||
</TabItem> | ||||||||||
</Tabs> | ||||||||||
|
||||||||||
:::info | ||||||||||
This rule does not aim to flag template literals without substitution expressions that could have been written as an ordinary string. | ||||||||||
That is to say, this rule will not help you turn `` `this` `` into `"this"`. | ||||||||||
If you are looking for such a rule, you can configure the [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes) rule to do this. | ||||||||||
::: | ||||||||||
Comment on lines
+74
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like it! |
||||||||||
|
||||||||||
## When Not To Use It | ||||||||||
|
||||||||||
When you want to allow string expressions inside template literals. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, it's not only for strings, it's also for other literals such as numbers, booleans, null, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, true. That part of the PR is just unchanged code moved from the existing page, though. See on main: typescript-eslint/packages/eslint-plugin/docs/rules/no-useless-template-literals.mdx Lines 55 to 58 in f248e68
I don't feel strongly about it, so I would prefer not to bother changing it in this PR 🙃. Feel free to submit a separate PR to change that line if you like! 🙂 |
||||||||||
|
||||||||||
## Related To | ||||||||||
|
||||||||||
- [`restrict-template-expressions`](./restrict-template-expressions.mdx) | ||||||||||
- [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this PR changes multiple rules, maybe we should also change its title?
(so it'll also be squashed with the proper commit message)
or maybe split tp multiple PRs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comes out of #8821 (comment).
I think it makes sense to include in this change set since it's such a small (and related) change.