Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-useless-template-literals] rename tono-useless-template-expression
(deprecateno-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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
12182f5
11c599b
0bd126f
6420c69
9f56e21
3c4ab99
5431911
e347499
4829797
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -16,10 +16,10 @@ This rule restricts what can be thrown as an exception. | ||
:::warning | ||
This rule is being renamed to [`only-throw-error`](./only-throw-error.mdx). | ||
The current name, `no-throw-literal`, will be removed in a future major version of typescript-eslint. | ||
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. since this PR changes multiple rules, maybe we should also change its title? or maybe split tp multiple PRs? 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. This comes out of#8821 (comment). | ||
When it was first created, this rule only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object. | ||
With the `allowThrowingAny` and `allowThrowingUnknown` options, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`. | ||
::: | ||
{/* Intentionally Omitted: When Not To Use It */} |
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
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 fromthe existing page, though. See on main: typescript-eslint/packages/eslint-plugin/docs/rules/no-useless-template-literals.mdx Lines 55 to 58 inf248e68
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
description: 'Disallow unnecessary templateexpressions.' | ||
--- | ||
import Tabs from '@theme/Tabs'; | ||
@@ -9,53 +9,15 @@ import TabItem from '@theme/TabItem'; | ||
> | ||
> See **https://typescript-eslint.io/rules/no-useless-template-literals** for documentation. | ||
This rule reports template literals thatcontain substitution expressions (also variously referredtoas embedded expressions orstringinterpolations) that are unnecessary and can be simplified. | ||
:::warning | ||
This rule is being renamed to [`no-useless-template-expression`](./no-useless-template-expression.mdx). | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
The current name, `no-useless-template-literals`, will be removed in a future major version of typescript-eslint. | ||
After the creation of this rule, it was realized that the name `no-useless-template-literals` could be misleading, seeing as this rule only targets template literals with substitution expressions. | ||
In particular, it does _not_ aim to flag useless template literals that look like `` `this` `` and could be simplified to `"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. | ||
::: | ||
{/* Intentionally Omitted: When Not To Use It */} |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.