• 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 */
15import { ErrorMessage, ErrorTagFormat } from '../../../typedef/checker/result_type';
16import { Comment } from '../../../typedef/parser/Comment';
17import { tagsArrayOfOrder, CommonFunctions } from '../../../utils/checkUtils';
18
19export class OrderCheck {
20  /**
21   * Check if the tags order is correct.
22   * @param { Comment.JsDocInfo } apiJsdoc -api jsdoc all infomation
23   * @returns { boolean }
24   */
25  static orderCheck(apiJsdoc: Comment.JsDocInfo): ErrorTagFormat {
26    const orderCheckResult: ErrorTagFormat = {
27      state: true,
28      errorInfo: '',
29    };
30    const tagsOrder: Comment.CommentTag[] | undefined = apiJsdoc.tags;
31    if (tagsOrder === undefined) {
32      return orderCheckResult;
33    }
34    for (let tagIndex = 0; tagIndex < tagsOrder.length; tagIndex++) {
35      if (tagIndex + 1 < tagsOrder.length) {
36        // 获取前后两个tag下标
37        const firstIndex = tagsArrayOfOrder.indexOf(tagsOrder[tagIndex].tag);
38        const secondIndex = tagsArrayOfOrder.indexOf(tagsOrder[tagIndex + 1].tag);
39        // 判断标签是否为官方标签
40        const firstTag = CommonFunctions.isOfficialTag(tagsOrder[tagIndex].tag);
41        // 非自定义标签在前或数组降序时报错
42        if ((firstTag && secondIndex > -1) || (firstIndex > secondIndex && secondIndex > -1)) {
43          orderCheckResult.state = false;
44          orderCheckResult.errorInfo = ErrorMessage.ERROR_ORDER;
45          break;
46        }
47      }
48    }
49    return orderCheckResult;
50  }
51}
52