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-misused-promises] theinheritedMethods andproperties options to check all statically analyzable declarations#10310

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
ronami wants to merge22 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromronami:no-misused-promises-inherited-methods-static-access-value
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
22 commits
Select commitHold shift + click to select a range
2431803
initial implementation
ronamiNov 8, 2024
35bd930
test asyncIterator symbol
ronamiNov 8, 2024
61f8e0c
support properties
ronamiNov 8, 2024
f4107ff
update tests
ronamiNov 8, 2024
2c4ff28
update additional tests
ronamiNov 8, 2024
714f657
update additional tests
ronamiNov 8, 2024
a31559d
update additional tests
ronamiNov 8, 2024
a12f0a0
update additional tests
ronamiNov 8, 2024
a8d14c1
update additional tests
ronamiNov 8, 2024
07b9daa
simplify test cases, check static properties in isolation
ronamiNov 8, 2024
e00a2b0
expand tests
ronamiNov 8, 2024
125a54a
test edge case of no static key
ronamiNov 9, 2024
fa27a02
use a type assertion over unreachable runtime conditional
ronamiNov 10, 2024
fcbe2ca
merge conflicts
ronamiNov 14, 2024
13127b6
rework tests
ronamiNov 14, 2024
10a8eee
merge main branch
ronamiDec 26, 2024
800a502
adjust tests
ronamiDec 26, 2024
a3d3fb3
don't hard-code solution for well known symbols
ronamiDec 26, 2024
cffb24a
codecov
ronamiDec 26, 2024
528f5c7
remove redundant tests and fix type errors on tests
ronamiDec 26, 2024
75f8b65
refactor
ronamiDec 26, 2024
6c48db9
don't skip empty strings
ronamiDec 30, 2024
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
75 changes: 60 additions & 15 deletionspackages/eslint-plugin/src/rules/no-misused-promises.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,13 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import type { NodeWithKey } from '../util';

import {
createRule,
getFunctionHeadLoc,
getParserServices,
getStaticMemberAccessValue,
isArrayMethodCallWithPredicate,
isFunction,
isRestParameterDeclaration,
Expand DownExpand Up@@ -470,9 +473,6 @@ export default createRule<Options, MessageId>({
});
}
} else if (ts.isMethodDeclaration(tsNode)) {
if (ts.isComputedPropertyName(tsNode.name)) {
return;
}
const obj = tsNode.parent;

// Below condition isn't satisfied unless something goes wrong,
Expand All@@ -492,9 +492,14 @@ export default createRule<Options, MessageId>({
if (objType == null) {
return;
}
const propertySymbol = checker.getPropertyOfType(
const staticAccessValue = getStaticMemberAccessValue(node, context);
if (staticAccessValue == null) {
return;
}
const propertySymbol = getMemberIfExists(
objType,
tsNode.name.text,
staticAccessValue,
checker,
);
if (propertySymbol == null) {
return;
Expand DownExpand Up@@ -594,11 +599,20 @@ export default createRule<Options, MessageId>({
continue;
}

const staticAccessValue = getStaticMemberAccessValue(
node as NodeWithKey,
context,
);

if (staticAccessValue == null) {
continue;
}

for (const heritageType of heritageTypes) {
checkHeritageTypeForMemberReturningVoid(
nodeMember,
heritageType,
memberName,
staticAccessValue,
);
}
}
Expand All@@ -614,9 +628,13 @@ export default createRule<Options, MessageId>({
function checkHeritageTypeForMemberReturningVoid(
nodeMember: ts.Node,
heritageType: ts.Type,
memberName: string,
staticAccessValue: string | symbol,
): void {
const heritageMember = getMemberIfExists(heritageType, memberName);
const heritageMember = getMemberIfExists(
heritageType,
staticAccessValue,
checker,
);
if (heritageMember == null) {
return;
}
Expand DownExpand Up@@ -970,18 +988,45 @@ function getHeritageTypes(
.map(typeExpression => checker.getTypeAtLocation(typeExpression));
}

function getWellKnownStringOfSymbol(symbol: symbol): string | null {
const globalSymbolKeys = Object.getOwnPropertyNames(Symbol);

for (const key of globalSymbolKeys) {
if (symbol === Symbol[key as keyof typeof Symbol]) {
return key;
}
}

return null;
}

/**
* @returns The member with the given name in `type`, if it exists.
* @returns The member with the given nameor known-symbolin `type`, if it exists.
*/
function getMemberIfExists(
type: ts.Type,
memberName: string,
staticAccessValue: string | symbol,
checker: ts.TypeChecker,
): ts.Symbol | undefined {
const escapedMemberName = ts.escapeLeadingUnderscores(memberName);
const symbolMemberMatch = type.getSymbol()?.members?.get(escapedMemberName);
return (
symbolMemberMatch ?? tsutils.getPropertyOfType(type, escapedMemberName)
);
if (typeof staticAccessValue === 'string') {
const escapedMemberName = ts.escapeLeadingUnderscores(staticAccessValue);
const symbolMemberMatch = type.getSymbol()?.members?.get(escapedMemberName);
return (
symbolMemberMatch ?? tsutils.getPropertyOfType(type, escapedMemberName)
);
}

const wellKnownSymbolName = getWellKnownStringOfSymbol(staticAccessValue);

if (wellKnownSymbolName != null) {
return tsutils.getWellKnownSymbolPropertyOfType(
type,
wellKnownSymbolName,
checker,
);
}

return undefined;
}

function isStaticMember(node: TSESTree.Node): boolean {
Expand Down
6 changes: 4 additions & 2 deletionspackages/eslint-plugin/src/util/misc.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -234,13 +234,15 @@ function isParenlessArrowFunction(
);
}

type NodeWithKey =
exporttype NodeWithKey =
| TSESTree.MemberExpression
| TSESTree.MethodDefinition
| TSESTree.Property
| TSESTree.PropertyDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.TSAbstractPropertyDefinition;
| TSESTree.TSAbstractPropertyDefinition
| TSESTree.TSMethodSignature
| TSESTree.TSPropertySignature;

/**
* Gets a member being accessed or declared if its value can be determined statically, and
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp