• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 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 */
15const path = require('path');
16const {
17  ErrorType,
18  ErrorLevel,
19  FileType,
20  getApiVersion,
21  getCheckApiVersion,
22} = require('./utils');
23const { addAPICheckErrorLogs } = require('./compile_info');
24const nameDictionary = require('./name_dictionary.json');
25const nameScenarioScope = require('./name_scenario_scope.json');
26
27function checkNaming(node, sourcefile, fileName) {
28  const apiVersionToBeVerified = getCheckApiVersion();
29  const apiVersion = Number(getApiVersion(node));
30
31  if (apiVersionToBeVerified.indexOf('^') === 0) {
32    const minCheckApiVersion = Number(apiVersionToBeVerified.substr(1));
33    checkApiVerion(minCheckApiVersion);
34    if (apiVersion > minCheckApiVersion) {
35      checkApiNaming(node, sourcefile, fileName);
36    }
37  } else if (apiVersion === Number(apiVersionToBeVerified)) {
38    checkApiNaming(node, sourcefile, fileName);
39  } else {
40    checkApiVerion(apiVersionToBeVerified);
41  }
42}
43exports.checkNaming = checkNaming;
44
45function checkApiNaming(node, sourcefile, fileName) {
46  const lowIdentifier = node.getText().toLowerCase();
47  checkApiNamingWords(node, sourcefile, fileName, lowIdentifier);
48  checkApiNamingScenario(node, sourcefile, fileName, lowIdentifier);
49}
50
51function checkApiNamingWords(node, sourcefile, fileName, lowIdentifier) {
52  const lowercaseNamingMap = getlowercaseNamingMap();
53  for (const [key, value] of lowercaseNamingMap) {
54    const prohibitedWordIndex = lowIdentifier.indexOf(key);
55    if (prohibitedWordIndex === -1) {
56      continue;
57    }
58    const lowercaseIgnoreWordArr = value.ignore.map(word => word.toLowerCase());
59    const internalWord = node.getText().substr(prohibitedWordIndex, key.length);
60    const errorInfo = `Prohibited word in [${node.getText()}]:{${internalWord}}.The word allowed is [${value.suggestion}]`;
61    if (lowercaseIgnoreWordArr.length === 0) {
62      addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.NAMING_ERRORS, errorInfo, FileType.LOG_API,
63        ErrorLevel.MIDDLE
64      );
65      break;
66    } else {
67      const isIgnoreWord = checkIgnoreWord(lowercaseIgnoreWordArr, lowIdentifier);
68      if (isIgnoreWord === false) {
69        addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.NAMING_ERRORS, errorInfo, FileType.LOG_API,
70          ErrorLevel.MIDDLE
71        );
72      }
73    }
74  }
75}
76
77function checkIgnoreWord(lowercaseIgnoreWordArr, lowIdentifier) {
78  let isIgnoreWord = false;
79  for (let i = 0; i < lowercaseIgnoreWordArr.length; i++) {
80    if (lowercaseIgnoreWordArr[i] && lowIdentifier.indexOf(lowercaseIgnoreWordArr[i]) !== -1) {
81      isIgnoreWord = true;
82      break;
83    }
84  }
85  return isIgnoreWord;
86}
87
88function checkApiVerion(apiVersion) {
89  if (isNaN(parseInt(apiVersion))) {
90    throw 'Please configure the correct API version to be verified';
91  }
92}
93
94function checkApiNamingScenario(node, sourcefile, fileName, lowIdentifier) {
95  const lowercaseNamingScenarioMap = getlowercaseNamingScenarioMap();
96  for (const [key, value] of lowercaseNamingScenarioMap) {
97    const prohibitedWordIndex = lowIdentifier.indexOf(key);
98    if (
99      prohibitedWordIndex !== -1 &&
100      !isInAllowedFiles(value.files, path.basename(fileName))
101    ) {
102      const internalWord = node.getText().substr(prohibitedWordIndex, key.length);
103      const errorInfo = `Prohibited word in [${node.getText()}]:{${internalWord}} in the [${path.basename(fileName)}] file`;
104      addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.NAMING_ERRORS, errorInfo, FileType.LOG_API,
105        ErrorLevel.MIDDLE
106      );
107    }
108  }
109}
110
111function getlowercaseNamingMap() {
112  const lowercaseNamingMap = new Map();
113  for (const item of nameDictionary) {
114    const key = item.badWord.toLowerCase();
115    const { badWord, suggestion, ignore } = item;
116    lowercaseNamingMap.set(key, { badWord, suggestion, ignore });
117  }
118  return lowercaseNamingMap;
119}
120
121function getlowercaseNamingScenarioMap() {
122  const lowercaseNamingScenarioMap = new Map();
123  for (const item of nameScenarioScope) {
124    const key = item.word.toLowerCase();
125    const { word, files } = item;
126    lowercaseNamingScenarioMap.set(key, { word, files });
127  }
128  return lowercaseNamingScenarioMap;
129}
130
131function isInAllowedFiles(files, fileName) {
132  for (const item of files) {
133    const pattern = new RegExp(item);
134    pattern.test(fileName);
135    if (pattern.test(fileName)) {
136      return true;
137    }
138  }
139  return false;
140}
141