• 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, followTagArr } 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    const apiJsdoc: Comment.JsDocInfo | undefined = singleApi.getLastJsDocInfo();
30    if (apiJsdoc === undefined) {
31      return tagNameCheckResult;
32    }
33    const tagsInfo: Comment.CommentTag[] | undefined = apiJsdoc.tags;
34    const apiTagsName: string[] = [];
35    if (tagsInfo === undefined) {
36      return tagNameCheckResult;
37    }
38    tagsInfo.forEach((tag) => {
39      apiTagsName.push(tag.tag);
40    });
41    let parentApi: ContainerApiInfo = singleApi.getParentApi() as ContainerApiInfo;
42    if (containerApiTypes.has(parentApi.getApiType())) {
43      TagInheritCheck.checkParentJsdoc(parentApi, apiTagsName, tagNameCheckResult);
44    }
45    return tagNameCheckResult;
46  }
47
48  static checkParentJsdoc(basicApiInfo: BasicApiInfo | undefined, apiTagsName: string[],
49    tagNameCheckResult: ErrorTagFormat[]): boolean {
50    if (basicApiInfo === undefined || !containerApiTypes.has(basicApiInfo.getApiType())) {
51      return true;
52    }
53    const parentApi = basicApiInfo as ContainerApiInfo;
54    const parentApisJsdoc: Comment.CommentTag[] | undefined = parentApi.getLastJsDocInfo()?.tags;
55    const trueCheckResult: ErrorTagFormat = {
56      state: true,
57      errorInfo: '',
58    };
59    if (parentApisJsdoc === undefined) {
60      return true;
61    }
62    let currTag: string = '';
63    const hasInheritTag: boolean = parentApisJsdoc.some((parentApiJsdoc: Comment.CommentTag) => {
64      currTag = parentApiJsdoc.tag;
65      return inheritTagArr.includes(parentApiJsdoc.tag) && !apiTagsName.includes(parentApiJsdoc.tag);
66    });
67    const inheritCheckResult: ErrorTagFormat = hasInheritTag ? {
68      state: false,
69      errorInfo: CommonFunctions.createErrorInfo(ErrorMessage.ERROR_INFO_INHERIT, [currTag.toLocaleLowerCase()]),
70    } : trueCheckResult;
71
72    const parentApiTagName: string[] = [];
73    parentApisJsdoc.forEach(parentApiJsdoc => {
74      parentApiTagName.push(parentApiJsdoc.tag);
75    });
76    const hasFollowTag: boolean = apiTagsName.some((apiTagName: string) => {
77      currTag = apiTagName;
78      return followTagArr.includes(apiTagName) && !parentApiTagName.includes(apiTagName);
79    });
80    const followCheckResult: ErrorTagFormat = hasFollowTag ? {
81      state: false,
82      errorInfo: CommonFunctions.createErrorInfo(ErrorMessage.ERROR_INFO_FOLLOW, [currTag]),
83    } : trueCheckResult;
84
85    if (hasInheritTag || hasFollowTag) {
86      tagNameCheckResult.push(...(hasInheritTag ?
87        (hasFollowTag ? [followCheckResult, inheritCheckResult] : [inheritCheckResult]) :
88        [followCheckResult]));
89      return false;
90    }
91    const parentApis: BasicApiInfo | undefined = parentApi.getParentApi();
92    return TagInheritCheck.checkParentJsdoc(parentApis, apiTagsName, tagNameCheckResult);
93  }
94}
95