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 */ 15 16const path = require("path"); 17const fs = require("fs"); 18const ts = require(path.resolve(__dirname, "../node_modules/typescript")); 19const { checkAPIDecorators } = require("./check_decorator"); 20const { checkSpelling } = require("./check_spelling"); 21const { checkAPINameOfHump } = require("./check_hump"); 22const { checkPermission } = require("./check_permission"); 23const { checkSyscap } = require('./check_syscap'); 24const { checkDeprecated } = require('./check_deprecated'); 25const { hasAPINote } = require("./utils"); 26let result = require("../check_result.json"); 27 28function checkAPICodeStyle(url) { 29 if (fs.existsSync(url)) { 30 const mdApiFiles = getMdFiles(url); 31 tsTransform(mdApiFiles, checkAPICodeStyleCallback); 32 } 33} 34 35function getMdFiles(url) { 36 const content = fs.readFileSync(url, "utf-8"); 37 const mdFiles = content.split("\r\n"); 38 return mdFiles; 39} 40 41function tsTransform(uFiles, callback) { 42 uFiles.forEach(filePath => { 43 if (/\.d\.ts/.test(filePath)) { 44 const content = fs.readFileSync(filePath, "utf-8"); 45 const fileName = path.basename(filePath).replace(/.d.ts/g, ".ts"); 46 ts.transpileModule(content, { 47 compilerOptions: { 48 "target": ts.ScriptTarget.ES2017 49 }, 50 fileName: fileName, 51 transformers: { before: [callback(filePath)] } 52 }) 53 } 54 }) 55} 56 57function checkAPICodeStyleCallback(fileName) { 58 return (context) => { 59 return (node) => { 60 checkAllNode(node, node, fileName); 61 return node; 62 } 63 } 64} 65 66function checkAllNode(node, sourcefile, fileName) { 67 if (!ts.isImportDeclaration) { 68 69 } 70 if (hasAPINote(node)) { 71 // check decorator 72 checkAPIDecorators(node, sourcefile, fileName); 73 // check apiNote spelling 74 checkSpelling(node, sourcefile, fileName); 75 // check syscap 76 // checkSyscap(node, sourcefile, fileName); 77 // check deprecated 78 checkDeprecated(node, sourcefile, fileName); 79 // check permission 80 checkPermission(node, sourcefile, fileName); 81 } 82 if (ts.isIdentifier(node)) { 83 // check variable spelling 84 checkSpelling(node, sourcefile, fileName); 85 // check hump naming 86 checkAPINameOfHump(node, sourcefile, fileName); 87 } 88 node.getChildren().forEach((item) => checkAllNode(item, sourcefile, fileName)); 89} 90 91function scanEntry(url) { 92 // scan entry 93 checkAPICodeStyle(url); 94 return result.scanResult; 95} 96exports.scanEntry = scanEntry; 97