Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:main
base:main
Choose a base branch
Loading
fromundsoft:10958-string-literals-in-deprecated
Open
Show file tree
Hide file tree
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…
undsoftMar 28, 2025
0d81a5b
Merge branch 'main' into 10958-string-literals-in-deprecated
undsoftMar 31, 2025
82f748f
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoftApr 4, 2025
21ffe65
Merge branch 'main' into 10958-string-literals-in-deprecated
undsoftApr 4, 2025
4c24c44
Merge branch '10958-string-literals-in-deprecated' of github.com:unds…
undsoftApr 4, 2025
74c79d9
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoftApr 4, 2025
cb50efe
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoftApr 20, 2025
b749c56
Merge branch 'master' of github.com:undsoft/typescript-eslint into 10…
undsoftApr 20, 2025
7223496
fix(eslint-plugin): [no-deprecated] adds support for computed member …
undsoftApr 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 85 additions & 24 deletionspackages/eslint-plugin/src/rules/no-deprecated.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 isCallLikeNode {
function isNodeCalleeOfParent(node: TSESTree.Node): node isCalleeNode {
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>({
}
}

functiongetCallLikeNode(node: TSESTree.Node):CallLikeNode | undefined {
functiongetCalleeNode(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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp