Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
109 changes: 85 additions & 24 deletions packages/eslint-plugin/src/rules/no-deprecated.ts
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';

Expand All @@ -10,15 +12,22 @@ import {
createRule,
getParserServices,
nullThrows,
typeOrValueSpecifiersSchema,
typeMatchesSomeSpecifier,
typeOrValueSpecifiersSchema,
} from '../util';

type IdentifierLike =
| TSESTree.Identifier
| TSESTree.JSXIdentifier
| TSESTree.Literal
| TSESTree.PrivateIdentifier
| TSESTree.Super;
| TSESTree.Super
| TSESTree.TemplateLiteral;

type IdentifierInComputedProperty =
| TSESTree.Identifier
| TSESTree.Literal
| TSESTree.TemplateLiteral;

type MessageIds = 'deprecated' | 'deprecatedWithReason';

Expand Down Expand Up @@ -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 {
Expand All @@ -209,13 +231,25 @@ export default createRule<Options, MessageIds>({
return displayParts ? ts.displayPartsToString(displayParts) : '';
}

type CallLikeNode =
| TSESTree.CallExpression
| TSESTree.JSXOpeningElement
| TSESTree.NewExpression
| TSESTree.TaggedTemplateExpression;
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 is CallLikeNode {
function isNodeCalleeOfParent(node: TSESTree.Node): node is CalleeNode {
switch (node.parent?.type) {
case AST_NODE_TYPES.NewExpression:
case AST_NODE_TYPES.CallExpression:
Expand All @@ -232,7 +266,7 @@ export default createRule<Options, MessageIds>({
}
}

function getCallLikeNode(node: TSESTree.Node): CallLikeNode | undefined {
function getCalleeNode(node: TSESTree.Node): CalleeNode | undefined {
let callee = node;

while (
Expand All @@ -245,7 +279,7 @@ export default createRule<Options, MessageIds>({
return isNodeCalleeOfParent(callee) ? callee : undefined;
}

function getCallLikeDeprecation(node: CallLikeNode): string | 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.
Expand Down Expand Up @@ -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 {
const callLikeNode = getCallLikeNode(node);
if (isInComputedProperty(node)) {
return getComputedPropertyDeprecation(
node.parent as TSESTree.MemberExpression,
);
}

const callLikeNode = getCalleeNode(node);
if (callLikeNode) {
return getCallLikeDeprecation(callLikeNode);
}
Expand Down Expand Up @@ -379,7 +450,7 @@ export default createRule<Options, MessageIds>({
return;
}

const name = getReportedNodeName(node);
const name = getReportedNodeName(node, context);

context.report({
...(reason
Expand All @@ -402,20 +473,10 @@ export default createRule<Options, MessageIds>({
checkIdentifier(node);
}
},
'MemberExpression > Literal': checkIdentifier,
'MemberExpression > TemplateLiteral': checkIdentifier,
PrivateIdentifier: checkIdentifier,
Super: checkIdentifier,
};
},
});

function getReportedNodeName(node: IdentifierLike): string {
if (node.type === AST_NODE_TYPES.Super) {
return 'super';
}

if (node.type === AST_NODE_TYPES.PrivateIdentifier) {
return `#${node.name}`;
}

return node.name;
}
Loading
Loading