1/* 2 * Copyright (c) 2023 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 ts from 'typescript'; 17 18import { ApiType } from '../parser/ApiInfoDefination'; 19import { DecoratorInfo } from '../../typedef/parser/Decorator'; 20import { FunctionUtils } from '../../utils/FunctionUtils'; 21 22/** 23 * 统计工具类,用于存取API相关信息 24 */ 25export class ApiStatisticsInfo { 26 filePath: string = ''; 27 packageName: string = ''; 28 parentModuleName: string = 'global'; 29 // @syscap标签--api的系统能力 30 syscap: string = ''; 31 // @permission标签--api的权限 32 permissions: string = ''; 33 // @since标签--api的起始版本 34 since: string = ''; 35 // @form标签--api是否支持卡片应用 36 isForm: boolean = false; 37 // @crossplatform标签--api是否支持跨平台 38 isCrossPlatForm: boolean = false; 39 // @atomicservice标签--是否为高阶API 40 isAutomicService: boolean = false; 41 hierarchicalRelations: string = ''; 42 apiName: string = ''; 43 deprecatedVersion: string = ''; 44 useInstead: string = ''; 45 apiType: string = ''; 46 definedText: string = ''; 47 pos: ts.LineAndCharacter = { line: -1, character: -1 }; 48 isSystemapi: boolean = false; 49 modelLimitation: string = ''; 50 decorators: Array<string> | undefined = []; 51 errorCodes: number[] = []; 52 kitInfo: string = ''; 53 absolutePath: string = ''; //文件绝对路径 54 parentApiType: string = ''; 55 isOptional: boolean = false; 56 57 setFilePath(fileFilePath: string): ApiStatisticsInfo { 58 this.filePath = fileFilePath; 59 this.packageName = FunctionUtils.getPackageName(fileFilePath); 60 return this; 61 } 62 63 getPackageName(): string { 64 return this.packageName; 65 } 66 67 getFilePath(): string { 68 return this.filePath; 69 } 70 71 setApiType(apiType: string): ApiStatisticsInfo { 72 this.apiType = apiType === ApiType.DECLARE_CONST ? ApiType.PROPERTY : apiType; 73 return this; 74 } 75 76 getParentModuleName(): string { 77 return this.parentModuleName; 78 } 79 80 setParentModuleName(parentModuleName: string): ApiStatisticsInfo { 81 this.parentModuleName = parentModuleName; 82 return this; 83 } 84 85 setSyscap(syscap?: string): ApiStatisticsInfo { 86 if (syscap) { 87 this.syscap = syscap; 88 } 89 return this; 90 } 91 92 getSyscap(): string { 93 return this.syscap; 94 } 95 96 setPermission(permissions: string): ApiStatisticsInfo { 97 this.permissions = permissions; 98 return this; 99 } 100 101 getPermission(): string { 102 return this.permissions; 103 } 104 105 setSince(since: string): ApiStatisticsInfo { 106 this.since = since; 107 return this; 108 } 109 110 getSince(): string { 111 return this.since; 112 } 113 114 setIsForm(isForm: boolean): ApiStatisticsInfo { 115 this.isForm = isForm; 116 return this; 117 } 118 119 getIsForm(): boolean { 120 return this.isForm; 121 } 122 123 setIsCrossPlatForm(isCrossPlatForm: boolean): ApiStatisticsInfo { 124 this.isCrossPlatForm = isCrossPlatForm; 125 return this; 126 } 127 128 getIsCrossPlatForm(): boolean { 129 return this.isCrossPlatForm; 130 } 131 132 setIsAutomicService(isAutomicService: boolean): ApiStatisticsInfo { 133 this.isAutomicService = isAutomicService; 134 return this; 135 } 136 137 getIsAutomicService(): boolean { 138 return this.isAutomicService; 139 } 140 141 getApiType(): string { 142 return this.apiType; 143 } 144 145 setDefinedText(definedText: string): ApiStatisticsInfo { 146 this.definedText = definedText; 147 return this; 148 } 149 150 getDefinedText(): string { 151 return this.definedText; 152 } 153 154 setPos(pos: ts.LineAndCharacter): ApiStatisticsInfo { 155 this.pos = pos; 156 return this; 157 } 158 159 getPos(): ts.LineAndCharacter { 160 return this.pos; 161 } 162 163 setApiName(apiName: string): ApiStatisticsInfo { 164 this.apiName = apiName; 165 return this; 166 } 167 168 getApiName(): string { 169 return this.apiName; 170 } 171 172 setHierarchicalRelations(hierarchicalRelations: string): ApiStatisticsInfo { 173 this.hierarchicalRelations = hierarchicalRelations; 174 return this; 175 } 176 177 getHierarchicalRelations(): string { 178 return this.hierarchicalRelations; 179 } 180 181 setDeprecatedVersion(deprecatedVersion: string): ApiStatisticsInfo { 182 this.deprecatedVersion = deprecatedVersion; 183 return this; 184 } 185 186 getDeprecatedVersion(): string { 187 return this.deprecatedVersion; 188 } 189 190 setUseInstead(useInstead: string): ApiStatisticsInfo { 191 this.useInstead = useInstead; 192 return this; 193 } 194 195 getUseInstead(): string { 196 return this.useInstead; 197 } 198 199 setApiLevel(isSystemApi: boolean): ApiStatisticsInfo { 200 this.isSystemapi = isSystemApi; 201 return this; 202 } 203 204 getApiLevel(): boolean { 205 return this.isSystemapi; 206 } 207 208 setModelLimitation(modelLimitation: string): ApiStatisticsInfo { 209 this.modelLimitation = modelLimitation; 210 return this; 211 } 212 213 getModelLimitation(): string { 214 return this.modelLimitation; 215 } 216 217 setDecorators(decorators: DecoratorInfo[] | undefined): ApiStatisticsInfo { 218 decorators?.forEach((decoratorInfo: DecoratorInfo) => { 219 this.decorators?.push(decoratorInfo.expression); 220 }); 221 return this; 222 } 223 224 getDecorators(): Array<string> | undefined { 225 return this.decorators; 226 } 227 228 setErrorCodes(errorCodes: number[]): ApiStatisticsInfo { 229 this.errorCodes = errorCodes; 230 return this; 231 } 232 233 getErrorCodes(): number[] { 234 return this.errorCodes; 235 } 236 237 setKitInfo(kitInfo: string): ApiStatisticsInfo { 238 this.kitInfo = kitInfo; 239 return this; 240 } 241 242 getKitInfo(): string { 243 return this.kitInfo; 244 } 245 246 setAbsolutePath(absolutePath: string): ApiStatisticsInfo { 247 this.absolutePath = absolutePath; 248 return this; 249 } 250 251 getAbsolutePath(): string { 252 return this.absolutePath; 253 } 254 255 setParentApiType(parentApiType: string): ApiStatisticsInfo { 256 this.parentApiType = parentApiType; 257 return this; 258 } 259 260 getParentApiType(): string { 261 return this.parentApiType; 262 } 263 264 setIsOptional(isOptional: boolean): ApiStatisticsInfo { 265 this.isOptional = isOptional; 266 return this; 267 } 268 getIsOptional(): boolean { 269 return this.isOptional; 270 } 271} 272 273/** 274 * 需要统计为API的类型 275 */ 276export const apiStatisticsType: Set<string> = new Set([ 277 ApiType.PROPERTY, 278 ApiType.CLASS, 279 ApiType.INTERFACE, 280 ApiType.NAMESPACE, 281 ApiType.METHOD, 282 ApiType.CONSTANT, 283 ApiType.ENUM_VALUE, 284 ApiType.ENUM, 285 ApiType.TYPE_ALIAS, 286 ApiType.DECLARE_CONST, 287 ApiType.STRUCT, 288]); 289 290/** 291 * 不需要被统计成API的类型,但是子节点需要统计成API 292 */ 293export const apiNotStatisticsType: Set<string> = new Set([ApiType.ENUM, ApiType.NAMESPACE]); 294 295/** 296 * 名字为on/off的函数,不合并API声明 297 */ 298export const notMergeDefinedText: Set<string> = new Set(['on', 'off']); 299 300/** 301 * 需要进行同名函数合并的API类型 302 */ 303export const mergeDefinedTextType: Set<number> = new Set([ 304 ts.SyntaxKind.MethodDeclaration, 305 ts.SyntaxKind.MethodSignature, 306 ts.SyntaxKind.FunctionDeclaration, 307]); 308 309export type StatisticsInfoValueType = { 310 /** 311 * 统计工具返回的经过筛选后的数据 312 * 313 * @type {ApiStatisticsInfo[]} 314 */ 315 apiStatisticsInfos: ApiStatisticsInfo[]; 316 317 /** 318 * 统计工具返回的未经筛选后的数据 319 * 320 * @type {ApiStatisticsInfo[]} 321 */ 322 allApiStatisticsInfos?: ApiStatisticsInfo[]; 323}; 324