Skip to content

feat(eslint-plugin): [no-misused-disposable] add rule (PROTOTYPE) #11087

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
126 changes: 126 additions & 0 deletions packages/eslint-plugin/src/rules/no-misused-disposable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import type { TSESTree } from '@typescript-eslint/utils';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { nullThrows } from '@typescript-eslint/utils/eslint-utils';
import * as tsutils from 'ts-api-utils';

import { createRule, findVariable, getParserServices } from '../util';

export default createRule({
name: 'no-misused-disposable',
meta: {
type: 'suggestion',
docs: {
description: "Disallow using disposables in ways that won't be disposed",
},
messages: {
misusedDisposable:
"Disposable is not handled correctly. Disposable must be in a 'using'/'await using' declaration, or returned",
},
schema: [],
},
defaultOptions: [],
create(context) {
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
return {
CallExpression(node): void {
const type = services.getTypeAtLocation(node);
const disposeSymbol = tsutils.getWellKnownSymbolPropertyOfType(
type,
'dispose',
checker,
);
if (disposeSymbol == null) {
return;
}

if (isValidWayToHandleDisposable(node)) {
return;
}
context.report({
node,
messageId: 'misusedDisposable',
});
},
};

function traverseUpTransparentParents(node: TSESTree.Node): TSESTree.Node {
if (node.parent == null) {
return node;
}
switch (node.parent.type) {
case AST_NODE_TYPES.ConditionalExpression:
return traverseUpTransparentParents(node.parent);
case AST_NODE_TYPES.LogicalExpression:
if (node.parent.operator === '||' || node.parent.operator === '&&') {
return traverseUpTransparentParents(node.parent);
}
return node;
case AST_NODE_TYPES.SequenceExpression:
if (node.parent.expressions.at(-1) === node) {
return traverseUpTransparentParents(node.parent);
}
return node;
default:
return node;
}
}

function isValidWayToHandleDisposable(
disposableNode: TSESTree.Node,
): boolean {
disposableNode = traverseUpTransparentParents(disposableNode);

if (disposableNode.parent == null) {
return false;
}

if (disposableNode.parent.type === AST_NODE_TYPES.VariableDeclarator) {
// using x = makeDisposable();
if (
['using', 'await using'].includes(disposableNode.parent.parent.kind)
) {
return true;
}

// Support code like:
// const foo = makeDisposable();
// return foo;
if (
disposableNode.parent.parent.kind === 'const' &&
disposableNode.parent.id.type === AST_NODE_TYPES.Identifier
) {
const scope = context.sourceCode.getScope(disposableNode);
const smVariable = nullThrows(
findVariable(scope, disposableNode.parent.id),
"couldn't find variable",
);
for (const reference of smVariable.references) {
// TODO: if one of these is a return statement within the same function, it's handled.
const refNode = reference.identifier;
if (refNode === disposableNode) {
continue;
}
const refScope = context.sourceCode.getScope(refNode);
if (
refScope.variableScope === scope.variableScope &&
isValidWayToHandleDisposable(refNode)
) {
return true;
}
}
}

return false;
}

// return makeDisposable();
if (disposableNode.parent.type === AST_NODE_TYPES.ReturnStatement) {
return true;
}

return false;
}
},
});
111 changes: 111 additions & 0 deletions packages/eslint-plugin/tests/rules/no-misused-disposable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import rule from '../../src/rules/no-misused-disposable';
import { getFixturesRootDir } from '../RuleTester';

const rootPath = getFixturesRootDir();

const ruleTester = new RuleTester({
languageOptions: {
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: rootPath,
},
},
});

ruleTester.run('no-misused-disposable', rule, {
valid: [
`
declare function d(): Disposable;
using foo = d();
`,
`
declare function f(): void;
declare function d(): Disposable;
using foo = (f(), d());
`,
`
declare function ad(): AsyncDisposable;
async function f() {
await using foo = ad();
}
`,
`
declare function d(): Disposable;
function makeDisposable() {
return d();
}
`,
`
declare function d(): Disposable;
function makeDisposable() {
const x = d();
return x;
}
`,
// not all paths handle the disposable... should this flag?
`
declare function d(): Disposable;
function makeDisposable() {
const x = d();
if (Math.random() > 0.5) {
return x;
}
}
`,
],
invalid: [
{
code: `
declare function d(): Disposable;
const foo = d();
`,
errors: [
{
column: 13,
endColumn: 16,
endLine: 3,
line: 3,
messageId: 'misusedDisposable',
},
],
},
{
code: `
declare function f(): void;
declare function d(): Disposable;
const foo = (f(), d());
`,
errors: [
{
column: 19,
endColumn: 22,
endLine: 4,
line: 4,
messageId: 'misusedDisposable',
},
],
},
{
code: `
declare function d(): Disposable;
function makeDisposable() {
const x = d();
function inner() {
return x;
}
}
`,
errors: [
{
column: 13,
endColumn: 16,
endLine: 4,
line: 4,
messageId: 'misusedDisposable',
},
],
},
],
});
Loading