• 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 */
15
16const path = require('path');
17const ts = require(path.resolve(__dirname, "../node_modules/typescript"));
18const fs = require("fs");
19const result = require("../check_result.json");
20const rules = require("../code_style_rule.json");
21const { getAPINote, error_type } = require('./utils');
22const { addAPICheckErrorLogs } = require('./compile_info');
23
24
25function checkSyscap(node, sourcefile, fileName) {
26    // check invalid import
27    const program = ts.createProgram({ rootNames: fileName, option: {} });
28    const diagnostic = program.getSuggestionDiagnostics(sourcefile);
29    const invalidImport = '';
30    if (invalidImport !== "") {
31        invalidImport += `,${diagnostic}`;
32    } else {
33        invalidImport += diagnostic;
34    }
35    addAPICheckErrorLogs(node, sourcefile, fileName, error_type.INVALID_IMPORT, invalidImport);
36    // check syscap
37    const syscapTags = rules.syscap.SystemCapability;
38    const syscapRuleSet = new Set();
39    for (const i in syscapTags) {
40        syscapTags[i].forEach(syscap => {
41            const syscapTag = 'SystemCapability.' + i + '.' + syscap;
42            syscapRuleSet.add(syscapTag);
43        });
44    }
45    const apiNote = getAPINote(node);
46    const apiNoteArr = apiNote.split('*');
47    let errorInfo = "";
48    apiNoteArr.forEach(note => {
49        if (note.match(new RegExp('@syscap'))) {
50            const syscapNote = note.replace('@syscap', '').trim();
51            if (!syscapRuleSet.has(syscapNote)) {
52                if (errorInfo !== "") {
53                    errorInfo += `,${syscapNote}`;
54                } else {
55                    errorInfo += syscapNote;
56                }
57                addAPICheckErrorLogs(node, sourcefile, fileName, error_type.UNKNOW_SYSCAP, errorInfo);
58            }
59        }
60    });
61}
62exports.checkSyscap = checkSyscap;