diff --git a/build-tools/api_check_plugin/code_style_rule.json b/build-tools/api_check_plugin/code_style_rule.json index 981c5daeb396911086e11883b7106c70dfa49ef5..e2650026eba03bb60751763fb20404a4192cf0d0 100644 --- a/build-tools/api_check_plugin/code_style_rule.json +++ b/build-tools/api_check_plugin/code_style_rule.json @@ -99,5 +99,10 @@ "name", "undocumented" ] - } + }, + "namingWhitelist": [ + "Symbol", + "StageModelOnly", + "FAModelOnly" + ] } \ No newline at end of file diff --git a/build-tools/api_check_plugin/entry.js b/build-tools/api_check_plugin/entry.js index 5e11d86b6c218a75a57387d9ef07e159478efe84..d8eb1b248f9f3680278c0c8f76cea6e53f0117c4 100644 --- a/build-tools/api_check_plugin/entry.js +++ b/build-tools/api_check_plugin/entry.js @@ -27,6 +27,7 @@ function checkEntry(url) { removeDir(path.resolve(__dirname, "node_modules")); } catch (error) { // catch error + result = error; } return result; } diff --git a/build-tools/api_check_plugin/plugin/dictionaries.txt b/build-tools/api_check_plugin/plugin/dictionaries.txt index 3c1f9e2c8b3dcfda6e97564b0fcc40f5c6d055fb..0456908e3bc9b5d2bb71c538a3a52a8d9e16bc81 100644 --- a/build-tools/api_check_plugin/plugin/dictionaries.txt +++ b/build-tools/api_check_plugin/plugin/dictionaries.txt @@ -14935,6 +14935,7 @@ damped dampen dampener damper +damping dampness damsel damselfly @@ -30629,6 +30630,7 @@ indubitable indubitableness indubitably induce +induced inducement inducer inducible @@ -51536,6 +51538,7 @@ replaces replacewith replacing replay +replayed replenish replenishment replete diff --git a/build-tools/api_check_plugin/src/api_check_plugin.js b/build-tools/api_check_plugin/src/api_check_plugin.js index ad641b03c69b5f7d5525425737f7e3e322fabdc5..e5e215041b93c92501b6e18ebcc9648e7e7e7905 100644 --- a/build-tools/api_check_plugin/src/api_check_plugin.js +++ b/build-tools/api_check_plugin/src/api_check_plugin.js @@ -16,10 +16,9 @@ const path = require("path"); const fs = require("fs"); const ts = require(path.resolve(__dirname, "../node_modules/typescript")); -// used in local test -// const ts = require("typescript"); const { checkAPIDecorators } = require("./check_decorator"); const { checkSpelling } = require("./check_spelling"); +const { checkAPINameOfHump } = require("./check_hump"); const { hasAPINote } = require("./utils"); let result = require("../check_result.json"); @@ -62,6 +61,9 @@ function checkAPICodeStyleCallback(fileName) { } function checkAllNode(node, sourcefile, fileName) { + if (!ts.isImportDeclaration) { + + } if (hasAPINote(node)) { // check decorator checkAPIDecorators(node, sourcefile, fileName); @@ -71,6 +73,8 @@ function checkAllNode(node, sourcefile, fileName) { if (ts.isIdentifier(node)) { // check variable spelling checkSpelling(node, sourcefile, fileName); + // check hump naming + checkAPINameOfHump(node, sourcefile, fileName); } node.getChildren().forEach((item) => checkAllNode(item, sourcefile, fileName)); } diff --git a/build-tools/api_check_plugin/src/check_decorator.js b/build-tools/api_check_plugin/src/check_decorator.js index fbc2dc44aa6b348237e55b4d2f886db76a642b75..b19060bd28e87ccaf5ab660eb6a67560deca3d77 100644 --- a/build-tools/api_check_plugin/src/check_decorator.js +++ b/build-tools/api_check_plugin/src/check_decorator.js @@ -14,8 +14,8 @@ */ const rules = require("../code_style_rule.json"); -const result = require("../check_result.json"); -const { getAPINote } = require("./utils"); +const { addAPICheckErrorLogs } = require('./compile_info'); +const { getAPINote, error_type } = require("./utils"); // duplicate removal const API_ERROR_DECORATOR_POS = new Set([]); @@ -47,22 +47,8 @@ function checkAPIDecorators(node, sourcefile, fileName) { if (hasCodeStyleError) { API_ERROR_DECORATOR_POS.add(node.pos); - const checkFailFileNameSet = new Set(result.apiFiles); - if (!checkFailFileNameSet.has(fileName)) { - result.apiFiles.push(fileName); - } - const posOfNode = sourcefile.getLineAndCharacterOfPosition(node.pos); - const errorMessage = { - "error_type": "unknow decorator", - "file": fileName, - "pos": node.pos, - "column": posOfNode.character + 1, - "line": posOfNode.line + 1, - "error_info": errorInfo - }; - const scanResultSet = new Set(result.scanResult); - scanResultSet.add(errorMessage); - result.scanResult = [...scanResultSet]; + errorInfo += `.`; + addAPICheckErrorLogs(node, sourcefile, fileName, error_type.UNKNOW_DECORATOR, errorInfo); } } } diff --git a/build-tools/api_check_plugin/src/check_hump.js b/build-tools/api_check_plugin/src/check_hump.js new file mode 100644 index 0000000000000000000000000000000000000000..fd6b16c12b0854d7e69a807f95875508ab9933e8 --- /dev/null +++ b/build-tools/api_check_plugin/src/check_hump.js @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const path = require('path'); +const ts = require(path.resolve(__dirname, "../node_modules/typescript")); +const { error_type } = require('./utils'); +const rules = require("../code_style_rule.json"); +const { addAPICheckErrorLogs } = require('./compile_info'); + +function checkAPINameOfHump(node, sourcefile, fileName) { + let errorInfo = ''; + if (node.parent && !ts.isImportClause(node.parent) && !ts.isImportSpecifier(node.parent) && + (ts.isInterfaceDeclaration(node.parent) || ts.isClassDeclaration(node.parent) || + ts.isEnumDeclaration(node.parent))) { + if (!checkLargeHump(node.escapedText.toString())) { + errorInfo = `This moduleName [${node.escapedText.toString()}] should be used by large hump.`; + } + } else if (!node.parent || (!ts.isImportClause(node.parent) && !ts.isImportSpecifier(node.parent) && + !ts.isTypeReferenceNode(node.parent) && !ts.isQualifiedName(node.parent) && + !ts.isTypeParameterDeclaration(node.parent))) { + if (!checkSmallHump(node.escapedText.toString())) { + errorInfo = `This moduleName [${node.escapedText.toString()}] should be used by small hump.`; + } + } + if (errorInfo !== '') { + addAPICheckErrorLogs(node, sourcefile, fileName, error_type.NAMING_ERRORS, errorInfo); + } +} +exports.checkAPINameOfHump = checkAPINameOfHump; + +function checkAPIFileName(sourcefile, fileName) { + const moduleNames = new Set([]); + const exportAssignments = new Set([]); + sourcefile.statements.forEach((statement, index) => { + if (ts.isModuleDeclaration(statement) && statement.name && ts.isIdentifier(statement.name)) { + moduleNames.add(statement.name.escapedText.toString()); + } + if (ts.isExportAssignment(statement) && statement.expression && ts.isIdentifier(statement.expression)) { + exportAssignments.add(statement.expression.escapedText.toString()); + } + }); + if (exportAssignments.size > 1) { + addAPICheckErrorLogs(sourcefile, sourcefile, fileName, error_type.NAMING_ERRORS, + `This API file can only have one export default statement.`); + } else if (exportAssignments.size === 1) { + const basename = path.basename(fileName); + if (/^@ohos|@system/g.test(basename)) { + for (const exportAssignment of exportAssignments) { + const lastModuleName = basename.split('.').at(-1); + if (moduleNames.has(exportAssignment) && !checkSmallHump(lastModuleName)) { + addAPICheckErrorLogs(sourcefile, sourcefile, fileName, error_type.NAMING_ERRORS, + `This API file should be named by small hump.`); + } else if (!checkLargeHump(lastModuleName)) { + addAPICheckErrorLogs(sourcefile, sourcefile, fileName, error_type.NAMING_ERRORS, + `This API file should be named by large hump.`); + } + } + } else if (!checkSmallHump(basename)) { + addAPICheckErrorLogs(sourcefile, sourcefile, fileName, error_type.NAMING_ERRORS, + `This API file should be named by small hump.`); + } + } +} + +function spliteByHump(word) { + let firstBasicWord = word; + if (/(? { - const posOfNode = sourceFile.getLineAndCharacterOfPosition(diagnosis.pos); - diagnosis.column = posOfNode.character + 1; - diagnosis.line = posOfNode.line + 1; - diagnosis.messageText = ``; - formatDiagnosises.push(diagnosis); - }); -} \ No newline at end of file +function addAPICheckErrorLogs(node, sourcefile, fileName, errorType, errorInfo) { + const checkFailFileNameSet = new Set(result.apiFiles); + if (!checkFailFileNameSet.has(fileName)) { + result.apiFiles.push(fileName); + } + const posOfNode = sourcefile.getLineAndCharacterOfPosition(node.pos); + // const errorMessage = { + // "error_type": "misspell words", + // "file": fileName, + // "pos": node.pos, + // "column": posOfNode.character + 1, + // "line": posOfNode.line + 1, + // "error_info": `Error basic words in [${nodeText}]: ${errorWords}. ` + + // `Do you want to spell it as [${suggest}]?` + // }; + const errorMessage = `API check error of [${errorType}] in ${fileName}(line:${posOfNode.line + 1}, col:` + + `${posOfNode.character + 1}): ${errorInfo}`; + const scanResultSet = new Set(result.scanResult); + scanResultSet.add(errorMessage); + result.scanResult = [...scanResultSet]; +} +exports.addAPICheckErrorLogs = addAPICheckErrorLogs; diff --git a/build-tools/api_check_plugin/src/utils.js b/build-tools/api_check_plugin/src/utils.js index 96a03ca44b4c561c6cf9a868e61b4b84586f02d1..7658f2858b3628ad88885d7a8c539b0f1c851920 100644 --- a/build-tools/api_check_plugin/src/utils.js +++ b/build-tools/api_check_plugin/src/utils.js @@ -65,3 +65,10 @@ function overwriteIndexOf(item, array) { return indexArr; } exports.overwriteIndexOf = overwriteIndexOf; + +const error_type = { + UNKNOW_DECORATOR: 'unknow decorator', + MISSPELL_WORDS: 'misspell words', + NAMING_ERRORS: 'naming errors' +} +exports.error_type = error_type;