!6813 [Bug]: delete_systemapi工具删除节点不全

Merge pull request !6813 from 王曹宇/master
This commit is contained in:
openharmony_ci 2023-09-05 11:30:08 +00:00 committed by Gitee
commit 90ba1c253b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -378,7 +378,7 @@ function deleteSystemApi(url) {
return (node) => {
sourceFile = node;
node = processSourceFile(node); // 处理最外层节点
node = ts.visitEachChild(node, processAllNodes, context); // 遍历所有子节点
node = processVisitEachChild(context, node);
if (!isEmptyFile(node)) {
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const result = printer.printNode(ts.EmitHint.Unspecified, node, sourceFile);
@ -396,11 +396,114 @@ function deleteSystemApi(url) {
}
return node;
};
};
}
exports.deleteSystemApi = deleteSystemApi;
/**
* 遍历每个文件下的所有节点然后删除节点
* @param node
* @returns
*/
/**
* 处理最外层的节点看是否删除
* @param node 解析过后的节点
* @returns
*/
function processSourceFile(node) {
const newStatements = [];
const newStatementsWithoutExport = [];
const deleteSystemApiSet = new Set();
node.statements.forEach((statement) => {
if (isSystemapi(statement)) {
if (ts.isVariableStatement(statement)) {
deleteSystemApiSet.add(variableStatementGetEscapedText(statement));
} else if (
ts.isModuleDeclaration(statement) ||
ts.isInterfaceDeclaration(statement) ||
ts.isClassDeclaration(statement) ||
ts.isEnumDeclaration(statement)
) {
if (statement && statement.name && statement.name.escapedText) {
deleteSystemApiSet.add(statement.name.escapedText.toString());
}
}
} else {
newStatements.push(statement);
}
});
newStatements.forEach((statement) => {
const names = getExportIdentifierName(statement);
if (names.length === 0) {
newStatementsWithoutExport.push(statement);
return;
}
if (names.length === 1 && !deleteSystemApiSet.has(names[0])) {
//exports.name = test;
//export default test1
//export {test1}
newStatementsWithoutExport.push(statement);
return;
}
//export {test1 as test,testa as test2}
if (ts.isExportDeclaration(statement)) {
let needExport = false;
const newSpecifiers = [];
names.forEach((name, index) => {
if (!deleteSystemApiSet.has(name)) {
newSpecifiers.push(statement.exportClause.elements[index]);
needExport = true;
}
});
if (needExport) {
statement.exportClause = ts.factory.updateNamedExports(statement.exportClause, newSpecifiers);
newStatementsWithoutExport.push(statement);
}
}
});
return ts.factory.updateSourceFile(node, newStatementsWithoutExport, node.isDeclarationFile, node.referencedFiles);
}
/**
* 获取export节点的名字只获取第一个关键词
* @param {ts.node} statement
* @returns {Array<string>}
*/
function getExportIdentifierName(statement) {
const names = [];
if (ts.isExpressionStatement(statement)) {
//exports.name = test;
if (ts.isBinaryExpression(statement.expression)) {
names.push(statement.expression.right.escapedText.toString());
}
} else if (ts.isExportAssignment(statement)) {
//export default test1
names.push(statement.expression.escapedText.toString());
} else if (ts.isExportDeclaration(statement)) {
//export {test1} 、export {test1 as test} 、export * from './featureability'
const exportClause = statement.exportClause;
if (exportClause) {
const specifiers = exportClause.elements;
specifiers.forEach((specifier) => {
if (ts.isExportSpecifier(specifier)) {
const name = specifier.propertyName ? specifier.propertyName : specifier.name;
names.push(name.escapedText.toString());
}
});
}
}
return names;
}
/**
* 遍历处理tsnode节点
* @param context 解析过后的内容
* @param node 解析过后的节点
* @returns ts.node
*/
function processVisitEachChild(context, node) {
return ts.visitEachChild(node, processAllNodes, context); // 遍历所有子节点
function processAllNodes(node) {
if (ts.isInterfaceDeclaration(node)) {
const newMembers = [];
@ -452,45 +555,7 @@ function deleteSystemApi(url) {
}
return ts.visitEachChild(node, processAllNodes, context);
}
/**
* 处理最外层的节点看是否删除
* @param node 解析过后的节点
* @returns
*/
function processSourceFile(node) {
const stateNamesSet = new Set([]);
const newStatements = [];
const newStatementsWithoutExport = [];
node.statements.forEach((statement) => {
if (!isSystemapi(statement)) {
newStatements.push(statement);
} else if (ts.isModuleDeclaration(statement) && statement.name && ts.isIdentifier(statement.name)) {
stateNamesSet.add(statement.name.escapedText.toString());
}
});
newStatements.forEach((statement) => {
if (
!(
ts.isExportAssignment(statement) &&
statement.expression &&
ts.isIdentifier(statement.expression) &&
stateNamesSet.has(statement.expression.escapedText.toString())
)
) {
newStatementsWithoutExport.push(statement);
}
});
return ts.factory.updateSourceFile(
node,
newStatementsWithoutExport,
node.isDeclarationFile,
node.referencedFiles
);
}
};
}
exports.deleteSystemApi = deleteSystemApi;
/**
* 解析reference
@ -565,6 +630,18 @@ function resolveCallback(url) {
}
});
}
function collectAllIdentifier(node) {
if (isSystemapi(node)) {
return;
}
if (ts.isIdentifier(node)) {
allReferencesIdentifierSet.add(node.escapedText.toString());
}
return ts.visitEachChild(node, collectAllIdentifier, context);
}
};
}
function variableStatementGetEscapedText(statement) {
let name = '';
if (
@ -579,17 +656,6 @@ function resolveCallback(url) {
}
return name;
}
function collectAllIdentifier(node) {
if (isSystemapi(node)) {
return;
}
if (ts.isIdentifier(node)) {
allReferencesIdentifierSet.add(node.escapedText.toString());
}
return ts.visitEachChild(node, collectAllIdentifier, context);
}
};
}
function isSystemapi(node) {
const notesContent = node.getFullText().replace(node.getText(), '').replace(/[\s]/g, '');
@ -600,7 +666,7 @@ function isSystemapi(node) {
lastNodeName = node.name && node.name.escapedText ? node.name.escapedText.toString() : '';
lastNoteStr = notesStr;
}
return /\@systemapi/g.test(notesStr);
return /@systemapi/g.test(notesStr);
} else {
if (
(ts.isFunctionDeclaration(node) || ts.isMethodSignature(node) || ts.isMethodDeclaration(node)) &&
@ -608,7 +674,7 @@ function isSystemapi(node) {
node.name.escapedText.toString() !== '' &&
node.name.escapedText.toString() === lastNodeName
) {
return /\@systemapi/g.test(lastNoteStr);
return /@systemapi/g.test(lastNoteStr);
} else {
return false;
}
@ -616,15 +682,18 @@ function isSystemapi(node) {
}
function isEmptyFile(node) {
let isEmpty = true;
if (ts.isSourceFile(node) && node.statements) {
for (let i = 0; i < node.statements.length; i++) {
const statement = node.statements[i];
if (!ts.isImportDeclaration(statement)) {
return false;
if (ts.isImportDeclaration(statement)) {
continue;
}
isEmpty = false;
break;
}
}
}
return true;
return isEmpty;
}
const apiSourcePath = '../api';