• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { ErrorMessage, ErrorTagFormat } from '../../../typedef/checker/result_type';
17import { Comment } from '../../../typedef/parser/Comment';
18import { CommonFunctions, inheritTagArr } from '../../../utils/checkUtils';
19import { ApiInfo, BasicApiInfo, ContainerApiInfo, containerApiTypes } from '../../../typedef/parser/ApiInfoDefination';
20
21export class TagInheritCheck {
22  /**
23   * Tag's inheritance check.
24   * @param { ApiInfo } singleApi
25   * @returns { ErrorTagFormat }
26   */
27  static tagInheritCheck(singleApi: ApiInfo): ErrorTagFormat {
28    const tagNameCheckResult: ErrorTagFormat = {
29      state: true,
30      errorInfo: '',
31    };
32    const apiJsdoc: Comment.JsDocInfo | undefined = singleApi.getLastJsDocInfo();
33    if (apiJsdoc === undefined) {
34      return tagNameCheckResult;
35    }
36    const tagsInfo: Comment.CommentTag[] | undefined = apiJsdoc.tags;
37    const apiTagsName: string[] = [];
38    if (tagsInfo === undefined) {
39      return tagNameCheckResult;
40    }
41    tagsInfo.forEach((tag) => {
42      apiTagsName.push(tag.tag);
43    });
44    let parentApi: ContainerApiInfo = singleApi.getParentApi() as ContainerApiInfo;
45    if (containerApiTypes.has(parentApi.getApiType())) {
46      TagInheritCheck.checkParentJsdoc(parentApi, apiTagsName, tagNameCheckResult);
47    }
48    return tagNameCheckResult;
49  }
50
51  static checkParentJsdoc(basicApiInfo: BasicApiInfo | undefined, apiTagsName: string[],
52    tagNameCheckResult: ErrorTagFormat): boolean {
53    if (basicApiInfo === undefined || !containerApiTypes.has(basicApiInfo.getApiType())) {
54      return true;
55    }
56    const parentApi = basicApiInfo as ContainerApiInfo;
57    const parentApisJsdoc: Comment.CommentTag[] | undefined = parentApi.getLastJsDocInfo()?.tags;
58    if (parentApisJsdoc === undefined) {
59      return true;
60    }
61    let currTag: string = '';
62    const hasInheritTag = parentApisJsdoc.some((parentApiJsdoc: Comment.CommentTag) => {
63      currTag = parentApiJsdoc.tag;
64      return inheritTagArr.includes(parentApiJsdoc.tag) && !apiTagsName.includes(parentApiJsdoc.tag);
65    });
66    if (hasInheritTag) {
67      tagNameCheckResult.state = false;
68      tagNameCheckResult.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ERROR_INFO_INHERIT, [currTag]);
69      return false;
70    }
71    const parentApis: BasicApiInfo | undefined = parentApi.getParentApi();
72    return TagInheritCheck.checkParentJsdoc(parentApis, apiTagsName, tagNameCheckResult);
73  }
74}
75