/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import ts from 'typescript'; import { projectConfig } from '../../../main'; import { DECORATOR_SUFFIX } from '../../pre_define'; export function disableMockDecorator(node: ts.Decorator): boolean { if (!shouldDisableMockDecorator()) { return false; } let parent: ts.Node = node.parent; switch (parent.kind) { case ts.SyntaxKind.Parameter: { ts.factory.updateParameterDeclaration(parent, ts.getModifiers((parent)), (parent).dotDotDotToken, (parent).name, (parent).questionToken, (parent).type, (parent).initializer); break; } case ts.SyntaxKind.MethodDeclaration: { ts.factory.updateMethodDeclaration(parent, ts.getModifiers((parent)), (parent).asteriskToken, (parent).name, (parent).questionToken, (parent).typeParameters, (parent).parameters, (parent).type, (parent).body); break; } case ts.SyntaxKind.Constructor: { ts.factory.updateConstructorDeclaration(parent, ts.getModifiers((parent)), (parent).parameters, (parent).body); break; } case ts.SyntaxKind.GetAccessor: { ts.factory.updateGetAccessorDeclaration(parent, ts.getModifiers((parent)), (parent).name, (parent).parameters, (parent).type, (parent).body); break; } case ts.SyntaxKind.SetAccessor: { ts.factory.updateSetAccessorDeclaration(parent, ts.getModifiers((parent)), (parent).name, (parent).parameters, (parent).body); break; } case ts.SyntaxKind.PropertyDeclaration: { if ((parent).questionToken) { ts.factory.updatePropertyDeclaration(parent, ts.getModifiers((parent)), (parent).name, (parent).questionToken, (parent).type, (parent).initializer); } else if ((parent).exclamationToken) { ts.factory.updatePropertyDeclaration(parent, ts.getModifiers((parent)), (parent).name, (parent).exclamationToken, (parent).type, (parent).initializer); } else { ts.factory.updatePropertyDeclaration(parent, ts.getModifiers((parent)), (parent).name, undefined, (parent).type, (parent).initializer); } break; } case ts.SyntaxKind.ClassDeclaration: { ts.factory.updateClassDeclaration(parent, ts.getModifiers((parent)), (parent).name, (parent).typeParameters, (parent).heritageClauses, (parent).members); break; } default: { break; } } return true; } function removeMockDecorator(decs: readonly ts.Decorator[]): ts.Decorator[] { let res: ts.Decorator[]; for (let dec of decs) { if (!isMockDecorator(dec)) { res.push(dec); } } return res; } export function isMockDecorator(dec: ts.Decorator): boolean { let decObj = dec.expression; if (!ts.isIdentifier(decObj)) { return false; } if (projectConfig.mockParams) { let mockDecorator: string = projectConfig.mockParams.decorator.replace(DECORATOR_SUFFIX, ''); return ((decObj).escapedText.toString() === mockDecorator); } return false; } function shouldDisableMockDecorator(): boolean { // mock decorator only takes effect under preview mode, should be removed otherswise if (projectConfig.isPreview) { return false; } // mockParams = { // "decorator": "name of mock decorator", // "packageName": "name of mock package", // "etsSourceRootPath": "path of ets source root", // "mockConfigPath": "path of mock configuration file" // } return (projectConfig.mockParams && projectConfig.mockParams.decorator && projectConfig.mockParams.packageName) ? true : false; } export const ORIGIN_EXTENTION: string = '.origin';