From 7d541529e9ecb1873c6be1cfa8ae06d109d3258a Mon Sep 17 00:00:00 2001 From: yangbo Date: Sun, 18 Sep 2022 15:03:30 +0800 Subject: [PATCH] add api hump check Signed-off-by: yangbo Change-Id: I6d8f37122af610eba810d3245d43c11bab719105 --- .../api_check_plugin/code_style_rule.json | 7 +- build-tools/api_check_plugin/entry.js | 1 + .../api_check_plugin/plugin/dictionaries.txt | 3 + .../api_check_plugin/src/api_check_plugin.js | 8 +- .../api_check_plugin/src/check_decorator.js | 22 +---- .../api_check_plugin/src/check_hump.js | 95 +++++++++++++++++++ .../api_check_plugin/src/check_spelling.js | 34 ++----- .../api_check_plugin/src/compile_info.js | 33 +++++-- build-tools/api_check_plugin/src/utils.js | 7 ++ 9 files changed, 154 insertions(+), 56 deletions(-) create mode 100644 build-tools/api_check_plugin/src/check_hump.js diff --git a/build-tools/api_check_plugin/code_style_rule.json b/build-tools/api_check_plugin/code_style_rule.json index 981c5daeb39..e2650026eba 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 5e11d86b6c2..d8eb1b248f9 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 3c1f9e2c8b3..0456908e3bc 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 ad641b03c69..e5e215041b9 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 fbc2dc44aa6..b19060bd28e 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 00000000000..fd6b16c12b0 --- /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 96a03ca44b4..7658f2858b3 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; -- Gitee