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 { Language } from 'arkanalyzer/lib/core/model/ArkFile'; 17 18export class InteropRuleInfo { 19 ruleId: string; 20 severity: number; 21 description: string; 22} 23 24// 1.2 import 1.1 25// 1.1 Object => ArkTS 1.2 对象 26export const D2S_DYNAMIC_OBJECT: InteropRuleInfo = { 27 ruleId: 'arkts-interop-d2s-dynamic-object-on-static-instance', 28 severity: 1, 29 description: "ArkTS 1.1 Object's built-in methods work on ArkTS 1.2 objects", 30}; 31 32// 1.2 import 1.1 33// 1.1 Reflect => ArkTS 1.2 对象 34export const D2S_DYNAMIC_REFLECT: InteropRuleInfo = { 35 ruleId: 'arkts-interop-d2s-dynamic-reflect-on-static-instance', 36 severity: 1, 37 description: "ArkTS 1.1 Reflect's built-in methods work on ArkTS 1.2 objects", 38}; 39 40// 1.2 import 1.1 41// 1.2 Object => ArkTS 1.1 对象 42export const D2S_STATIC_OBJECT: InteropRuleInfo = { 43 ruleId: 'arkts-interop-d2s-static-object-on-dynamic-instance', 44 severity: 1, 45 description: "ArkTS 1.2 Object's built-in methods work on ArkTS 1.1 objects", 46}; 47 48// 1.2 import 1.1 49// 1.2 Reflect => ArkTS 1.1 对象 50export const D2S_STATIC_REFLECT: InteropRuleInfo = { 51 ruleId: 'arkts-interop-d2s-static-reflect-on-dynamic-instance', 52 severity: 1, 53 description: "ArkTS 1.2 Reflect's built-in methods work on ArkTS 1.1 objects", 54}; 55 56// 1.1 import 1.2 57// 1.1 Object => ArkTS 1.2 对象 58export const S2D_DYNAMIC_OBJECT: InteropRuleInfo = { 59 ruleId: 'arkts-interop-s2d-dynamic-object-on-static-instance', 60 severity: 1, 61 description: "ArkTS 1.1 Object's built-in methods work on ArkTS 1.2 objects", 62}; 63 64// 1.1 import 1.2 65// 1.1 Reflect => ArkTS 1.2 对象 66export const S2D_DYNAMIC_REFLECT: InteropRuleInfo = { 67 ruleId: 'arkts-interop-s2d-dynamic-reflect-on-static-instance', 68 severity: 1, 69 description: "ArkTS 1.1 Reflect's built-in methods work on ArkTS 1.2 objects", 70}; 71 72// 1.1 import 1.2 73// 1.2 Object => 1.1 对象 74export const S2D_STATIC_OBJECT: InteropRuleInfo = { 75 ruleId: 'arkts-interop-s2d-static-object-on-dynamic-instance', 76 severity: 1, 77 description: "ArkTS 1.2 Object's built-in methods work on ArkTS 1.1 objects", 78}; 79 80// 1.1 import 1.2 81// 1.2 Reflect => 1.1 对象 82export const S2D_STATIC_REFLECT: InteropRuleInfo = { 83 ruleId: 'arkts-interop-s2d-static-reflect-on-dynamic-instance', 84 severity: 1, 85 description: "ArkTS 1.2 Reflect's built-in methods work on ArkTS 1.1 objects", 86}; 87 88export const TS_OBJECT: InteropRuleInfo = { 89 ruleId: 'arkts-interop-ts2s-ts-object-on-static-instance', 90 severity: 1, 91 description: "TypeScript Object's built-in methods work on ArkTS objects", 92}; 93 94export const TS_REFLECT: InteropRuleInfo = { 95 ruleId: 'arkts-interop-ts2s-ts-reflect-on-static-instance', 96 severity: 1, 97 description: "TypeScript Reflect's built-in methods work on ArkTS objects", 98}; 99 100export const JS_OBJECT: InteropRuleInfo = { 101 ruleId: 'arkts-interop-js2s-js-object-on-static-instance', 102 severity: 1, 103 description: "JavaScript Object's built-in methods work on ArkTS objects", 104}; 105 106export const JS_REFLECT: InteropRuleInfo = { 107 ruleId: 'arkts-interop-js2s-js-reflect-on-static-instance', 108 severity: 1, 109 description: "JavaScript Reflect's built-in methods work on ArkTS objects", 110}; 111 112export function findInteropRule( 113 methodLang: Language, 114 typeDefLang: Language, 115 problemStmtLang: Language, 116 isReflect: boolean 117): InteropRuleInfo | undefined { 118 if (methodLang === Language.ARKTS1_2 && typeDefLang === Language.ARKTS1_1) { 119 if (problemStmtLang === Language.ARKTS1_1) { 120 // 包含 object API 的 1.2 函数被导出到 1.1 文件使用 121 return isReflect ? S2D_STATIC_REFLECT : S2D_STATIC_OBJECT; 122 } else { 123 return undefined; 124 } 125 } 126 if (methodLang === Language.ARKTS1_1 && typeDefLang === Language.ARKTS1_2) { 127 if (problemStmtLang === Language.ARKTS1_2) { 128 // 包含 object API 的 1.1 函数被导出到 1.2 文件使用 129 return isReflect ? D2S_DYNAMIC_REFLECT : D2S_DYNAMIC_OBJECT; 130 } else { 131 return isReflect ? S2D_DYNAMIC_REFLECT: S2D_DYNAMIC_OBJECT ; 132 } 133 } 134 if (methodLang === Language.TYPESCRIPT && typeDefLang === Language.ARKTS1_2) { 135 return isReflect ? TS_REFLECT : TS_OBJECT; 136 } 137 if (methodLang === Language.JAVASCRIPT && typeDefLang === Language.ARKTS1_2) { 138 return isReflect ? JS_REFLECT : JS_OBJECT; 139 } 140 return undefined; 141}