mirror of
https://github.com/Microsoft/vscode
synced 2024-10-12 14:30:13 +00:00
ae91138701
`DisposableStore`/`MutableDisposable` properties should almost always be `readonly`. Otherwise it's easy to accidentally overwrite the property and leak the previous value. This commonly happens with code such as: ```ts class Foo { private disposables = new DisposableStore(); bar() { this.disposables = new DisposableStore(); // leaks old values ... } ``` This change adds an eslint rule to enforce this and adopts `readonly` for the caught cases. I only needed to add 2 suppression comments, which seems like an acceptable tradeoff for helping catch a common mistake
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import * as eslint from 'eslint';
|
|
|
|
/**
|
|
* Checks for potentially unsafe usage of `DisposableStore` / `MutableDisposable`.
|
|
*
|
|
* These have been the source of leaks in the past.
|
|
*/
|
|
export = new class implements eslint.Rule.RuleModule {
|
|
|
|
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
|
|
function checkVariableDeclaration(inNode: any) {
|
|
context.report({
|
|
node: inNode,
|
|
message: `Use const for 'DisposableStore' to avoid leaks by accidental reassignment.`
|
|
});
|
|
}
|
|
|
|
function checkProperty(inNode: any) {
|
|
context.report({
|
|
node: inNode,
|
|
message: `Use readonly for DisposableStore/MutableDisposable to avoid leaks through accidental reassignment.`
|
|
});
|
|
}
|
|
|
|
return {
|
|
'VariableDeclaration[kind!="const"] NewExpression[callee.name="DisposableStore"]': checkVariableDeclaration,
|
|
|
|
'PropertyDefinition[readonly!=true][typeAnnotation.typeAnnotation.typeName.name=/DisposableStore|MutableDisposable/]': checkProperty,
|
|
'PropertyDefinition[readonly!=true] NewExpression[callee.name=/DisposableStore|MutableDisposable/]': checkProperty,
|
|
};
|
|
}
|
|
};
|