• 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 { ErrorTagFormat, ErrorMessage } from '../../../typedef/checker/result_type';
17import { ClassInfo } from '../../../typedef/parser/ApiInfoDefination';
18import { Comment } from '../../../typedef/parser/Comment';
19import { CommonFunctions } from '../../../utils/checkUtils';
20import { punctuationMarkSet } from '../../../utils/checkUtils';
21
22export class ForbiddenWordsCheck {
23  /**
24   * forbidden words check-'any','this','unknow'
25   * @param { ClassInfo } singleApi -single api info
26   * @returns { ErrorTagFormat }
27   */
28  static forbiddenWordsCheck(singleApi: ClassInfo): ErrorTagFormat {
29    const forbiddenWordsArr: string[] = ['this', 'unknown'];
30    const forbiddenWordAny: string = 'any';
31    const forbiddenWordsResult: ErrorTagFormat = {
32      state: true,
33      errorInfo: '',
34    };
35    const apiFullText: string = singleApi.getDefinedText();
36    const jsDocInfo: Comment.JsDocInfo[] = singleApi.getJsDocInfos();
37    const publishVersion: string = CommonFunctions.getSinceVersion(jsDocInfo[0].getSince());
38    const apiVersionToBeVerified: string = CommonFunctions.getCheckApiVersion();
39    const reg = /\s{2,}/g;
40    const regx = /(\/\*|\*\/|\*)|\\n|\\r/g;
41    let fullText = apiFullText.replace(regx, ' ');
42    punctuationMarkSet.forEach(punctuationMark => {
43      const punctuationMarkReg = new RegExp(punctuationMark, 'g');
44      if (punctuationMarkReg.test(fullText)) {
45        fullText = fullText.replace(punctuationMarkReg, ' ').replace(reg, ' ');
46      }
47    });
48    let apiTextWordsArr = fullText.split(/\s/g);
49    apiTextWordsArr.forEach((apiTextWord) => {
50
51      if (publishVersion === apiVersionToBeVerified && (forbiddenWordsArr.includes(apiTextWord) || (apiTextWord === forbiddenWordAny && singleApi.getFilePath().indexOf('.d.ets') !== -1))) {
52        forbiddenWordsResult.state = false;
53        forbiddenWordsResult.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ILLEGAL_USE_ANY, [apiTextWord]);
54      }
55    });
56    return forbiddenWordsResult;
57  }
58}
59