/* * Copyright (c) 2024 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 { SyntaxKind } from 'typescript'; import { Declares, MockBuffer, mockedFilePath, rawFilePath } from '../types'; import { handleIterableIterator } from './commonUtils'; // api 路径参数所在索引 export const paramIndex = 2; // 源文件到mock文件的映射 export const RawFileMap: Map = new Map(); // mock文件到源文件的映射 export const MockedFileMap: Map = new Map(); // 开源api接口文件列表 export const ohosDtsFileList: string[] = []; // 导入全局声明类型的文件的列表 export const importDeclarationFiles: string[] = []; // compoent 目录所有类型文件的列表 export const componentDtsFileList: string[] = []; // arkts 目录所有类型文件的列表 export const arktsDtsFileList: string[] = []; // kits 目录所有类型文件的列表 export const kitsDtsFileList: string[] = []; // 源文件路径到mock信息的映射 export const mockBufferMap: Map = new Map(); /** * 由于有些接口类型不需要直接mock,这会导致部分接口文件不用生成任何内容 * 这导致引入这样的文件后,运行时会报错 * 因此需要将空文件收集起来,以便后续进行忽略 */ export const NO_CONTENT_FILES: Set = new Set(); // typescript 内置高级类型的mock方式 export const TSTypes = { Record: (): string => '{}', IterableIterator: (typeParams: string): string => handleIterableIterator(typeParams), PropertyDecorator: (): string => '() => {}', ClassDecorator: (): string => '() => {}', MethodDecorator: (): string => '() => {}', Readonly: (params: string): string => params, CanvasRenderingContext2D: (): string => '{name: \'The CanvasRenderingContext2D is not mocked\'}', WebGLRenderingContext: (): string => '{name: \'The WebGLRenderingContext is not mocked\'}', WebGL2RenderingContext: (): string => '{name: \'The WebGL2RenderingContext is not mocked\'}', thisType: (): string => '{}' }; export const specialTSTypes = ['Record']; // 可以忽略的typescript内置类型 export const IGNORE_REFERENCES: Set = new Set([ 'Promise', 'Partial', 'Required', 'Pick', 'Exclude', 'Extract', 'Omit', 'NonNullable' ]); // 组件装饰器 export const COMPONENT_DECORATORS: string[] = ['CustomDialog', 'Component']; // keyValue 节点类型 export enum KeyValueTypes { FUNCTION, CLASS, VALUE, FILE, MODULE, INTERFACE, IMPORT, EXPORT, INTERSECTION, VARIABLE, REFERENCE, PROPERTY, ENUM, EXPRESSION, } // napi 目录 export const NAPI_DIR_PATH = path.join(__dirname, '..', '..', '..', 'runtime', 'main', 'extend', 'systemplugin', 'napi'); // 全局声明的类型集 export const DECLARES: Declares = {}; // typescript的基础类型 export const BASE_KINDS: SyntaxKind[] = [ SyntaxKind.AnyKeyword, SyntaxKind.BigIntKeyword, SyntaxKind.BooleanKeyword, SyntaxKind.IntrinsicKeyword, SyntaxKind.NeverKeyword, SyntaxKind.NumberKeyword, SyntaxKind.ObjectKeyword, SyntaxKind.StringKeyword, SyntaxKind.SymbolKeyword, SyntaxKind.UndefinedKeyword, SyntaxKind.UnknownKeyword, SyntaxKind.VoidKeyword, SyntaxKind.NullKeyword, SyntaxKind.LiteralType ]; // typescript 关键字 export const TYPESCRIPT_KEYWORDS: Set = new Set([ 'abstract', 'any', 'as', 'async', 'await', 'boolean', 'break', 'byte', 'case', 'catch', 'class', 'const', 'constructor', 'continue', 'debugger', 'declare', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'float', 'for', 'from', 'function', 'get', 'global', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'is', 'let', 'long', 'map', 'module', 'namespace', 'native', 'new', 'null', 'of', 'package', 'private', 'protected', 'public', 'readonly', 'require', 'return', 'set', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield', 'async', 'await', 'declare', 'readonly', 'assert', 'unknown', 'partial', 'readonlyRecord', 'override', 'infer', 'satisfies', 'as const', 'is unknown', 'is any', 'is never', 'checks', 'constraint', 'covariant', 'contravariant', 'invariant' ]); export const ClassNotInEts: Set = new Set(['TextEncoder', 'TextDecoder', 'URL']); // .d.ts 文件后缀 export const D_TS: string = '.d.ts'; // .d.ets 文件后缀 export const D_ETS: string = '.d.ets'; // on、off函数做特殊处理 export const specialOverloadedFunctionArr = ['on', 'off']; // callback error info export const callbackError = '{\'code\': \'\',\'data\': \'\',\'name\': \'\',\'message\': \'\',\'stack\': \'\'}, '; // Mapping configuration of corresponding open source files in closed source files export const ClosedSourceFileMapping = { '@ohos.inner.application.Context': 'application/Context', '@ohos.inner.application.ExtensionContext': 'application/ExtensionContext', '@ohos.inner.application.UIExtensionContext': 'application/UIExtensionContext' }; // Filter files that are not mocked export const needShieldingDirOrFile = [ path.join('arkui', 'component'), path.join('arkui', 'external'), path.join('arkui', 'runtime-api'), path.join('arkui', 'stateManagement') ]; // Special handling for interface types not found globally export const undefinedTypes = { 'int': 0, 'double': 0, 'float': 0, 'long': 0, 'VpnConnectState': 'undefined', 'Intl': 'undefined' };