Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(eslint-plugin): [no-misused-promises] the inheritedMethods and properties 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 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 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Original file line number Diff line number Diff 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 Down Expand 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 Down Expand 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 Down Expand 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 name or known-symbol in `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 deletions packages/eslint-plugin/src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,15 @@ function isParenlessArrowFunction(
);
}

type NodeWithKey =
export type 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