vscode/.eslintplugin/code-ensure-no-disposables-leak-in-test.ts
Connor Peet 16c469a671
eng: add a lint rule for ensureNoDisposablesAreLeakedInTestSuite (#200089)
Adds a lint rule that ensures ensureNoDisposablesAreLeakedInTestSuite
is called in suites. It grandfathers in existing files that were lacking
the call entirely.

This PR also includes manual fixes to files that used the function
already but were missing it in one or more suites, which the lint rule
detects.
2023-12-05 14:35:27 -08:00

39 lines
1.3 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';
import { Node } from 'estree';
export = new class EnsureNoDisposablesAreLeakedInTestSuite implements eslint.Rule.RuleModule {
readonly meta: eslint.Rule.RuleMetaData = {
type: 'problem',
messages: {
ensure: 'Suites should include a call to `ensureNoDisposablesAreLeakedInTestSuite()` to ensure no disposables are leaked in tests.'
}
};
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
const config = <{ exclude: string[] }>context.options[0];
const needle = context.getFilename().replace(/\\/g, '/');
if (config.exclude.some((e) => needle.endsWith(e))) {
return {};
}
return {
[`Program > ExpressionStatement > CallExpression[callee.name='suite']`]: (node: Node) => {
const src = context.getSourceCode().getText(node)
if (!src.includes('ensureNoDisposablesAreLeakedInTestSuite(')) {
context.report({
node,
messageId: 'ensure',
});
}
},
};
}
};