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 */ 15import ts from 'typescript'; 16 17export class DecoratorInfo { 18 expression: string = ''; //修饰符名称 19 expressionArguments: string[] | undefined = undefined; //修饰符传参,默认为undefined 20 21 constructor(decorator: ts.Decorator) { 22 const expression: ts.LeftHandSideExpression = decorator.expression; 23 if (ts.isCallExpression(expression)) { 24 this.setExpression(expression.expression.getText()); 25 const args: ts.NodeArray<ts.Expression> = expression.arguments; 26 args.forEach((arg: ts.Expression) => { 27 this.addExpressionArguments([arg.getText()]); 28 }); 29 } 30 if (ts.isIdentifier(expression)) { 31 this.setExpression(expression.getText()); 32 } 33 } 34 35 setExpression(expression: string): DecoratorInfo { 36 this.expression = expression; 37 return this; 38 } 39 40 getExpression(): string { 41 return this.expression; 42 } 43 44 setExpressionArguments(expressionArguments: string[]): DecoratorInfo { 45 this.expressionArguments = expressionArguments; 46 return this; 47 } 48 49 addExpressionArguments(expressionArguments: string[]): DecoratorInfo { 50 if (!this.expressionArguments) { 51 this.expressionArguments = []; 52 } 53 this.expressionArguments.push(...expressionArguments); 54 return this; 55 } 56 57 getExpressionArguments(): string[] | undefined { 58 return this.expressionArguments; 59 } 60} 61