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 ts from 'typescript'; 17 18import { 19 ELMTID, 20 FINALIZE_CONSTRUCTION 21} from './pre_define'; 22 23function createParameterDeclaration(name: string): ts.ParameterDeclaration { 24 let initializer: ts.Expression; 25 if (name === ELMTID) { 26 initializer = ts.factory.createPrefixUnaryExpression( 27 ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral('1')); 28 } 29 return ts.factory.createParameterDeclaration(undefined, undefined, 30 ts.factory.createIdentifier(name), undefined, undefined, initializer); 31} 32 33function createFinalizeConstruction(freezeParamNode: ts.Expression): ts.Statement { 34 const params: ts.Expression[] = []; 35 if (freezeParamNode) { 36 params.push(freezeParamNode); 37 } 38 return ts.factory.createExpressionStatement(ts.factory.createCallExpression( 39 ts.factory.createPropertyAccessExpression( 40 ts.factory.createThis(), 41 ts.factory.createIdentifier(FINALIZE_CONSTRUCTION) 42 ), 43 undefined, 44 params 45 )); 46} 47 48function createImportNodeForModuleInfo(): ts.ImportDeclaration { 49 return ts.factory.createImportDeclaration( 50 undefined, ts.factory.createImportClause(false, undefined, 51 ts.factory.createNamedImports([ts.factory.createImportSpecifier( 52 false, undefined, ts.factory.createIdentifier('__MODULE_NAME__') 53 ), ts.factory.createImportSpecifier(false, undefined, 54 ts.factory.createIdentifier('__BUNDLE_NAME__'))]) 55 ), ts.factory.createStringLiteral('ModuleInfo'), undefined 56 ); 57} 58 59export default { 60 createParameterDeclaration: createParameterDeclaration, 61 createFinalizeConstruction: createFinalizeConstruction, 62 createImportNodeForModuleInfo: createImportNodeForModuleInfo 63}; 64