Checker中的context调用不能是解构赋值,context调用需要判断是否为Promise

This commit is contained in:
Pan Qiancheng 2024-11-02 15:10:40 +08:00
parent ac474ae936
commit 8137b1cd4b
1 changed files with 102 additions and 82 deletions

View File

@ -824,67 +824,9 @@ export const checkChecker = (
// 这里判断一下是不是context.xxx的调用 // 这里判断一下是不是context.xxx的调用
const expression = child.expression; const expression = child.expression;
if (ts.isPropertyAccessExpression(expression)) { if (ts.isPropertyAccessExpression(expression)) {
// 如果是context.xxx的调用
// 判断一下context的类型名称是不是BackendRuntimeContext
// const typeChecker = checker.tsInfo.typeChecker;
// const type = typeChecker.getTypeAtLocation(
// expression.expression
// );
// const typeName = typeChecker.typeToString(type);
// if (typeName.includes('BackendRuntimeContext')) {
// // // 无论如何显示一个警告先debug
// // diagnostics.push(
// // createDiagnostic(
// // checker.tsInfo.sourceFile,
// // child.getStart(),
// // child.getEnd(),
// // 'checker.invalidContextCall',
// // 'test warning',
// // vscode.DiagnosticSeverity.Warning
// // )
// // );
// // 检查是不是await的调用否则出现警告
// const parent = child.parent;
// if (
// !ts.isAwaitExpression(parent) &&
// !ts.isReturnStatement(parent)
// ) {
// // 获取方法调用的类型信息
// const signature =
// typeChecker.getResolvedSignature(child);
// if (!signature) {
// return;
// }
// const returnType =
// typeChecker.getReturnTypeOfSignature(
// signature
// );
// const returnTypeString =
// typeChecker.typeToString(returnType);
// if (!isPromiseType(returnType, typeChecker)) {
// // 如果返回值是Promise类型那么就是正确的
// return;
// }
// diagnostics.push(
// createDiagnostic(
// checker.tsInfo.sourceFile,
// child.getStart(),
// child.getEnd(),
// 'checker.invalidContextCall',
// 'context调用应该使用await',
// vscode.DiagnosticSeverity.Warning
// )
// );
// }
// }
if ( if (
expression.expression.getText() === expression.expression.getText() ===
fnMeta.contextIdentifier fnMeta.contextIdentifier
) {
const parent = child.parent;
if (
!ts.isAwaitExpression(parent) &&
!ts.isReturnStatement(parent)
) { ) {
// 现在只检查select operate commitrollback // 现在只检查select operate commitrollback
if ( if (
@ -902,16 +844,23 @@ export const checkChecker = (
) { ) {
return; return;
} }
diagnostics.push( const parent = child.parent;
createDiagnostic( // diagnostics.push(
checker.tsInfo.sourceFile, // createDiagnostic(
child.getStart(), // checker.tsInfo.sourceFile,
child.getEnd(), // child.getStart(),
'checker.invalidContextCall', // child.getEnd(),
'context调用应该使用await', // 'checker.invalidContextCall',
vscode.DiagnosticSeverity.Warning // 'context调用应该使用await',
) // vscode.DiagnosticSeverity.Warning
); // )
// );
if (ts.isReturnStatement(parent)) {
handleReturn(parent, diagnostics);
}
// VariableDeclaration
if (ts.isVariableDeclaration(parent)) {
handleVariable(parent, diagnostics);
} }
} }
} }
@ -931,6 +880,77 @@ export const checkChecker = (
}; };
}; };
// 针对不同的代码块要执行不同的操作下面定义不同操作的函数检查context调用的不同父情况
const handleReturn = (
node: ts.ReturnStatement,
diagnostics: vscode.Diagnostic[]
) => {
// context的父节点是return需要判断在哪里return的这里先不管
};
const handleVariable = (
node: ts.VariableDeclaration,
diagnostics: vscode.Diagnostic[]
) => {
// 首先,不能是{ans} 的解构或者[]的解构
if (ts.isArrayBindingPattern(node.name) || ts.isObjectBindingPattern(node.name)) {
diagnostics.push(
createDiagnostic(
node.getSourceFile(),
node.getStart(),
node.getEnd(),
'checker.invalidDestruct',
'Checker中的context调用不能是解构赋值',
vscode.DiagnosticSeverity.Error
)
);
return;
}
// context的父节点是变量声明需要判断下面有没有identifter instanceof Promise的判断
const identifier = node.name.getText();
// 找到这个作用域的block(VariableDeclaration -> VariableDeclarationList -> VariableStatement -> Block)
const block = node.parent.parent.parent;
// debug一下内容
const walkBlock = (block: ts.Node) => {
let hasPromise = false;
ts.forEachChild(block, (child) => {
if (ts.isIfStatement(child)) {
// 如果是if语句判断条件是否是identifier instanceof Promise
if (ts.isBinaryExpression(child.expression)) {
const left = child.expression.left;
const right = child.expression.right;
if (
ts.isIdentifier(left) &&
ts.isIdentifier(right) &&
left.getText() === identifier &&
right.getText() === 'Promise'
) {
hasPromise = true;
return;
}
}
}
if (ts.isBlock(child)) {
walkBlock(child);
}
});
if (!hasPromise) {
diagnostics.push(
createDiagnostic(
node.getSourceFile(),
node.getStart(),
node.getEnd(),
'checker.invalidPromise',
'context调用需要判断是否为Promise',
vscode.DiagnosticSeverity.Error
)
);
}
};
walkBlock(block);
};
/** /**
* *
* @param sourceFile * @param sourceFile