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