-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-useless-template-literals] add new rule #7957
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
Merged
JoshuaKGoldberg
merged 23 commits into
typescript-eslint:main
from
StyleShit:feat/no-useless-template-literals
Dec 12, 2023
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d56e6f2
feat(eslint-plugin): [no-useless-template-literals] add new rule
StyleShit 5affb9e
fix tests
StyleShit 77593ac
thank you josh
StyleShit d924265
hopefully fix tests?
StyleShit 20d9b37
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-us…
StyleShit 5e89515
support template literals with new lines
StyleShit ba23199
support also quotes
StyleShit dd17ad1
fix all files (damn, we need an auto fixer...)
StyleShit 479c10a
Merge branch 'main' into feat/no-useless-template-literals
StyleShit fbdd6f3
wip
StyleShit 0a8e985
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-us…
StyleShit 7f0cc39
report on specific node
StyleShit d22b0f8
fix docs
StyleShit 775594d
fix lint
StyleShit 4da820d
wip
StyleShit 1a545b8
fix lint
StyleShit 0068bd1
Merge branch 'main' into feat/no-useless-template-literals
JoshuaKGoldberg ba1ddbd
revert unrelated changes
StyleShit e24e0f2
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-us…
StyleShit 4c0eda3
Merge branch 'feat/no-useless-template-literals' of https://github.co…
StyleShit 4b2f9e1
more reverts
StyleShit a971081
wip
StyleShit 408e5c7
wip
StyleShit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/eslint-plugin/docs/rules/no-useless-template-literals.md
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,57 @@ | ||
--- | ||
description: 'Disallow unnecessary template literals.' | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-useless-template-literals** for documentation. | ||
|
||
This rule reports template literals that can be simplified to a normal string literal. | ||
|
||
## Examples | ||
|
||
<!--tabs--> | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
const ab1 = `${'a'}${'b'}`; | ||
const ab2 = `a${'b'}`; | ||
|
||
const stringWithNumber = `${'1 + 1 = '}${2}`; | ||
|
||
const stringWithBoolean = `${'true is '}${true}`; | ||
|
||
const text = 'a'; | ||
const wrappedText = `${text}`; | ||
|
||
declare const intersectionWithString: string & { _brand: 'test-brand' }; | ||
const wrappedIntersection = `${intersectionWithString}`; | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
const ab1 = 'ab'; | ||
const ab2 = 'ab'; | ||
|
||
const stringWithNumber = `1 + 1 = 2`; | ||
|
||
const stringWithBoolean = `true is true`; | ||
|
||
const text = 'a'; | ||
const wrappedText = text; | ||
|
||
declare const intersectionWithString: string & { _brand: 'test-brand' }; | ||
const wrappedIntersection = intersectionWithString; | ||
``` | ||
|
||
<!--/tabs--> | ||
|
||
## When Not To Use It | ||
|
||
When you want to allow string expressions inside template literals. | ||
|
||
## Related To | ||
|
||
- [`restrict-template-expressions`](./restrict-template-expressions.md) |
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
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
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
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
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
91 changes: 91 additions & 0 deletions
91
packages/eslint-plugin/src/rules/no-useless-template-literals.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,91 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import * as ts from 'typescript'; | ||
|
||
import { | ||
createRule, | ||
getConstrainedTypeAtLocation, | ||
getParserServices, | ||
isTypeFlagSet, | ||
isUndefinedIdentifier, | ||
} from '../util'; | ||
|
||
type MessageId = 'noUselessTemplateLiteral'; | ||
|
||
export default createRule<[], MessageId>({ | ||
name: 'no-useless-template-literals', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow unnecessary template literals', | ||
recommended: 'strict', | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
noUselessTemplateLiteral: | ||
'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); | ||
} | ||
|
||
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: 'noUselessTemplateLiteral', | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const literalsOrUndefinedExpressions = node.expressions.filter( | ||
(expression): expression is TSESTree.Literal | TSESTree.Identifier => | ||
expression.type === AST_NODE_TYPES.Literal || | ||
isUndefinedIdentifier(expression), | ||
); | ||
|
||
literalsOrUndefinedExpressions.forEach(expression => { | ||
context.report({ | ||
node: expression, | ||
messageId: 'noUselessTemplateLiteral', | ||
}); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.