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, softwarecheck{ } 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 16 17import ts from 'typescript'; 18import { ErrorMessage, ErrorTagFormat } from '../../../typedef/checker/result_type'; 19import { ApiInfo, ApiType, MethodInfo, ParamInfo, PropertyInfo } from '../../../typedef/parser/ApiInfoDefination'; 20import { CommonFunctions } from '../../../utils/checkUtils'; 21import { Comment } from '../../../typedef/parser/Comment'; 22 23export class AnonymousFunctionCheck { 24 static checkAnonymousFunction(singleApi: ApiInfo): ErrorTagFormat { 25 const anonymousFunctionCheckResult: ErrorTagFormat = { 26 state: true, 27 errorInfo: '', 28 }; 29 const jsDocInfo: Comment.JsDocInfo[] = singleApi.getJsDocInfos(); 30 const publishVersion: string = CommonFunctions.getSinceVersion(jsDocInfo[0].getSince()); 31 const apiVersionToBeVerified: string = CommonFunctions.getCheckApiVersion(); 32 if (publishVersion !== apiVersionToBeVerified) { return anonymousFunctionCheckResult }; 33 34 let IllegalType: ts.SyntaxKind[] = [ts.SyntaxKind.FunctionType, ts.SyntaxKind.TypeLiteral]; 35 let returnValueIsFunction: boolean = false; 36 let paramTypeIsFunction: boolean = false; 37 let typeValueIsFunction: boolean = false; 38 if (singleApi.getApiType() === ApiType.METHOD) { 39 returnValueIsFunction = IllegalType.includes((singleApi as MethodInfo).returnValueType); 40 paramTypeIsFunction = false; 41 const paramApiInfos: ParamInfo[] = (singleApi as MethodInfo).getParams(); 42 paramApiInfos.forEach((paramApiInfo: ParamInfo) => { 43 paramTypeIsFunction = IllegalType.includes(paramApiInfo.getParamType()); 44 }); 45 } else if (singleApi.getApiType() === ApiType.PROPERTY) { 46 typeValueIsFunction = IllegalType.includes((singleApi as PropertyInfo).typeKind); 47 } 48 if (returnValueIsFunction || paramTypeIsFunction || typeValueIsFunction) { 49 anonymousFunctionCheckResult.state = false; 50 anonymousFunctionCheckResult.errorInfo = ErrorMessage.ERROR_ANONYMOUS_FUNCTION; 51 } 52 return anonymousFunctionCheckResult; 53 } 54}