Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
kirkwaiblinger wants to merge1 commit intotypescript-eslint:mainChoose a base branch fromkirkwaiblinger:no-misused-disposable-2
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletionspackages/eslint-plugin/src/rules/no-misused-disposable.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionspackages/eslint-plugin/tests/rules/no-misused-disposable.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.