161847f8eSopenharmony_ci/* 261847f8eSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 461847f8eSopenharmony_ci * you may not use this file except in compliance with the License. 561847f8eSopenharmony_ci * You may obtain a copy of the License at 661847f8eSopenharmony_ci * 761847f8eSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 861847f8eSopenharmony_ci * 961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and 1361847f8eSopenharmony_ci * limitations under the License. 1461847f8eSopenharmony_ci */ 1561847f8eSopenharmony_ciconst path = require('path'); 1661847f8eSopenharmony_ciconst { 1761847f8eSopenharmony_ci ErrorType, 1861847f8eSopenharmony_ci ErrorLevel, 1961847f8eSopenharmony_ci FileType, 2061847f8eSopenharmony_ci getApiVersion, 2161847f8eSopenharmony_ci getCheckApiVersion, 2261847f8eSopenharmony_ci requireTypescriptModule 2361847f8eSopenharmony_ci} = require('./utils'); 2461847f8eSopenharmony_ciconst { addAPICheckErrorLogs, getParentkind } = require('./compile_info'); 2561847f8eSopenharmony_ciconst ts = requireTypescriptModule(); 2661847f8eSopenharmony_ciconst nameDictionary = require('./name_dictionary.json'); 2761847f8eSopenharmony_ciconst nameScenarioScope = require('./name_scenario_scope.json'); 2861847f8eSopenharmony_ci 2961847f8eSopenharmony_cifunction checkNaming(node, sourcefile, fileName) { 3061847f8eSopenharmony_ci const apiVersionToBeVerified = getCheckApiVersion(); 3161847f8eSopenharmony_ci const apiVersion = Number(getApiVersion(node)); 3261847f8eSopenharmony_ci 3361847f8eSopenharmony_ci if (apiVersionToBeVerified.indexOf('^') === 0) { 3461847f8eSopenharmony_ci const minCheckApiVersion = Number(apiVersionToBeVerified.substr(1)); 3561847f8eSopenharmony_ci checkApiVerion(minCheckApiVersion); 3661847f8eSopenharmony_ci if (apiVersion > minCheckApiVersion) { 3761847f8eSopenharmony_ci checkApiNaming(node, sourcefile, fileName); 3861847f8eSopenharmony_ci } 3961847f8eSopenharmony_ci } else if (apiVersion === Number(apiVersionToBeVerified)) { 4061847f8eSopenharmony_ci checkApiNaming(node, sourcefile, fileName); 4161847f8eSopenharmony_ci } else { 4261847f8eSopenharmony_ci checkApiVerion(apiVersionToBeVerified); 4361847f8eSopenharmony_ci } 4461847f8eSopenharmony_ci} 4561847f8eSopenharmony_ciexports.checkNaming = checkNaming; 4661847f8eSopenharmony_ci 4761847f8eSopenharmony_cifunction checkApiNaming(node, sourcefile, fileName) { 4861847f8eSopenharmony_ci const lowIdentifier = node.getText().toLowerCase(); 4961847f8eSopenharmony_ci const apiParentKind = []; 5061847f8eSopenharmony_ci getParentkind(node, apiParentKind); 5161847f8eSopenharmony_ci if (node.parent.kind === ts.SyntaxKind.TypeReference || apiParentKind.includes('JSDoc')) { 5261847f8eSopenharmony_ci return; 5361847f8eSopenharmony_ci } 5461847f8eSopenharmony_ci checkApiNamingWords(node, sourcefile, fileName, lowIdentifier); 5561847f8eSopenharmony_ci checkApiNamingScenario(node, sourcefile, fileName, lowIdentifier); 5661847f8eSopenharmony_ci} 5761847f8eSopenharmony_ci 5861847f8eSopenharmony_cifunction checkApiNamingWords(node, sourcefile, fileName, lowIdentifier) { 5961847f8eSopenharmony_ci const lowercaseNamingMap = getlowercaseNamingMap(); 6061847f8eSopenharmony_ci for (const [key, value] of lowercaseNamingMap) { 6161847f8eSopenharmony_ci const prohibitedWordIndex = lowIdentifier.indexOf(key); 6261847f8eSopenharmony_ci if (prohibitedWordIndex === -1) { 6361847f8eSopenharmony_ci continue; 6461847f8eSopenharmony_ci } 6561847f8eSopenharmony_ci const lowercaseIgnoreWordArr = value.ignore.map(word => word.toLowerCase()); 6661847f8eSopenharmony_ci const internalWord = node.getText().substr(prohibitedWordIndex, key.length); 6761847f8eSopenharmony_ci const errorInfo = `Prohibited word in [${node.getText()}]:{${internalWord}}.The word allowed is [${value.suggestion}]`; 6861847f8eSopenharmony_ci if (lowercaseIgnoreWordArr.length === 0) { 6961847f8eSopenharmony_ci addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.NAMING_ERRORS, errorInfo, FileType.LOG_API, 7061847f8eSopenharmony_ci ErrorLevel.MIDDLE 7161847f8eSopenharmony_ci ); 7261847f8eSopenharmony_ci break; 7361847f8eSopenharmony_ci } else { 7461847f8eSopenharmony_ci const isIgnoreWord = checkIgnoreWord(lowercaseIgnoreWordArr, lowIdentifier, value.badWord); 7561847f8eSopenharmony_ci if (isIgnoreWord === false) { 7661847f8eSopenharmony_ci addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.NAMING_ERRORS, errorInfo, FileType.LOG_API, 7761847f8eSopenharmony_ci ErrorLevel.MIDDLE 7861847f8eSopenharmony_ci ); 7961847f8eSopenharmony_ci } 8061847f8eSopenharmony_ci } 8161847f8eSopenharmony_ci } 8261847f8eSopenharmony_ci} 8361847f8eSopenharmony_ci 8461847f8eSopenharmony_cifunction checkIgnoreWord(lowercaseIgnoreWordArr, lowIdentifier, badWord) { 8561847f8eSopenharmony_ci let isIgnoreWord = false; 8661847f8eSopenharmony_ci const isNamingFoot = lowIdentifier.substring(lowIdentifier.length - badWord.length, lowIdentifier.length) === badWord; 8761847f8eSopenharmony_ci for (let i = 0; i < lowercaseIgnoreWordArr.length; i++) { 8861847f8eSopenharmony_ci if (lowercaseIgnoreWordArr[i] && lowIdentifier.indexOf(lowercaseIgnoreWordArr[i]) !== -1) { 8961847f8eSopenharmony_ci isIgnoreWord = true; 9061847f8eSopenharmony_ci break; 9161847f8eSopenharmony_ci } 9261847f8eSopenharmony_ci } 9361847f8eSopenharmony_ci if (!isNamingFoot) { 9461847f8eSopenharmony_ci isIgnoreWord = true; 9561847f8eSopenharmony_ci } 9661847f8eSopenharmony_ci return isIgnoreWord; 9761847f8eSopenharmony_ci} 9861847f8eSopenharmony_ci 9961847f8eSopenharmony_cifunction checkApiVerion(apiVersion) { 10061847f8eSopenharmony_ci if (isNaN(parseInt(apiVersion))) { 10161847f8eSopenharmony_ci throw 'Please configure the correct API version to be verified'; 10261847f8eSopenharmony_ci } 10361847f8eSopenharmony_ci} 10461847f8eSopenharmony_ci 10561847f8eSopenharmony_cifunction checkApiNamingScenario(node, sourcefile, fileName, lowIdentifier) { 10661847f8eSopenharmony_ci const lowercaseNamingScenarioMap = getlowercaseNamingScenarioMap(); 10761847f8eSopenharmony_ci for (const [key, value] of lowercaseNamingScenarioMap) { 10861847f8eSopenharmony_ci const prohibitedWordIndex = lowIdentifier.indexOf(key); 10961847f8eSopenharmony_ci if ( 11061847f8eSopenharmony_ci prohibitedWordIndex !== -1 && 11161847f8eSopenharmony_ci !isInAllowedFiles(value.files, path.basename(fileName)) 11261847f8eSopenharmony_ci ) { 11361847f8eSopenharmony_ci const internalWord = node.getText().substr(prohibitedWordIndex, key.length); 11461847f8eSopenharmony_ci const errorInfo = `Prohibited word in [${node.getText()}]:{${internalWord}} in the [${path.basename(fileName)}] file`; 11561847f8eSopenharmony_ci addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.NAMING_ERRORS, errorInfo, FileType.LOG_API, 11661847f8eSopenharmony_ci ErrorLevel.MIDDLE 11761847f8eSopenharmony_ci ); 11861847f8eSopenharmony_ci } 11961847f8eSopenharmony_ci } 12061847f8eSopenharmony_ci} 12161847f8eSopenharmony_ci 12261847f8eSopenharmony_cifunction getlowercaseNamingMap() { 12361847f8eSopenharmony_ci const lowercaseNamingMap = new Map(); 12461847f8eSopenharmony_ci for (const item of nameDictionary) { 12561847f8eSopenharmony_ci const key = item.badWord.toLowerCase(); 12661847f8eSopenharmony_ci const { badWord, suggestion, ignore } = item; 12761847f8eSopenharmony_ci lowercaseNamingMap.set(key, { badWord, suggestion, ignore }); 12861847f8eSopenharmony_ci } 12961847f8eSopenharmony_ci return lowercaseNamingMap; 13061847f8eSopenharmony_ci} 13161847f8eSopenharmony_ci 13261847f8eSopenharmony_cifunction getlowercaseNamingScenarioMap() { 13361847f8eSopenharmony_ci const lowercaseNamingScenarioMap = new Map(); 13461847f8eSopenharmony_ci for (const item of nameScenarioScope) { 13561847f8eSopenharmony_ci const key = item.word.toLowerCase(); 13661847f8eSopenharmony_ci const { word, files } = item; 13761847f8eSopenharmony_ci lowercaseNamingScenarioMap.set(key, { word, files }); 13861847f8eSopenharmony_ci } 13961847f8eSopenharmony_ci return lowercaseNamingScenarioMap; 14061847f8eSopenharmony_ci} 14161847f8eSopenharmony_ci 14261847f8eSopenharmony_cifunction isInAllowedFiles(files, fileName) { 14361847f8eSopenharmony_ci for (const item of files) { 14461847f8eSopenharmony_ci const pattern = new RegExp(item); 14561847f8eSopenharmony_ci pattern.test(fileName); 14661847f8eSopenharmony_ci if (pattern.test(fileName)) { 14761847f8eSopenharmony_ci return true; 14861847f8eSopenharmony_ci } 14961847f8eSopenharmony_ci } 15061847f8eSopenharmony_ci return false; 15161847f8eSopenharmony_ci} 152