diff --git a/build-tools/compiler-plugins/api-check-plugin-static/api-check-wrapper/utils/api_check_wrapper_enums.ts b/build-tools/compiler-plugins/api-check-plugin-static/api-check-wrapper/utils/api_check_wrapper_enums.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..78e1f83cb65214cccc2af5f7004cb11cad5d120e 100644 --- a/build-tools/compiler-plugins/api-check-plugin-static/api-check-wrapper/utils/api_check_wrapper_enums.ts +++ b/build-tools/compiler-plugins/api-check-plugin-static/api-check-wrapper/utils/api_check_wrapper_enums.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2025 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. + */ + +export enum DiagnosticCategory { + Warning = 0, + Error = 1 +} \ No newline at end of file diff --git a/build-tools/compiler-plugins/api-check-plugin-static/index.ts b/build-tools/compiler-plugins/api-check-plugin-static/index.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9ebab635eb2f66354fb6e26113a9783a94de0d51 100644 --- a/build-tools/compiler-plugins/api-check-plugin-static/index.ts +++ b/build-tools/compiler-plugins/api-check-plugin-static/index.ts @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2025 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 { Plugins, PluginContext } from '../common/plugin-context'; +import { GlobalObject, ProjectConfig } from './utils/api_check_plugin_typedef'; +import { createOrCleanProjectConfig, updateProjectConfig, initProjectConfig } from './utils/api_check_plugin_utils'; +import { ApiCheckWrapperServiceHost } from './api-check-wrapper'; +import { getApiCheckWrapperServiceHost } from './src/api_check_config'; +import { checkApiExpression, WrapperApi } from './api-check-wrapper/src/api_check_wrapper'; + +/** + * 导出projectConfig作为全局变量 + */ +export const globalObject: GlobalObject = { + projectConfig: createOrCleanProjectConfig() +} + +/** + * 预留apiCheckProjectConfig对象 + */ +export class apiCheckProjectConfig { + constructor(projectConfig: ProjectConfig | undefined) { + if (projectConfig) { + updateProjectConfig(projectConfig); + initProjectConfig(); + } + } +} + +/** + * 入口方法 + * @returns {Plugins} + */ +export function apiCheckPlugin(): Plugins { + return { + name: 'api-check-plugins', + checked: apiCheckCallback, + clean() { + console.log("API_CHECK_PLUGIN CLEAN") + WrapperApi.arktsGlobal.clearContext(); + } + }; +} + +/** + * 入口回调 + * @param {this} + */ +function apiCheckCallback(this: PluginContext): void { + console.log('[API_CHECK_PLUGIN] AFTER CHECKED ENTER'); + const currentProjectConfig: ProjectConfig | undefined = this.getProjectConfig() as ProjectConfig | undefined; + if (currentProjectConfig) { + updateProjectConfig(currentProjectConfig); + initProjectConfig(); + } + const ContextPtr = this.getContextPtr(); + const apiCheckHost: ApiCheckWrapperServiceHost = getApiCheckWrapperServiceHost(); + checkApiExpression(apiCheckHost, ContextPtr); + console.log('[API_CHECK_PLUGIN] AFTER CHECKED EXIT'); +} \ No newline at end of file diff --git a/build-tools/compiler-plugins/api-check-plugin-static/src/api_check_config.ts b/build-tools/compiler-plugins/api-check-plugin-static/src/api_check_config.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..104c9f50f286575f370ed4983cf4e13e073312e9 100644 --- a/build-tools/compiler-plugins/api-check-plugin-static/src/api_check_config.ts +++ b/build-tools/compiler-plugins/api-check-plugin-static/src/api_check_config.ts @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2025 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 'path'; +import { + PERMISSION_TAG_CHECK_NAME, + PERMISSION_TAG_CHECK_ERROR, + SYSTEM_API_TAG_CHECK_NAME, + SYSTEM_API_TAG_CHECK_WARNING, + TEST_TAG_CHECK_NAME, + TEST_TAG_CHECK_ERROR, + SYSCAP_TAG_CHECK_NAME, + SYSCAP_TAG_CHECK_WARNING, + FORM_TAG_CHECK_NAME, + FORM_TAG_CHECK_ERROR, + FIND_MODULE_WARNING, + CROSSPLATFORM_TAG_CHECK_NAME, + CROSSPLATFORM_TAG_CHECK_ERROR, + DEPRECATED_TAG_CHECK_NAME, + DEPRECATED_TAG_CHECK_WARNING, + FA_TAG_CHECK_NAME, + FA_TAG_HUMP_CHECK_NAME, + FA_TAG_CHECK_ERROR, + ATOMICSERVICE_BUNDLE_TYPE, + ATOMICSERVICE_TAG_CHECK_NAME, + ATOMICSERVICE_TAG_CHECK_ERROR, + ATOMICSERVICE_TAG_CHECK_VERSION, + SINCE_TAG_NAME, + SINCE_TAG_CHECK_ERROR, + STAGE_TAG_CHECK_NAME, + STAGE_TAG_HUMP_CHECK_NAME, + STAGE_TAG_CHECK_ERROR, + STAGE_COMPILE_MODE +} from '../utils/api_check_plugin_define'; +import { globalObject } from '../index'; +import { + checkPermissionTag, + checkSinceTag, + checkSyscapTag, + getJsDocNodeCheckConfigItem, + isCardFile, + pushLog, + collectInfo +} from '../utils/api_check_plugin_utils'; +import { + ApiCheckWrapperServiceHost, + CurrentAddress, + DiagnosticCategory, + JsDocNodeCheckConfig, + JsDocNodeCheckConfigItem +} from '../api-check-wrapper'; + +const jsDocNodeCheckConfigCache: Map> = + new Map>(); + +/** + * 根据ProjectConfig对象存入JsDoc校验规则 + * + * @param {string} fileName - 原始node文件路径 + * @param {string} sourceFileName - 目标api文件路径 + * @returns {JsDocNodeCheckConfig} - 返回校验规则对象 + */ +function getJsDocNodeCheckConfig(fileName: string, sourceFileName: string): JsDocNodeCheckConfig { + let byFileName: Map | undefined = jsDocNodeCheckConfigCache.get(fileName); + if (byFileName === undefined) { + byFileName = new Map(); + jsDocNodeCheckConfigCache.set(fileName, byFileName); + } + let result: JsDocNodeCheckConfig | undefined = byFileName.get(sourceFileName); + if (result !== undefined) { + return result; + } + // 预留check条件 + let needCheckResult: boolean = true; + const checkConfigArray: JsDocNodeCheckConfigItem[] = []; + const apiName: string = path.basename(fileName); + const sourceBaseName: string = path.basename(sourceFileName); + if (/(?= ATOMICSERVICE_TAG_CHECK_VERSION) { + checkConfigArray.push(getJsDocNodeCheckConfigItem([ATOMICSERVICE_TAG_CHECK_NAME], ATOMICSERVICE_TAG_CHECK_ERROR, + DiagnosticCategory.Error, true)); + } + } + result = { + nodeNeedCheck: needCheckResult, + checkConfig: checkConfigArray + }; + byFileName.set(sourceFileName, result); + return result; +} + + +/** + * 返回apiHost + * @returns {ApiCheckWrapperServiceHost} - apiHost + */ +export function getApiCheckWrapperServiceHost(): ApiCheckWrapperServiceHost { + return { + getJsDocNodeCheckedConfig: (currentFileName: string, symbolSourceFilePath: string) => { + return getJsDocNodeCheckConfig(currentFileName, symbolSourceFilePath); + }, + getFileCheckedModuleInfo: (containFilePath: string) => { + return { + fileNeedCheck: true, + currentFileName: containFilePath + }; + }, + pushLogInfo: (apiName: string, currentFilePath: string, currentAddress: CurrentAddress, + logLevel: DiagnosticCategory, logMessage: string) => { + return pushLog(apiName, currentFilePath, currentAddress, logLevel, logMessage); + }, + collectImportInfo: (moduleName: string[], modulePath: string, currentFilePath: string) => { + collectInfo(moduleName, modulePath, currentFilePath); + } + }; +} \ No newline at end of file diff --git a/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_define.ts b/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_define.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..061091846f37994fb03f21914792886f70b62d12 100644 --- a/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_define.ts +++ b/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_define.ts @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 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. + */ + +export const PERMISSION_TAG_CHECK_NAME: string = 'permission'; + +export const PERMISSION_TAG_CHECK_ERROR: string = "To use this API, you need to apply for the permissions: $DT"; + +export const SYSTEM_API_TAG_CHECK_NAME: string = 'systemapi'; + +export const SYSTEM_API_TAG_CHECK_WARNING: string = "'{0}' is system api"; + +export const TEST_TAG_CHECK_NAME: string = 'test'; + +export const TEST_TAG_CHECK_ERROR: string = "'{0}' can only be used for testing directories "; + +export const SYSCAP_TAG_CHECK_NAME: string = 'syscap'; + +export const SYSCAP_TAG_CHECK_WARNING: string = "The system capacity of this api '{0}' is not supported on all devices"; + +export const SYSCAP_TAG_CONDITION_CHECK_WARNING: string = + 'The API is not supported on all devices. Use the canIUse condition to determine whether the API is supported.'; + +export const CANIUSE_FUNCTION_NAME: string = 'canIUse'; + +export const VERSION_CHECK_FUNCTION_NAME: string = 'isApiVersionGreaterOrEqual'; + +export const RUNTIME_OS_OH: string = 'OpenHarmony'; + +export const FORM_TAG_CHECK_NAME: string = 'form'; + +export const FORM_TAG_CHECK_ERROR: string = "'{0}' can't support form application."; + +export const CROSSPLATFORM_TAG_CHECK_NAME: string = 'crossplatform'; + +export const CROSSPLATFORM_TAG_CHECK_ERROR: string = "'{0}' can't support crossplatform application."; + +export const DEPRECATED_TAG_CHECK_NAME: string = 'deprecated'; + +export const DEPRECATED_TAG_CHECK_WARNING: string = "'{0}' has been deprecated."; + +export const FA_TAG_CHECK_NAME: string = 'famodelonly'; + +export const FA_TAG_HUMP_CHECK_NAME: string = 'FAModelOnly'; + +export const FA_TAG_CHECK_ERROR: string = 'This API is used only in FA Mode, but the current Mode is Stage.'; + +export const STAGE_TAG_CHECK_NAME: string = 'stagemodelonly'; + +export const STAGE_TAG_HUMP_CHECK_NAME: string = 'StageModelOnly'; + +export const STAGE_TAG_CHECK_ERROR: string = 'This API is used only in Stage Mode, but the current Mode is FA.'; + +export const STAGE_COMPILE_MODE: string = 'moduleJson'; + +export const ATOMICSERVICE_BUNDLE_TYPE: string = 'atomicService'; + +export const ATOMICSERVICE_TAG_CHECK_NAME: string = 'atomicservice'; + +export const ATOMICSERVICE_TAG_CHECK_ERROR: string = "'{0}' can't support atomicservice application."; + +export const SINCE_TAG_NAME: string = 'since'; + +export const SINCE_TAG_CHECK_ERROR: string = + "The '{0}' API is supported since SDK version $SINCE1. However, the current compatible SDK version is $SINCE2."; + +export const ATOMICSERVICE_TAG_CHECK_VERSION: number = 11; + +export const FIND_MODULE_WARNING: string = "Cannot find name '{0}'."; + +export const CONSTANT_STEP_0: number = 0; + +export const CONSTANT_STEP_1: number = 1; + +export const CONSTANT_STEP_2: number = 2; + +export const CONSTANT_STEP_3: number = 3; + +export const GLOBAL_DECLARE_WHITE_LIST: Set = new Set(['Context', 'PointerStyle', 'PixelMap', 'UnifiedData', + 'Summary', 'UniformDataType', 'IntentionCode', 'NavDestinationInfo', 'UIContext', 'Resource', 'WebviewController']); + +export const MESSAGE_CONFIG_COLOR_RED: string = '\u001b[31m'; + +export const MESSAGE_CONFIG_COLOR_RESET: string = '\u001b[39m'; + +export const MESSAGE_CONFIG_COLOR_ERROR: string = 'color:#f5a623'; + +export const MESSAGE_CONFIG_COLOR_WARNING: string = 'color:#d0021b'; + +export const MESSAGE_CONFIG_HEADER_ERROR: string = 'ERROR: ArkTS:ERROR File: '; + +export const MESSAGE_CONFIG_HEADER_WARNING: string = 'WARN: ArkTS:WARN File: '; \ No newline at end of file diff --git a/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_enums.ts b/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_enums.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1208ff3c6564041f33d331ce4064ddafad3ab3b7 100644 --- a/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_enums.ts +++ b/build-tools/compiler-plugins/api-check-plugin-static/utils/api_check_plugin_enums.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 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. + */ + +export enum PermissionValidTokenState { + Init, + LeftParenthesis, + RightParenthesis, + PermissionChar, + And, + Or, +} \ No newline at end of file