From aef773372f302d18786cf8971240d5ad6f2586a2 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Fri, 15 Aug 2025 18:35:39 +0800 Subject: [PATCH] Fix for MethodInheritRule Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICS9Q8 Test scenarios: Fix Bug Signed-off-by: zhong_ning --- ets2panda/linter/src/lib/LinterRunner.ts | 2 + ets2panda/linter/src/lib/TypeScriptLinter.ts | 12 +++++ .../linter/src/lib/ts-compiler/ResolveSdks.ts | 4 ++ .../src/lib/utils/functions/CommonApiInfo.ts | 47 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100755 ets2panda/linter/src/lib/utils/functions/CommonApiInfo.ts diff --git a/ets2panda/linter/src/lib/LinterRunner.ts b/ets2panda/linter/src/lib/LinterRunner.ts index 3696ab6898..cab8af9115 100644 --- a/ets2panda/linter/src/lib/LinterRunner.ts +++ b/ets2panda/linter/src/lib/LinterRunner.ts @@ -54,6 +54,7 @@ import { mergeArrayMaps } from './utils/functions/MergeArrayMaps'; import { clearPathHelperCache, pathContainsDirectory } from './utils/functions/PathHelper'; import { processSyncErr } from './utils/functions/ProcessWrite'; import type { LinterInputInfo } from './LinterInputInfo'; +import { collectCommonApiInfo } from './utils/functions/CommonApiInfo'; function prepareInputFilesList(cmdOptions: CommandLineOptions): string[] { let inputFiles = cmdOptions.inputFiles.map((x) => { @@ -120,6 +121,7 @@ function lintImpl(config: LinterConfig, migrationInfo?: MigrationInfo): LintRunR if (srcFile) { srcFiles.push(srcFile); } + collectCommonApiInfo(tsProgram); } const tscStrictDiagnostics = getTscDiagnostics(tscCompiledProgram, srcFiles); diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 672a6aec7f..637405994a 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -224,6 +224,7 @@ import { ExtendedIdentifierType } from './utils/consts/Types'; import { COMPONENT_DECORATOR, SELECT_IDENTIFIER, SELECT_OPTIONS, STRING_ERROR_LITERAL } from './utils/consts/Literals'; import { ES_OBJECT } from './utils/consts/ESObject'; import { cookBookMsg } from './CookBookMsg'; +import { getCommonApiInfoMap } from './utils/functions/CommonApiInfo'; export class TypeScriptLinter extends BaseTypeScriptLinter { supportedStdCallApiChecker: SupportedStdCallApiChecker; @@ -3993,6 +3994,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const classDecl = node.parent; if (!ts.isClassDeclaration(classDecl)) { + this.handleMethodInheritForCommonApi(node); return; } const isStatic = @@ -4012,6 +4014,16 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.checkIncompatibleFunctionTypes(node); } + private handleMethodInheritForCommonApi(node: ts.MethodDeclaration): void { + const commonApiInfos = getCommonApiInfoMap(); + commonApiInfos?.forEach((apiNode) => { + if (node.name.getText() === apiNode.name.getText()) { + this.checkMethodParameters(node, apiNode); + this.checkMethodReturnType(node, apiNode); + } + }); + } + private checkMethodType( allBaseTypes: ts.Type[], methodName: string, diff --git a/ets2panda/linter/src/lib/ts-compiler/ResolveSdks.ts b/ets2panda/linter/src/lib/ts-compiler/ResolveSdks.ts index 290326c0fc..7ca65eeb23 100644 --- a/ets2panda/linter/src/lib/ts-compiler/ResolveSdks.ts +++ b/ets2panda/linter/src/lib/ts-compiler/ResolveSdks.ts @@ -35,6 +35,10 @@ export function readDeclareFiles(SdkPath: string): string[] { return []; } const declarationsFileNames: string[] = []; + const commonPath = path.resolve(SdkPath, './component/common.d.ts'); + if (fs.existsSync(commonPath)) { + declarationsFileNames.push(path.resolve(SdkPath, './component/common.d.ts')); + } const declarationsPath = path.resolve(SdkPath, './build-tools/ets-loader/declarations'); if (!fs.existsSync(declarationsPath)) { throw new Error('get wrong sdkDefaultApiPath, declarationsPath not found'); diff --git a/ets2panda/linter/src/lib/utils/functions/CommonApiInfo.ts b/ets2panda/linter/src/lib/utils/functions/CommonApiInfo.ts new file mode 100755 index 0000000000..cd7ad4f322 --- /dev/null +++ b/ets2panda/linter/src/lib/utils/functions/CommonApiInfo.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022-2024 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. + */ + +import path from 'node:path'; +import * as ts from 'typescript'; +import { forEachNodeInSubtree } from './ForEachNodeInSubtree'; + +export const COMMON_FILE_NAME = 'common.d.ts'; +const commonApiInfoMap = new Set(); +function visitSourceFile(sf: ts.SourceFile | undefined): void { + if (!sf) { + return; + } + const callback = (node: ts.Node): void => { + const isSave = ts.isMethodDeclaration(node) && ts.isClassDeclaration(node.parent); + if (isSave) { + commonApiInfoMap.add(node); + } + }; + forEachNodeInSubtree(sf, callback); +} +export function collectCommonApiInfo(tsProgram: ts.Program): void { + const rootNames = tsProgram.getRootFileNames(); + rootNames.some((file) => { + if (path.basename(file) === COMMON_FILE_NAME) { + const commonSrcFile = tsProgram.getSourceFile(file); + visitSourceFile(commonSrcFile); + return true; + } + return false; + }); +} +export function getCommonApiInfoMap(): Set | undefined { + return commonApiInfoMap.size > 0 ? commonApiInfoMap : undefined; +} -- Gitee