Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [no-useless-template-literals] rename to no-useless-template-expression (ALTERNATE VERSION, copy-pasted)#8919
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
Closed
kirkwaiblinger wants to merge6 commits intotypescript-eslint:mainfromkirkwaiblinger:rename-no-useless-template-literals-copypasta
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
12182f5
Rename no-useless-template-literals to no-useless-template-expression
kirkwaiblinger11c599b
Merge branch 'main' into rename-no-useless-template-literal
kirkwaiblinger0bd126f
Apply suggestions from code review
kirkwaiblinger6420c69
fix syntax
kirkwaiblinger9f56e21
follow up on confusing typed function issue
kirkwaiblinger3c4ab99
copypasta approach
kirkwaiblingerFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-useless-template-expression.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
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. | ||
## 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 [`quotes`](./quotes.mdx) rule to do this. | ||
::: | ||
## When Not To Use It | ||
When you want to allow string expressions inside template literals. | ||
## Related To | ||
- [`restrict-template-expressions`](./restrict-template-expressions.mdx) | ||
- [`quotes`](./quotes.mdx) |
58 changes: 10 additions & 48 deletionspackages/eslint-plugin/docs/rules/no-useless-template-literals.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/configs/all.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/disable-type-checked.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/configs/strict-type-checked-only.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/configs/strict-type-checked.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletionspackages/eslint-plugin/src/rules/no-useless-template-expression.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import * as ts from 'typescript'; | ||
import { | ||
createRule, | ||
getConstrainedTypeAtLocation, | ||
getParserServices, | ||
getStaticStringValue, | ||
isTypeFlagSet, | ||
isUndefinedIdentifier, | ||
} from '../util'; | ||
type MessageId = 'noUselessTemplateExpression'; | ||
export default createRule<[], MessageId>({ | ||
name: 'no-useless-template-expression', | ||
meta: { | ||
fixable: 'code', | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow unnecessary template expressions', | ||
recommended: 'strict', | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
noUselessTemplateExpression: | ||
'Template literal expression is unnecessary and can be simplified.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const services = getParserServices(context); | ||
function isUnderlyingTypeString( | ||
expression: TSESTree.Expression, | ||
): expression is TSESTree.StringLiteral | TSESTree.Identifier { | ||
const type = getConstrainedTypeAtLocation(services, expression); | ||
const isString = (t: ts.Type): boolean => { | ||
return isTypeFlagSet(t, ts.TypeFlags.StringLike); | ||
}; | ||
if (type.isUnion()) { | ||
return type.types.every(isString); | ||
} | ||
if (type.isIntersection()) { | ||
return type.types.some(isString); | ||
} | ||
return isString(type); | ||
} | ||
function isLiteral(expression: TSESTree.Expression): boolean { | ||
return expression.type === AST_NODE_TYPES.Literal; | ||
} | ||
function isTemplateLiteral(expression: TSESTree.Expression): boolean { | ||
return expression.type === AST_NODE_TYPES.TemplateLiteral; | ||
} | ||
function isInfinityIdentifier(expression: TSESTree.Expression): boolean { | ||
return ( | ||
expression.type === AST_NODE_TYPES.Identifier && | ||
expression.name === 'Infinity' | ||
); | ||
} | ||
function isNaNIdentifier(expression: TSESTree.Expression): boolean { | ||
return ( | ||
expression.type === AST_NODE_TYPES.Identifier && | ||
expression.name === 'NaN' | ||
); | ||
} | ||
return { | ||
TemplateLiteral(node: TSESTree.TemplateLiteral): void { | ||
if (node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression) { | ||
return; | ||
} | ||
const hasSingleStringVariable = | ||
node.quasis.length === 2 && | ||
node.quasis[0].value.raw === '' && | ||
node.quasis[1].value.raw === '' && | ||
node.expressions.length === 1 && | ||
isUnderlyingTypeString(node.expressions[0]); | ||
if (hasSingleStringVariable) { | ||
context.report({ | ||
node: node.expressions[0], | ||
messageId: 'noUselessTemplateExpression', | ||
fix(fixer): TSESLint.RuleFix[] { | ||
const [prevQuasi, nextQuasi] = node.quasis; | ||
// Remove the quasis and backticks. | ||
return [ | ||
fixer.removeRange([ | ||
prevQuasi.range[1] - 3, | ||
node.expressions[0].range[0], | ||
]), | ||
fixer.removeRange([ | ||
node.expressions[0].range[1], | ||
nextQuasi.range[0] + 2, | ||
]), | ||
]; | ||
}, | ||
}); | ||
return; | ||
} | ||
const fixableExpressions = node.expressions.filter( | ||
expression => | ||
isLiteral(expression) || | ||
isTemplateLiteral(expression) || | ||
isUndefinedIdentifier(expression) || | ||
isInfinityIdentifier(expression) || | ||
isNaNIdentifier(expression), | ||
); | ||
fixableExpressions.forEach(expression => { | ||
context.report({ | ||
node: expression, | ||
messageId: 'noUselessTemplateExpression', | ||
fix(fixer): TSESLint.RuleFix[] { | ||
const index = node.expressions.indexOf(expression); | ||
const prevQuasi = node.quasis[index]; | ||
const nextQuasi = node.quasis[index + 1]; | ||
// Remove the quasis' parts that are related to the current expression. | ||
const fixes = [ | ||
fixer.removeRange([ | ||
prevQuasi.range[1] - 2, | ||
expression.range[0], | ||
]), | ||
fixer.removeRange([ | ||
expression.range[1], | ||
nextQuasi.range[0] + 1, | ||
]), | ||
]; | ||
const stringValue = getStaticStringValue(expression); | ||
if (stringValue != null) { | ||
const escapedValue = stringValue.replace(/([`$\\])/g, '\\$1'); | ||
fixes.push(fixer.replaceText(expression, escapedValue)); | ||
} else if (isTemplateLiteral(expression)) { | ||
// Note that some template literals get handled in the previous branch too. | ||
// Remove the beginning and trailing backtick characters. | ||
fixes.push( | ||
fixer.removeRange([ | ||
expression.range[0], | ||
expression.range[0] + 1, | ||
]), | ||
fixer.removeRange([ | ||
expression.range[1] - 1, | ||
expression.range[1], | ||
]), | ||
); | ||
} | ||
return fixes; | ||
}, | ||
}); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.