mirror of
https://github.com/Microsoft/vscode
synced 2024-09-29 16:01:05 +00:00
eng: add a basic 'must use result' and ensure assertSnapshot result is used (#194744)
* eng: add a basic 'must use result' and ensure assertSnapshot result is used Fixes #194570 * fix found lint
This commit is contained in:
parent
9106c6421b
commit
331e445eff
35
.eslintplugin/code-must-use-result.ts
Normal file
35
.eslintplugin/code-must-use-result.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 { TSESTree } from '@typescript-eslint/experimental-utils';
|
||||
|
||||
const VALID_USES = new Set<TSESTree.AST_NODE_TYPES | undefined>([
|
||||
TSESTree.AST_NODE_TYPES.AwaitExpression,
|
||||
TSESTree.AST_NODE_TYPES.VariableDeclarator,
|
||||
]);
|
||||
|
||||
export = new class MustUseResults implements eslint.Rule.RuleModule {
|
||||
|
||||
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
|
||||
|
||||
const config = <{ message: string, functions: string[] }[]>context.options[0];
|
||||
const listener: eslint.Rule.RuleListener = {};
|
||||
|
||||
for (const { message, functions } of config) {
|
||||
for (const fn of functions) {
|
||||
const query = `CallExpression[callee.property.name='${fn}'], CallExpression[callee.name='${fn}']`
|
||||
listener[query] = (node: any) => {
|
||||
const cast: TSESTree.CallExpression = node;
|
||||
if (!VALID_USES.has(cast.parent?.type)) {
|
||||
context.report({ node, message });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return listener;
|
||||
}
|
||||
};
|
|
@ -140,7 +140,18 @@
|
|||
"rules": {
|
||||
"local/code-no-test-only": "error",
|
||||
"local/code-no-test-async-suite": "warn",
|
||||
"local/code-no-unexternalized-strings": "off"
|
||||
"local/code-no-unexternalized-strings": "off",
|
||||
"local/code-must-use-result": [
|
||||
"warn",
|
||||
[
|
||||
{
|
||||
"message": "Expression must be awaited",
|
||||
"functions": [
|
||||
"assertSnapshot"
|
||||
]
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -257,7 +257,7 @@ suite('Chat', () => {
|
|||
const model = testDisposables.add(testService.startSession(providerId, CancellationToken.None));
|
||||
assert.strictEqual(model.getRequests().length, 0);
|
||||
|
||||
assertSnapshot(model.toExport());
|
||||
await assertSnapshot(model.toExport());
|
||||
|
||||
const response = await testService.sendRequest(model.sessionId, 'test request');
|
||||
assert(response);
|
||||
|
@ -266,7 +266,7 @@ suite('Chat', () => {
|
|||
|
||||
assert.strictEqual(model.getRequests().length, 1);
|
||||
|
||||
assertSnapshot(model.toExport());
|
||||
await assertSnapshot(model.toExport());
|
||||
});
|
||||
|
||||
test('can deserialize', async () => {
|
||||
|
@ -306,6 +306,6 @@ suite('Chat', () => {
|
|||
dispose: () => testService2.clearSession(serializedChatData.sessionId)
|
||||
});
|
||||
|
||||
assertSnapshot(chatModel2.toExport());
|
||||
await assertSnapshot(chatModel2.toExport());
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue