• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16import { ArkClass, ArkMethod } from 'arkanalyzer';
17import Logger, { LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger';
18import { BaseChecker, BaseMetaData } from '../BaseChecker';
19import { Rule, Defects, MatcherTypes, MatcherCallback, ClassMatcher } from '../../Index';
20import { IssueReport } from '../../model/Defects';
21
22const logger = Logger.getLogger(LOG_MODULE_TYPE.HOMECHECK, 'NoOptionalMethodCheck');
23const gMetaData: BaseMetaData = {
24    severity: 1,
25    ruleDocPath: '',
26    description: '',
27};
28
29export class NoOptionalMethodCheck implements BaseChecker {
30    readonly metaData: BaseMetaData = gMetaData;
31    public rule: Rule;
32    public defects: Defects[] = [];
33    public issues: IssueReport[] = [];
34
35    private classMetcher: ClassMatcher = {
36        matcherType: MatcherTypes.CLASS,
37    };
38
39    public registerMatchers(): MatcherCallback[] {
40        const classCb: MatcherCallback = {
41            matcher: this.classMetcher,
42            callback: this.check,
43        };
44        return [classCb];
45    }
46
47    public check = (target: ArkClass): void => {
48        target
49            .getMethods()
50            .filter(m => m.getQuestionToken())
51            .forEach(m => {
52                const posInfo = this.getMethodPos(m);
53                this.addIssueReport(posInfo);
54            });
55    };
56
57    private addIssueReport(warnInfo: { line: number; startCol: number; endCol: number; filePath: string }): void {
58        const problem = '';
59        const severity = this.rule.alert ?? this.metaData.severity;
60        let defects = new Defects(
61            warnInfo.line,
62            warnInfo.startCol,
63            warnInfo.endCol,
64            problem,
65            this.metaData.description,
66            severity,
67            this.rule.ruleId,
68            warnInfo.filePath,
69            this.metaData.ruleDocPath,
70            true,
71            false,
72            false
73        );
74        this.issues.push(new IssueReport(defects, undefined));
75    }
76
77    private getMethodPos(cls: ArkMethod): { line: number; startCol: number; endCol: number; filePath: string } {
78        const line = cls.getLineCol() ?? -1;
79        const startCol = cls.getColumn() ?? -1;
80        const endCol = startCol;
81        const arkFile = cls.getDeclaringArkFile();
82        if (arkFile) {
83            return { line, startCol, endCol, filePath: arkFile.getFilePath() };
84        } else {
85            logger.debug('ArkFile is null.');
86            return { line, startCol, endCol, filePath: '' };
87        }
88    }
89}
90