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 23import { 24 IFileLog, 25 LogInfo 26} from './utils'; 27 28class FileLog implements IFileLog { 29 private _sourceFile: ts.SourceFile | undefined; 30 private _errors: LogInfo[] = []; 31 32 public get sourceFile(): ts.SourceFile | undefined { 33 return this._sourceFile; 34 } 35 36 public set sourceFile(newValue: ts.SourceFile) { 37 this._sourceFile = newValue; 38 } 39 40 public get errors(): LogInfo[] { 41 return this._errors; 42 } 43 44 public set errors(newValue: LogInfo[]) { 45 this._errors = newValue; 46 } 47 48 public cleanUp(): void { 49 this._sourceFile = undefined; 50 this._errors = []; 51 } 52} 53 54 55function createParameterDeclaration(name: string): ts.ParameterDeclaration { 56 let initializer: ts.Expression; 57 if (name === ELMTID) { 58 initializer = ts.factory.createPrefixUnaryExpression( 59 ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral('1')); 60 } 61 return ts.factory.createParameterDeclaration(undefined, undefined, 62 ts.factory.createIdentifier(name), undefined, undefined, initializer); 63} 64 65function createFinalizeConstruction(freezeParamNode: ts.Expression): ts.Statement { 66 const params: ts.Expression[] = []; 67 if (freezeParamNode) { 68 params.push(freezeParamNode); 69 } 70 return ts.factory.createExpressionStatement(ts.factory.createCallExpression( 71 ts.factory.createPropertyAccessExpression( 72 ts.factory.createThis(), 73 ts.factory.createIdentifier(FINALIZE_CONSTRUCTION) 74 ), 75 undefined, 76 params 77 )); 78} 79 80function createImportNodeForModuleInfo(): ts.ImportDeclaration { 81 return ts.factory.createImportDeclaration( 82 undefined, ts.factory.createImportClause(false, undefined, 83 ts.factory.createNamedImports([ts.factory.createImportSpecifier( 84 false, undefined, ts.factory.createIdentifier('__MODULE_NAME__') 85 ), ts.factory.createImportSpecifier(false, undefined, 86 ts.factory.createIdentifier('__BUNDLE_NAME__'))]) 87 ), ts.factory.createStringLiteral('ModuleInfo'), undefined 88 ); 89} 90 91export default { 92 FileLog: FileLog, 93 createParameterDeclaration: createParameterDeclaration, 94 createFinalizeConstruction: createFinalizeConstruction, 95 createImportNodeForModuleInfo: createImportNodeForModuleInfo 96}; 97