From d381a9b50464f659b978bcc4d530c593482afb84 Mon Sep 17 00:00:00 2001 From: wangzhiyusss Date: Sat, 30 Aug 2025 14:45:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=B7=BB=E5=8A=A0use=20stati?= =?UTF-8?q?c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangzhiyusss --- build-tools/handleApiFiles.js | 62 ++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/build-tools/handleApiFiles.js b/build-tools/handleApiFiles.js index 1301616b12..2bf4c22798 100755 --- a/build-tools/handleApiFiles.js +++ b/build-tools/handleApiFiles.js @@ -21,6 +21,7 @@ const commander = require('commander'); let dirType = ''; const deleteApiSet = new Set(); const importNameSet = new Set(); +const ARKTS_FLAG = 'use static'; // 处理的目录类型,ets代表处理的是1.1目录,ets2代表处理的是1.2目录里有@arkts 1.1&1.2标签的文件, // noTagInEts2代表处理的是1.2目录里无标签的文件 @@ -169,7 +170,7 @@ function handleApiFileByType(apiRelativePath, rootPath, type, output, isPublic) writeFile(outputPath, deleteArktsTag(fileContent)); return; } else { - writeFile(outputPath.replace(/\.static\.d\.ets$/, '.d.ets'), deleteArktsTag(fileContent)); + writeFile(outputPath.replace(/\.static\.d\.ets$/, '.d.ets'), deleteArktsTag(newContent)); return; } } else { @@ -418,9 +419,7 @@ function handleFileInSecondType(apiRelativePath, fullPath, type, output) { if (sourceFile.statements.length === 0) { // 有1.2标签的文件,删除标记 if (secondRegx.test(sourceFile.getFullText())) { - let newFileContent = deleteUnsportedTag(fileContent); - newFileContent = getFileContent(newFileContent, fullPath); - writeFile(outputPath, deleteArktsTag(newFileContent)); + handlehasTagFile(sourceFile, outputPath); return; } // 处理标有@arkts 1.1&1.2的声明文件 @@ -444,9 +443,7 @@ function handleFileInSecondType(apiRelativePath, fullPath, type, output) { } // 有1.2标签的文件,删除标记 if (secondRegx.test(firstJsdocText)) { - let newFileContent = deleteUnsportedTag(fileContent); - newFileContent = getFileContent(newFileContent, fullPath); - writeFile(outputPath, deleteArktsTag(newFileContent)); + handleLable(outputPath, fileContent) return; } // 处理标有@arkts 1.1&1.2的声明文件 @@ -460,6 +457,17 @@ function handleFileInSecondType(apiRelativePath, fullPath, type, output) { handleNoTagFileInSecondType(sourceFile, outputPath, fullPath); } +/** + * 处理arkts标签,写回文件 + * @param {*} fileContent 文件内容 + */ +function handleLable(outputPath, fileContent) { + let newFileContent = deleteUnsportedTag(fileContent); + newFileContent = getFileContent(newFileContent, fullPath); + newFileContent = addStaticString(newFileContent); + writeFile(outputPath, deleteArktsTag(newFileContent)); +} + /** * 获取文件jsdoc * @param {*} firstNode @@ -493,6 +501,7 @@ function handlehasTagFile(sourceFile, outputPath) { newContent = saveLatestJsDoc(newContent); newContent = getFileContent(newContent, outputPath); newContent = deleteUnsportedTag(newContent); + newContent = addStaticString(newContent); writeFile(outputPath, deleteArktsTag(newContent)); } @@ -525,6 +534,7 @@ function handleNoTagFileInSecondType(sourceFile, outputPath, fullPath) { newContent = getFileContent(newContent, outputPath); newContent = deleteArktsTag(newContent); newContent = deleteUnsportedTag(newContent); + newContent = addStaticString(newContent); writeFile(outputPath, newContent); } @@ -602,8 +612,8 @@ function saveApiByArktsDefinition(sourceFile, fileContent, outputPath) { return !ts.isExpressionStatement(statement); }); let fileJsdoc = firstNode ? getFileJsdoc(firstNode) + '*/\n' : ''; - let newContent = copyrightMessage + fileJsdoc + Array.from(fileContent.matchAll(regx), match => match[2]).join('\n'); - + let newContent = copyrightMessage + fileJsdoc + Array.from(fileContent.matchAll(regx), match => match[4]).join('\n'); + newContent = addStaticString(newContent); writeFile(outputPath, saveLatestJsDoc(newContent)); } @@ -658,7 +668,7 @@ function getDeletionContent(sourceFile) { * @param {*} outputPath * @param {*} fileContent */ -function writeFile(outputPath, fileContent) { +function writeFile(outputPath, fileContent, isStaticFile = false) { const outputDir = path.dirname(outputPath); let newPath = outputPath; if (!fs.existsSync(outputDir)) { @@ -678,14 +688,18 @@ function writeFile(outputPath, fileContent) { * @param {*} copyrightMessage 版权头内容 * @returns */ -function addStaticString(fileContent, copyrightMessage) { - const hasStaticMessage = /use\s+static/g.test(fileContent); - const regex = /\/\*\r?\n\s*\*\s*Copyright[\s\S]*?limitations under the License\.\r?\n\s*\*\//g; - const staticMessage = 'use static'; +function addStaticString(fileContent) { let newContent = fileContent; + //判断是否存在use static且位置在第一个行 + const fileContentRegex = /^\s*(['"])use static\1\s*;?$/gm; + if (fileContentRegex.test(fileContent) && fileContentRegex.exec(fileContent) && + fileContentRegex.exec(fileContent).index > 0) { + newContent = newContent.replace(/^\s*(['"])use static\1\s*;?$/gm, ''); + } + const hasStaticMessage = fileContentRegex.test(newContent); + const staticMessage = 'use static'; if (!hasStaticMessage) { - const newfileJsdoc = `${copyrightMessage}'${staticMessage}'\r\n`; - newContent = newContent.replace(regex, newfileJsdoc); + newContent = `'${staticMessage}'\r\n${newContent}`; } return newContent; } @@ -788,12 +802,28 @@ const transformExportApi = (context) => { }; }; +/** + * 判断是否为use static标记 + * @param { ts.Node } node + * @returns { boolean } + */ +function isStaticFlag(node) { + return ts.isExpressionStatement(node) && + node.expression && + ts.isStringLiteral(node.expression) && + node.expression.text && + node.expression.text === ARKTS_FLAG; +} + function isEmptyFile(node) { let isEmpty = true; if (ts.isSourceFile(node) && node.statements) { const needExportName = new Set(); for (let i = 0; i < node.statements.length; i++) { const statement = node.statements[i]; + if (isStaticFlag(statement)) { + continue; + } if (ts.isExportDeclaration(statement) && statement.moduleSpecifier) { isEmpty = false; break; -- Gitee