修复了属性扫描中识别不到if语句的子语句的情况

This commit is contained in:
Pan Qiancheng 2024-10-24 17:42:28 +08:00
parent 3c13cf06cd
commit ec114eeaaa
1 changed files with 128 additions and 77 deletions

View File

@ -12,98 +12,149 @@ import { pathConfig } from './paths';
export const getAttrsFromFormData = (
formItem: ts.MethodDeclaration | ts.PropertyAssignment
): DocumentValue[] => {
const attrs: DocumentValue[] = [];
let element: ts.Node = formItem;
const attrList: DocumentValue[] = [];
// 如果是isPropertyAssignment
if (ts.isPropertyAssignment(element)) {
element = element.initializer;
}
element.getChildren().forEach((child) => {
if (ts.isBlock(child)) {
// 拿到block的returnStatement
let returnStatement: ts.ReturnStatement | null = null;
ts.forEachChild(child, (grandChild) => {
if (ts.isReturnStatement(grandChild)) {
// 处理 return 语句
returnStatement = grandChild;
}
});
if (!returnStatement) {
return;
const getFromBlock = (block: ts.Block): DocumentValue[] => {
const attrs: DocumentValue[] = [];
// 拿到block的returnStatement
let returnStatement: ts.ReturnStatement | null = null;
ts.forEachChild(block, (grandChild) => {
if (ts.isReturnStatement(grandChild)) {
// 处理 return 语句
returnStatement = grandChild;
}
ts.forEachChild(returnStatement, (returnChild) => {
if (ts.isObjectLiteralExpression(returnChild)) {
ts.forEachChild(returnChild, (objectChild) => {
if (ts.isShorthandPropertyAssignment(objectChild)) {
attrs.push({
value: objectChild.name.getText(),
pos: {
start: objectChild.name.getStart(),
end: objectChild.name.getEnd(),
},
});
}
if (ts.isPropertyAssignment(objectChild)) {
attrs.push({
value: objectChild.name.getText(),
pos: {
start: objectChild.name.getStart(),
end: objectChild.name.getEnd(),
},
});
}
});
if (!returnStatement) {
return [];
}
ts.forEachChild(returnStatement, (returnChild) => {
if (ts.isObjectLiteralExpression(returnChild)) {
ts.forEachChild(returnChild, (objectChild) => {
if (ts.isShorthandPropertyAssignment(objectChild)) {
attrs.push({
value: objectChild.name.getText(),
pos: {
start: objectChild.name.getStart(),
end: objectChild.name.getEnd(),
},
});
}
if (ts.isPropertyAssignment(objectChild)) {
attrs.push({
value: objectChild.name.getText(),
pos: {
start: objectChild.name.getStart(),
end: objectChild.name.getEnd(),
},
});
}
if (ts.isSpreadAssignment(objectChild)) {
// 这里是展开运算符
if (ts.isSpreadAssignment(objectChild)) {
// 这里是展开运算符
if (ts.isSpreadAssignment(objectChild)) {
// 处理展开运算符
if (
ts.isObjectLiteralExpression(
objectChild.expression
)
) {
// 如果展开的是一个对象字面量表达式
objectChild.expression.properties.forEach(
(prop) => {
if (
ts.isPropertyAssignment(prop) ||
ts.isShorthandPropertyAssignment(
prop
)
) {
attrs.push({
value: prop.name.getText(),
pos: {
start: prop.name.getStart(),
end: prop.name.getEnd(),
},
});
}
// 处理展开运算符
if (
ts.isObjectLiteralExpression(
objectChild.expression
)
) {
// 如果展开的是一个对象字面量表达式
objectChild.expression.properties.forEach(
(prop) => {
if (
ts.isPropertyAssignment(prop) ||
ts.isShorthandPropertyAssignment(
prop
)
) {
attrs.push({
value: prop.name.getText(),
pos: {
start: prop.name.getStart(),
end: prop.name.getEnd(),
},
});
}
);
} else if (
ts.isIdentifier(objectChild.expression)
) {
// 如果展开的是一个标识符,我们可能需要查找它的定义
console.error(
'Spread assignment with identifier:',
objectChild.expression.text
);
} else {
// 处理其他可能的情况
console.error(
'Spread assignment with expression type:',
objectChild.expression.kind
);
}
}
);
} else if (
ts.isIdentifier(objectChild.expression)
) {
// 如果展开的是一个标识符,我们可能需要查找它的定义
console.error(
'Spread assignment with identifier:',
objectChild.expression.text
);
} else {
// 处理其他可能的情况
console.error(
'Spread assignment with expression type:',
objectChild.expression.kind
);
}
}
}
});
}
});
// 这里额外处理block中存在ifif或者switch的情况
ts.forEachChild(block, (node) => {
if (ts.isIfStatement(node)) {
const blocks: ts.Block[] = [];
node.forEachChild((ifChild) => {
if (ts.isBlock(ifChild)) {
blocks.push(ifChild);
}
});
blocks.forEach((b) => {
attrs.push(...getFromBlock(b));
});
} else {
if (ts.isSwitchStatement(node)) {
const blocks: ts.Block[] = [];
node.forEachChild((switchChild) => {
if (ts.isCaseBlock(switchChild)) {
const caseClause: (ts.CaseClause | ts.DefaultClause)[] =
[];
switchChild.forEachChild((caseChild) => {
if (ts.isCaseClause(caseChild)) {
caseClause.push(caseChild);
} else {
if (ts.isDefaultClause(caseChild)) {
caseClause.push(caseChild);
}
}
});
caseClause.forEach((c) => {
c.forEachChild((caseChild) => {
if (ts.isBlock(caseChild)) {
blocks.push(caseChild);
}
});
});
}
});
blocks.forEach((b) => {
attrs.push(...getFromBlock(b));
});
}
});
}
});
return attrs;
};
element.getChildren().forEach((child) => {
if (ts.isBlock(child)) {
attrList.push(...getFromBlock(child));
}
});
return attrs;
return attrList;
};
/**