• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  setFilePath(fileFilePath: string): ApiStatisticsInfo {
53    this.filePath = fileFilePath;
54    this.packageName = FunctionUtils.getPackageName(fileFilePath);
55    return this;
56  }
57
58  getPackageName(): string {
59    return this.packageName;
60  }
61
62  getFilePath(): string {
63    return this.filePath;
64  }
65
66  setApiType(apiType: string): ApiStatisticsInfo {
67    this.apiType = apiType === ApiType.DECLARE_CONST ? ApiType.PROPERTY : apiType;
68    return this;
69  }
70
71  getParentModuleName(): string {
72    return this.parentModuleName;
73  }
74
75  setParentModuleName(parentModuleName: string): ApiStatisticsInfo {
76    this.parentModuleName = parentModuleName;
77    return this;
78  }
79
80  setSyscap(syscap?: string): ApiStatisticsInfo {
81    if (syscap) {
82      this.syscap = syscap;
83    }
84    return this;
85  }
86
87  getSyscap(): string {
88    return this.syscap;
89  }
90
91  setPermission(permissions: string): ApiStatisticsInfo {
92    this.permissions = permissions;
93    return this;
94  }
95
96  getPermission(): string {
97    return this.permissions;
98  }
99
100  setSince(since: string): ApiStatisticsInfo {
101    this.since = since;
102    return this;
103  }
104
105  getSince(): string {
106    return this.since;
107  }
108
109  setIsForm(isForm: boolean): ApiStatisticsInfo {
110    this.isForm = isForm;
111    return this;
112  }
113
114  getIsForm(): boolean {
115    return this.isForm;
116  }
117
118  setIsCrossPlatForm(isCrossPlatForm: boolean): ApiStatisticsInfo {
119    this.isCrossPlatForm = isCrossPlatForm;
120    return this;
121  }
122
123  getIsCrossPlatForm(): boolean {
124    return this.isCrossPlatForm;
125  }
126
127  setIsAutomicService(isAutomicService: boolean): ApiStatisticsInfo {
128    this.isAutomicService = isAutomicService;
129    return this;
130  }
131
132  getIsAutomicService(): boolean {
133    return this.isAutomicService;
134  }
135
136  getApiType(): string {
137    return this.apiType;
138  }
139
140  setDefinedText(definedText: string): ApiStatisticsInfo {
141    this.definedText = definedText;
142    return this;
143  }
144
145  getDefinedText(): string {
146    return this.definedText;
147  }
148
149  setPos(pos: ts.LineAndCharacter): ApiStatisticsInfo {
150    this.pos = pos;
151    return this;
152  }
153
154  getPos(): ts.LineAndCharacter {
155    return this.pos;
156  }
157
158  setApiName(apiName: string): ApiStatisticsInfo {
159    this.apiName = apiName;
160    return this;
161  }
162
163  getApiName(): string {
164    return this.apiName;
165  }
166
167  setHierarchicalRelations(hierarchicalRelations: string): ApiStatisticsInfo {
168    this.hierarchicalRelations = hierarchicalRelations;
169    return this;
170  }
171
172  getHierarchicalRelations(): string {
173    return this.hierarchicalRelations;
174  }
175
176  setDeprecatedVersion(deprecatedVersion: string): ApiStatisticsInfo {
177    this.deprecatedVersion = deprecatedVersion;
178    return this;
179  }
180
181  getDeprecatedVersion(): string {
182    return this.deprecatedVersion;
183  }
184
185  setUseInstead(useInstead: string): ApiStatisticsInfo {
186    this.useInstead = useInstead;
187    return this;
188  }
189
190  getUseInstead(): string {
191    return this.useInstead;
192  }
193
194  setApiLevel(isSystemApi: boolean): ApiStatisticsInfo {
195    this.isSystemapi = isSystemApi;
196    return this;
197  }
198
199  getApiLevel(): boolean {
200    return this.isSystemapi;
201  }
202
203  setModelLimitation(modelLimitation: string): ApiStatisticsInfo {
204    this.modelLimitation = modelLimitation;
205    return this;
206  }
207
208  getModelLimitation(): string {
209    return this.modelLimitation;
210  }
211
212  setDecorators(decorators: DecoratorInfo[] | undefined): ApiStatisticsInfo {
213    decorators?.forEach((decoratorInfo: DecoratorInfo) => {
214      this.decorators?.push(decoratorInfo.expression);
215    });
216    return this;
217  }
218
219  getDecorators(): Array<string> | undefined {
220    return this.decorators;
221  }
222
223  setErrorCodes(errorCodes: number[]): ApiStatisticsInfo {
224    this.errorCodes = errorCodes;
225    return this;
226  }
227
228  getErrorCodes(): number[] {
229    return this.errorCodes;
230  }
231}
232
233/**
234 * 需要统计为API的类型
235 */
236export const apiStatisticsType: Set<string> = new Set([
237  ApiType.PROPERTY,
238  ApiType.CLASS,
239  ApiType.INTERFACE,
240  ApiType.NAMESPACE,
241  ApiType.METHOD,
242  ApiType.CONSTANT,
243  ApiType.ENUM_VALUE,
244  ApiType.ENUM,
245  ApiType.TYPE_ALIAS,
246  ApiType.DECLARE_CONST,
247  ApiType.STRUCT,
248]);
249
250/**
251 * 不需要被统计成API的类型,但是子节点需要统计成API
252 */
253export const apiNotStatisticsType: Set<string> = new Set([ApiType.ENUM, ApiType.NAMESPACE]);
254
255/**
256 * 名字为on/off的函数,不合并API声明
257 */
258export const notMergeDefinedText: Set<string> = new Set(['on', 'off']);
259
260/**
261 * 需要进行同名函数合并的API类型
262 */
263export const mergeDefinedTextType: Set<number> = new Set([
264  ts.SyntaxKind.MethodDeclaration,
265  ts.SyntaxKind.MethodSignature,
266  ts.SyntaxKind.FunctionDeclaration,
267]);
268
269export type StatisticsInfoValueType = {
270  /**
271   * 统计工具返回的经过筛选后的数据
272   *
273   * @type {ApiStatisticsInfo[]}
274   */
275  apiStatisticsInfos: ApiStatisticsInfo[];
276
277  /**
278   * 统计工具返回的未经筛选后的数据
279   *
280   * @type {ApiStatisticsInfo[]}
281   */
282  allApiStatisticsInfos?: ApiStatisticsInfo[];
283};
284