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-deprecated] support for computed literal member access#11006
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
Open
undsoft wants to merge9 commits intotypescript-eslint:mainChoose a base branch fromundsoft:10958-string-literals-in-deprecated
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
845839a
fix(eslint-plugin): [no-deprecated] adds support for string literal m…
undsoft0d81a5b
Merge branch 'main' into 10958-string-literals-in-deprecated
undsoft82f748f
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoft21ffe65
Merge branch 'main' into 10958-string-literals-in-deprecated
undsoft4c24c44
Merge branch '10958-string-literals-in-deprecated' of github.com:unds…
undsoft74c79d9
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoftcb50efe
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoftb749c56
Merge branch 'master' of github.com:undsoft/typescript-eslint into 10…
undsoft7223496
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoftFile 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
109 changes: 85 additions & 24 deletionspackages/eslint-plugin/src/rules/no-deprecated.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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import { getPropertyName } from '@typescript-eslint/utils/ast-utils'; | ||
import * as tsutils from 'ts-api-utils'; | ||
import * as ts from 'typescript'; | ||
@@ -10,15 +12,22 @@ import { | ||
createRule, | ||
getParserServices, | ||
nullThrows, | ||
typeMatchesSomeSpecifier, | ||
typeOrValueSpecifiersSchema, | ||
} from '../util'; | ||
type IdentifierLike = | ||
| TSESTree.Identifier | ||
| TSESTree.JSXIdentifier | ||
| TSESTree.Literal | ||
| TSESTree.PrivateIdentifier | ||
| TSESTree.Super | ||
| TSESTree.TemplateLiteral; | ||
type IdentifierInComputedProperty = | ||
| TSESTree.Identifier | ||
| TSESTree.Literal | ||
| TSESTree.TemplateLiteral; | ||
type MessageIds = 'deprecated' | 'deprecatedWithReason'; | ||
@@ -188,6 +197,19 @@ export default createRule<Options, MessageIds>({ | ||
} | ||
} | ||
function isInComputedProperty( | ||
node: IdentifierLike, | ||
): node is IdentifierInComputedProperty { | ||
return ( | ||
node.parent.type === AST_NODE_TYPES.MemberExpression && | ||
node.parent.computed && | ||
node === node.parent.property && | ||
(node.type === AST_NODE_TYPES.Literal || | ||
node.type === AST_NODE_TYPES.TemplateLiteral || | ||
node.type === AST_NODE_TYPES.Identifier) | ||
); | ||
} | ||
function getJsDocDeprecation( | ||
symbol: ts.Signature | ts.Symbol | undefined, | ||
): string | undefined { | ||
@@ -209,13 +231,25 @@ export default createRule<Options, MessageIds>({ | ||
return displayParts ? ts.displayPartsToString(displayParts) : ''; | ||
} | ||
function getComputedPropertyDeprecation( | ||
node: TSESTree.MemberExpression, | ||
): string | undefined { | ||
const propertyName = getPropertyName( | ||
node, | ||
context.sourceCode.getScope(node), | ||
); | ||
if (!propertyName) { | ||
return undefined; | ||
} | ||
const objectType = services.getTypeAtLocation(node.object); | ||
const property = objectType.getProperty(propertyName); | ||
return getJsDocDeprecation(property); | ||
} | ||
type CalleeNode = TSESTree.Expression | TSESTree.JSXTagNameExpression; | ||
function isNodeCalleeOfParent(node: TSESTree.Node): node isCalleeNode { | ||
switch (node.parent?.type) { | ||
case AST_NODE_TYPES.NewExpression: | ||
case AST_NODE_TYPES.CallExpression: | ||
@@ -232,7 +266,7 @@ export default createRule<Options, MessageIds>({ | ||
} | ||
} | ||
functiongetCalleeNode(node: TSESTree.Node):CalleeNode | undefined { | ||
let callee = node; | ||
while ( | ||
@@ -245,7 +279,7 @@ export default createRule<Options, MessageIds>({ | ||
return isNodeCalleeOfParent(callee) ? callee : undefined; | ||
} | ||
function getCallLikeDeprecation(node:CalleeNode): string | undefined { | ||
const tsNode = services.esTreeNodeToTSNodeMap.get(node.parent); | ||
// If the node is a direct function call, we look for its signature. | ||
@@ -326,8 +360,45 @@ export default createRule<Options, MessageIds>({ | ||
return getJsDocDeprecation(symbol); | ||
} | ||
function getReportedNodeName( | ||
node: IdentifierLike, | ||
context: Readonly<RuleContext<MessageIds, Options>>, | ||
): string { | ||
if (node.type === AST_NODE_TYPES.Super) { | ||
return 'super'; | ||
} | ||
if (node.type === AST_NODE_TYPES.PrivateIdentifier) { | ||
return `#${node.name}`; | ||
} | ||
if (node.type === AST_NODE_TYPES.Literal) { | ||
return String(node.value); | ||
} | ||
if ( | ||
node.type === AST_NODE_TYPES.TemplateLiteral || | ||
isInComputedProperty(node) | ||
) { | ||
return ( | ||
getPropertyName( | ||
node.parent as TSESTree.MemberExpression, | ||
context.sourceCode.getScope(node), | ||
) || '' | ||
); | ||
} | ||
return node.name; | ||
} | ||
function getDeprecationReason(node: IdentifierLike): string | undefined { | ||
if (isInComputedProperty(node)) { | ||
return getComputedPropertyDeprecation( | ||
node.parent as TSESTree.MemberExpression, | ||
); | ||
} | ||
const callLikeNode = getCalleeNode(node); | ||
if (callLikeNode) { | ||
return getCallLikeDeprecation(callLikeNode); | ||
} | ||
@@ -379,7 +450,7 @@ export default createRule<Options, MessageIds>({ | ||
return; | ||
} | ||
const name = getReportedNodeName(node, context); | ||
context.report({ | ||
...(reason | ||
@@ -402,20 +473,10 @@ export default createRule<Options, MessageIds>({ | ||
checkIdentifier(node); | ||
} | ||
}, | ||
'MemberExpression > Literal': checkIdentifier, | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
'MemberExpression > TemplateLiteral': checkIdentifier, | ||
PrivateIdentifier: checkIdentifier, | ||
Super: checkIdentifier, | ||
}; | ||
}, | ||
}); | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.