1/* 2 * Copyright (c) 2024 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 path from 'path'; 17import { SyntaxKind } from 'typescript'; 18import { Declares, MockBuffer, mockedFilePath, rawFilePath } from '../types'; 19import { handleIterableIterator } from './commonUtils'; 20 21// api 路径参数所在索引 22export const paramIndex = 2; 23 24// 源文件到mock文件的映射 25export const RawFileMap: Map<rawFilePath, mockedFilePath> = new Map<rawFilePath, mockedFilePath>(); 26 27// mock文件到源文件的映射 28export const MockedFileMap: Map<mockedFilePath, rawFilePath> = new Map<mockedFilePath, rawFilePath>(); 29 30// 开源api接口文件列表 31export const ohosDtsFileList: string[] = []; 32 33// 导入全局声明类型的文件的列表 34export const importDeclarationFiles: string[] = []; 35 36// compoent 目录所有类型文件的列表 37export const componentDtsFileList: string[] = []; 38 39// arkts 目录所有类型文件的列表 40export const arktsDtsFileList: string[] = []; 41 42// kits 目录所有类型文件的列表 43export const kitsDtsFileList: string[] = []; 44 45// 源文件路径到mock信息的映射 46export const mockBufferMap: Map<rawFilePath, MockBuffer> = new Map(); 47 48/** 49 * 由于有些接口类型不需要直接mock,这会导致部分接口文件不用生成任何内容 50 * 这导致引入这样的文件后,运行时会报错 51 * 因此需要将空文件收集起来,以便后续进行忽略 52 */ 53export const NO_CONTENT_FILES: Set<string> = new Set<string>(); 54 55// typescript 内置高级类型的mock方式 56export const TSTypes = { 57 Record: (): string => '{}', 58 IterableIterator: (typeParams: string): string => handleIterableIterator(typeParams), 59 PropertyDecorator: (): string => '() => {}', 60 ClassDecorator: (): string => '() => {}', 61 MethodDecorator: (): string => '() => {}', 62 Readonly: (params: string): string => params, 63 CanvasRenderingContext2D: (): string => '{name: \'The CanvasRenderingContext2D is not mocked\'}', 64 WebGLRenderingContext: (): string => '{name: \'The WebGLRenderingContext is not mocked\'}', 65 WebGL2RenderingContext: (): string => '{name: \'The WebGL2RenderingContext is not mocked\'}', 66 thisType: (): string => '{}' 67}; 68 69export const specialTSTypes = ['Record']; 70 71// 可以忽略的typescript内置类型 72export const IGNORE_REFERENCES: Set<string> = new Set<string>([ 73 'Promise', 74 'Partial', 75 'Required', 76 'Pick', 77 'Exclude', 78 'Extract', 79 'Omit', 80 'NonNullable' 81]); 82 83// 组件装饰器 84export const COMPONENT_DECORATORS: string[] = ['CustomDialog', 'Component']; 85 86// keyValue 节点类型 87export enum KeyValueTypes { 88 FUNCTION, 89 CLASS, 90 VALUE, 91 FILE, 92 MODULE, 93 INTERFACE, 94 IMPORT, 95 EXPORT, 96 INTERSECTION, 97 VARIABLE, 98 REFERENCE, 99 PROPERTY, 100 ENUM, 101 EXPRESSION, 102} 103 104// napi 目录 105export const NAPI_DIR_PATH = path.join(__dirname, '..', '..', '..', 'runtime', 'main', 'extend', 'systemplugin', 'napi'); 106 107// 全局声明的类型集 108export const DECLARES: Declares = {}; 109 110// typescript的基础类型 111export const BASE_KINDS: SyntaxKind[] = [ 112 SyntaxKind.AnyKeyword, 113 SyntaxKind.BigIntKeyword, 114 SyntaxKind.BooleanKeyword, 115 SyntaxKind.IntrinsicKeyword, 116 SyntaxKind.NeverKeyword, 117 SyntaxKind.NumberKeyword, 118 SyntaxKind.ObjectKeyword, 119 SyntaxKind.StringKeyword, 120 SyntaxKind.SymbolKeyword, 121 SyntaxKind.UndefinedKeyword, 122 SyntaxKind.UnknownKeyword, 123 SyntaxKind.VoidKeyword, 124 SyntaxKind.NullKeyword, 125 SyntaxKind.LiteralType 126]; 127 128// typescript 关键字 129export const TYPESCRIPT_KEYWORDS: Set<string> = new Set([ 130 'abstract', 'any', 'as', 'async', 'await', 'boolean', 'break', 'byte', 'case', 'catch', 'class', 'const', 131 'constructor', 'continue', 'debugger', 'declare', 'default', 'delete', 'do', 'double', 'else', 'enum', 132 'export', 'extends', 'false', 'finally', 'float', 'for', 'from', 'function', 'get', 'global', 'goto', 133 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'is', 'let', 'long', 'map', 'module', 134 'namespace', 'native', 'new', 'null', 'of', 'package', 'private', 'protected', 'public', 'readonly', 135 'require', 'return', 'set', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 136 'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield', 'async', 'await', 137 'declare', 'readonly', 'assert', 'unknown', 'partial', 'readonlyRecord', 'override', 'infer', 'satisfies', 138 'as const', 'is unknown', 'is any', 'is never', 'checks', 'constraint', 'covariant', 'contravariant', 'invariant' 139]); 140 141export const ClassNotInEts: Set<string> = new Set(['TextEncoder', 'TextDecoder', 'URL']); 142 143// .d.ts 文件后缀 144export const D_TS: string = '.d.ts'; 145 146// .d.ets 文件后缀 147export const D_ETS: string = '.d.ets'; 148 149// on、off函数做特殊处理 150export const specialOverloadedFunctionArr = ['on', 'off']; 151 152// callback error info 153export const callbackError = '{\'code\': \'\',\'data\': \'\',\'name\': \'\',\'message\': \'\',\'stack\': \'\'}, '; 154 155// Mapping configuration of corresponding open source files in closed source files 156export const ClosedSourceFileMapping = { 157 '@ohos.inner.application.Context': 'application/Context', 158 '@ohos.inner.application.ExtensionContext': 'application/ExtensionContext', 159 '@ohos.inner.application.UIExtensionContext': 'application/UIExtensionContext' 160}; 161