1/* 2 * Copyright (c) 2022 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 { SourceFile, SyntaxKind } from 'typescript'; 17import { PropertySignatureEntity } from '../declaration-node/propertySignatureDeclaration'; 18import { 19 checkIsGenericSymbol, getCallbackStatement, getTheRealReferenceFromImport, 20 getWarnConsole, propertyTypeWhiteList 21} from './generateCommonUtil'; 22 23/** 24 * generate interface signature property 25 * @param rootName 26 * @param propertySignature 27 * @param sourceFile 28 * @returns 29 */ 30export function generatePropertySignatureDeclaration(rootName: string, propertySignature: PropertySignatureEntity, sourceFile: SourceFile): string { 31 let propertySignatureBody = ''; 32 if (propertySignature.kind === SyntaxKind.FunctionType) { 33 propertySignatureBody += `${propertySignature.propertyName}: function(...args) {`; 34 propertySignatureBody += getWarnConsole(rootName, propertySignature.propertyName); 35 propertySignatureBody += getCallbackStatement(); 36 propertySignatureBody += '},\n'; 37 } else { 38 if (propertySignature.propertyTypeName.startsWith('{')) { 39 propertySignatureBody = `${propertySignature.propertyName}: {},`; 40 } else if (propertySignature.kind === SyntaxKind.TypeReference) { 41 if (propertySignature.propertyTypeName.startsWith('Array')) { 42 propertySignatureBody = `${propertySignature.propertyName}: [],`; 43 } else if (propertySignature.propertyTypeName.startsWith('Map')) { 44 propertySignatureBody = `${propertySignature.propertyName}: {key: {}},`; 45 } else if (propertySignature.propertyTypeName === 'string' || checkIsGenericSymbol(propertySignature.propertyTypeName) || 46 propertySignature.propertyTypeName === 'bool' || propertySignature.propertyTypeName === 'Data') { 47 propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unkonwn ${propertySignature.propertyName}',`; 48 } else { 49 if (propertySignature.propertyTypeName.includes('<')) { 50 const preSplit = propertySignature.propertyTypeName.split('<'); 51 const genericArg = preSplit[preSplit.length - 1].split('>')[0]; 52 propertySignatureBody = `${propertySignature.propertyName}: ${genericArg},`; 53 } else { 54 if (propertyTypeWhiteList(propertySignature.propertyTypeName) === propertySignature.propertyTypeName) { 55 propertySignatureBody = `${propertySignature.propertyName}: ${getTheRealReferenceFromImport(sourceFile, propertySignature.propertyTypeName)},`; 56 } else { 57 propertySignatureBody = `${propertySignature.propertyName}: ${propertyTypeWhiteList(propertySignature.propertyTypeName)},`; 58 } 59 } 60 } 61 } else if (propertySignature.kind === SyntaxKind.NumberKeyword) { 62 propertySignatureBody = `${propertySignature.propertyName}: 0,`; 63 } else if (propertySignature.kind === SyntaxKind.StringKeyword) { 64 propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unkonwn ${propertySignature.propertyName}',`; 65 } else if (propertySignature.kind === SyntaxKind.BooleanKeyword) { 66 propertySignatureBody = `${propertySignature.propertyName}: true,`; 67 } else if (propertySignature.kind === SyntaxKind.UnionType) { 68 const unionFirstElement = propertySignature.propertyTypeName.split('|')[0].trimStart().trimEnd(); 69 if (unionFirstElement.startsWith('"')) { 70 propertySignatureBody = `${propertySignature.propertyName}: ${unionFirstElement},`; 71 } else if (unionFirstElement === 'string') { 72 propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unkonwn ${propertySignature.propertyName}',`; 73 } else if (unionFirstElement === 'number') { 74 propertySignatureBody = `${propertySignature.propertyName}: 0,`; 75 } else if (unionFirstElement === 'boolean') { 76 propertySignatureBody = `${propertySignature.propertyName}: true,`; 77 } else if (unionFirstElement === 'Uint8Array') { 78 propertySignatureBody = `${propertySignature.propertyName}: new ${unionFirstElement}(),`; 79 } else { 80 let element = unionFirstElement; 81 if (element === 'HTMLCanvasElement') { 82 element = `'[PC Preview] unkonwn ${propertySignature.propertyName}'`; 83 } else if (element === 'WebGLActiveInfo') { 84 element = `{size: '[PC Preview] unkonwn GLint', type: 0, name: '[PC Preview] unkonwn name'}`; 85 } else if (element === 'accessibility.EventType') { 86 element = `mockAccessibility().EventType`; 87 } 88 propertySignatureBody = `${propertySignature.propertyName}: ${element},`; 89 } 90 } else if (propertySignature.kind === SyntaxKind.ArrayType) { 91 propertySignatureBody = `${propertySignature.propertyName}: [],`; 92 } else { 93 propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unkonwn ${propertySignature.propertyName}',`; 94 } 95 } 96 return propertySignatureBody; 97} 98