• 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 { CommonFunctions } from '../../../utils/checkUtils';
18import { Comment } from '../../../typedef/parser/Comment';
19
20export class ChineseCheck {
21
22    static isChinese(str: string): boolean {
23        return /[\u4e00-\u9fa5]/.test(str);
24    }
25
26    /**
27     * 校验注释是否有中文
28     *
29     * @param {Comment.JsDocInfo} apiJsdoc
30     * @return {ErrorTagFormat}
31     */
32    static checkChinese(apiJsdoc: Comment.JsDocInfo): ErrorTagFormat {
33        const checkResult: ErrorTagFormat = {
34            state: true,
35            errorInfo: '',
36        };
37
38        if (this.isChinese(apiJsdoc.description)) {
39            checkResult.state = false;
40            checkResult.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ERROR_HAS_CHINESE, [
41                apiJsdoc.description
42            ]);
43        }
44
45        const tagsName: Comment.CommentTag[] | undefined = apiJsdoc.tags;
46        if (tagsName === undefined) {
47            return checkResult;
48        }
49        tagsName.forEach((tag) => {
50            for (let i = 0; i < tag.tokenSource.length; i++) {
51                if (this.isChinese(tag.tokenSource[i].source)) {
52                    checkResult.state = false;
53                    checkResult.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ERROR_HAS_CHINESE, [tag.tag]);
54                }
55            }
56        });
57        return checkResult;
58    }
59}
60
61
62