10000 fix(eslint-plugin): [no-misused-promises] perf: avoid getting types of variables/functions if the annotated type is obviously not a function by bradzacher · Pull Request #9656 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(eslint-plugin): [no-misused-promises] perf: avoid getting types of variables/functions if the annotated type is obviously not a function #9656

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

Merged
merged 3 commits into from
Jul 29, 2024
Merged
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
6 changes: 4 additions & 2 deletions eslint.config.mjs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(merge shenanigan)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah i added this cos I was running from the root with node --cpu-prof --max-old-space-size=10000 ./node_modules/.bin/eslint . and our lint run was matching reporting on files in these two folders.

the / should make eslint ignore the entire folder without globbing etc

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export default tseslint.config(
{
// config with just ignores is the replacement for `.eslintignore`
ignores: [
'.nx/',
'.yarn/',
'**/jest.config.js',
'**/node_modules/**',
'**/dist/**',
Expand All @@ -66,9 +68,9 @@ export default tseslint.config(
// Files copied as part of the build
'packages/types/src/generated/**/*.ts',
// Playground types downloaded from the web
'packages/website/src/vendor',
'packages/website/src/vendor/',
// see the file header in eslint-base.test.js for more info
'packages/rule-tester/tests/eslint-base',
'packages/rule-tester/tests/eslint-base/',
],
},

Expand Down
105 changes: 101 additions & 4 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import * as ts from 'typescript';
import {
createRule,
getParserServices,
isFunction,
isRestParameterDeclaration,
nullThrows,
NullThrowsReasons,
} from '../util';

type Options = [
Expand Down Expand Up @@ -170,9 +173,77 @@ export default createRule<Options, MessageId>({
SpreadElement: checkSpread,
};

function checkTestConditional(node: {
test: TSESTree.Expression | null;
}): void {
/**
* A syntactic check to see if an annotated type is maybe a function type.
* This is a perf optimization to help avoid requesting types where possible
*/
function isPossiblyFunctionType(node: TSESTree.TSTypeAnnotation): boolean {
switch (node.typeAnnotation.type) {
case AST_NODE_TYPES.TSConditionalType:
case AST_NODE_TYPES.TSConstructorType:
case AST_NODE_TYPES.TSFunctionType:
case AST_NODE_TYPES.TSImportType:
case AST_NODE_TYPES.TSIndexedAccessType:
case AST_NODE_TYPES.TSInferType:
case AST_NODE_TYPES.TSIntersectionType:
case AST_NODE_TYPES.TSQualifiedName:
case AST_NODE_TYPES.TSThisType:
case AST_NODE_TYPES.TSTypeOperator:
case AST_NODE_TYPES.TSTypeQuery:
case AST_NODE_TYPES.TSTypeReference:
case AST_NODE_TYPES.TSUnionType:
return true;

case AST_NODE_TYPES.TSTypeLiteral:
return node.typeAnnotation.members.some(
member =>
member.type === AST_NODE_TYPES.TSCallSignatureDeclaration ||
member.type === AST_NODE_TYPES.TSConstructSignatureDeclaration,
);

case AST_NODE_TYPES.TSAbstractKeyword:
case AST_NODE_TYPES.TSAnyKeyword:
case AST_NODE_TYPES.TSArrayType:
case AST_NODE_TYPES.TSAsyncKeyword:
case AST_NODE_TYPES.TSBigIntKeyword:
case AST_NODE_TYPES.TSBooleanKeyword:
case AST_NODE_TYPES.TSDeclareKeyword:
case AST_NODE_TYPES.TSExportKeyword:
case AST_NODE_TYPES.TSIntrinsicKeyword:
case AST_NODE_TYPES.TSLiteralType:
case AST_NODE_TYPES.TSMappedType:
case AST_NODE_TYPES.TSNamedTupleMember:
case AST_NODE_TYPES.TSNeverKeyword:
case AST_NODE_TYPES.TSNullKeyword:
case AST_NODE_TYPES.TSNumberKeyword:
case AST_NODE_TYPES.TSObjectKeyword:
case AST_NODE_TYPES.TSOptionalType:
case AST_NODE_TYPES.TSPrivateKeyword:
case AST_NODE_TYPES.TSProtectedKeyword:
case AST_NODE_TYPES.TSPublicKeyword:
case AST_NODE_TYPES.TSReadonlyKeyword:
case AST_NODE_TYPES.TSRestType:
case AST_NODE_TYPES.TSStaticKeyword:
case AST_NODE_TYPES.TSStringKeyword:
case AST_NODE_TYPES.TSSymbolKeyword:
case AST_NODE_TYPES.TSTemplateLiteralType:
case AST_NODE_TYPES.TSTupleType:
case AST_NODE_TYPES.TSTypePredicate:
case AST_NODE_TYPES.TSUndefinedKeyword:
case AST_NODE_TYPES.TSUnknownKeyword:
case AST_NODE_TYPES.TSVoidKeyword:
return false;
}
}

function checkTestConditional(
node:
| TSESTree.ConditionalExpression
| TSESTree.DoWhileStatement
| TSESTree.ForStatement
| TSESTree.IfStatement
| TSESTree.WhileStatement,
): void {
if (node.test) {
checkConditional(node.test, true);
}
Expand Down Expand Up @@ -255,9 +326,19 @@ export default createRule<Options, MessageId>({

function checkVariableDeclaration(node: TSESTree.VariableDeclarator): void {
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
if (tsNode.initializer === undefined || node.init == null) {
if (
tsNode.initializer === undefined ||
node.init == null ||
node.id.typeAnnotation == null
) {
return;
}

// syntactically ignore some known-good cases to avoid touching type info
if (!isPossiblyFunctionType(node.id.typeAnnotation)) {
return;
}

const varType = services.getTypeAtLocation(node.id);
if (!isVoidReturningFunctionType(checker, tsNode.initializer, varType)) {
return;
Expand Down Expand Up @@ -352,6 +433,22 @@ export default createRule<Options, MessageId>({
if (tsNode.expression === undefined || node.argument == null) {
return;
}

// syntactically ignore some known-good cases to avoid touching type info
const functionNode = (() => {
let current: TSESTree.Node | undefined = node.parent;
while (current && !isFunction(current)) {
current = current.parent;
}
return nullThrows(current, NullThrowsReasons.MissingParent);
})();
if (
functionNode.returnType &&
!isPossiblyFunctionType(functionNode.returnType)
) {
return;
}

const contextualType = checker.getContextualType(tsNode.expression);
if (
contextualType !== undefined &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ foo(bar);
},
options: [{ checksVoidReturn: { attributes: true } }],
},
`
const notAFn1: string = '';
const notAFn2: number = 1;
const notAFn3: boolean = true;
const notAFn4: { prop: 1 } = { prop: 1 };
const notAFn5: {} = {};
const notAFn5: {} = {};
`,
],

invalid: [
Expand Down
Loading
0