api check工具增加文件标签顺序及名称校验

Signed-off-by: fanjiaojiao0729 <fanjiaojiao@huawei.com>
This commit is contained in:
fanjiaojiao0729 2023-12-13 10:19:51 +08:00
parent a34fd4120a
commit 447351e84b
4 changed files with 52 additions and 2 deletions

View File

@ -30,7 +30,9 @@
"test",
"useinstead",
"FAModelOnly",
"StageModelOnly"
"StageModelOnly",
"kit",
"file"
],
"jsDoc": [
"abstract",
@ -47,7 +49,6 @@
"event",
"exports",
"external",
"file",
"function",
"generator",
"global",

View File

@ -26,6 +26,7 @@ const { checkJSDoc } = require('./check_legality');
const { checkNaming } = require('./check_naming');
const { checkEventSubscription } = require('./check_event_subscription');
const { checkAnyInAPI } = require('./check_any');
const { checkFileTagOrder } = require('./check_file_tag_order');
const { hasAPINote, ApiCheckResult, requireTypescriptModule, commentNodeWhiteList, splitPath,
isWhiteListFile } = require('./utils');
const ts = requireTypescriptModule();
@ -100,6 +101,10 @@ function checkAllNode(node, sourcefile, fileName) {
checkPermission(node, sourcefile, fileName);
// check event subscription
checkEventSubscription(node, sourcefile, fileName);
// check file tag order
if (ts.isSourceFile(node)) {
checkFileTagOrder(node, sourcefile, fileName);
}
const uesWhiteList = !isWhiteListFile(fileName, whiteLists.JSDocCheck);
if (commentNodeWhiteList.includes(node.kind) && uesWhiteList) {

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { ErrorType, ErrorLevel, FileType, fileTagOrder, ErrorValueInfo } = require('./utils');
const { parseJsDoc } = require('./utils');
const { addAPICheckErrorLogs } = require('./compile_info');
function checkFileTagOrder(node, sourcefile, fileName) {
const comments = parseJsDoc(node);
if (comments.length === 0) {
return;
}
const fileJsdoc = comments[0].tags;
for (let tagIndex = 0; tagIndex < fileJsdoc.length; tagIndex++) {
if (tagIndex + 1 < fileJsdoc.length) {
// 获取前后两个tag下标
const firstIndex = fileTagOrder.indexOf(fileJsdoc[tagIndex].tag);
const secondIndex = fileTagOrder.indexOf(fileJsdoc[tagIndex + 1].tag);
// 非自定义标签在前或数组降序时报错
if (firstIndex > secondIndex && secondIndex > -1 && firstIndex > -1) {
addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.WRONG_SCENE, ErrorValueInfo.ERROR_FILE_TAG_ORDER,
FileType.JSDOC, ErrorLevel.MIDDLE);
break;
}
}
};
}
exports.checkFileTagOrder = checkFileTagOrder;

View File

@ -53,6 +53,9 @@ const tagsArrayOfOrder = [
];
exports.tagsArrayOfOrder = tagsArrayOfOrder;
const fileTagOrder = ['file', 'kit'];
exports.fileTagOrder = fileTagOrder;
function getAPINote(node) {
const apiLength = node.getText().length;
const apiFullLength = node.getFullText().length;
@ -364,6 +367,7 @@ const ErrorValueInfo = {
ERROR_CHANGES_JSDOC_CHANGE: 'Forbid changes: Previous JSDoc cannot be changed.',
ERROR_CHANGES_JSDOC_TRROWS: 'Forbid changes: Throws tag cannot be created.',
ERROR_CHANGES_JSDOC_PERMISSION: 'Forbid changes: Permission tag cannot be created or modified.',
ERROR_FILE_TAG_ORDER: 'File tags order is incorrect.',
};
exports.ErrorValueInfo = ErrorValueInfo;