UNPKG

@taiga-ui/cdk

Version:

Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance

81 lines 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isServiceMethodCall = isServiceMethodCall; const ng_morph_1 = require("ng-morph"); /** * Checks if the given call expression is a method call on the given service. */ function isServiceMethodCall(call, serviceName, methodName) { const propAccess = call.getExpressionIfKind(ng_morph_1.SyntaxKind.PropertyAccessExpression); if ((propAccess === null || propAccess === void 0 ? void 0 : propAccess.getName()) !== methodName) { return false; } const expr = propAccess.getExpression(); // 1) inject(Service).method(...) if (ng_morph_1.Node.isCallExpression(expr) && isInjectCallWithService(expr, serviceName)) { return true; } // 2) this.service.method(...) if (ng_morph_1.Node.isPropertyAccessExpression(expr)) { const left = expr.getExpression(); if (left.getKind() === ng_morph_1.SyntaxKind.ThisKeyword) { const name = expr.getName(); return isIdentifierDeclaredAsInjectService(expr.getSourceFile(), name, serviceName); } return false; } // 3) service.method(...) return ng_morph_1.Node.isIdentifier(expr) ? isIdentifierDeclaredAsInjectService(expr.getSourceFile(), expr.getText(), serviceName) : false; } function isInjectCallWithService(call, serviceName) { const callee = call.getExpression(); // inject(...) if (!ng_morph_1.Node.isIdentifier(callee) || callee.getText() !== 'inject') { return false; } const args = call.getArguments(); if (args.length < 1) { return false; } // inject(Service) const first = args[0]; return ng_morph_1.Node.isIdentifier(first) && first.getText() === serviceName; } function isIdentifierDeclaredAsInjectService(sourceFile, name, serviceName) { // 1) const name = inject(Service) const vars = sourceFile.getVariableDeclarations().filter((v) => v.getName() === name); for (const v of vars) { const init = v.getInitializer(); if (init && ng_morph_1.Node.isCallExpression(init) && isInjectCallWithService(init, serviceName)) { return true; } } // 2) private name = inject(Service) const props = sourceFile .getDescendantsOfKind(ng_morph_1.SyntaxKind.PropertyDeclaration) .filter((p) => p.getName() === name); for (const p of props) { const init = p.getInitializer(); if (init && ng_morph_1.Node.isCallExpression(init) && isInjectCallWithService(init, serviceName)) { return true; } } // 3) constructor(private service: Service) {} const ctorParams = sourceFile .getDescendantsOfKind(ng_morph_1.SyntaxKind.Parameter) .filter((param) => param.getName() === name); for (const p of ctorParams) { const typeNode = p.getTypeNode(); if ((typeNode === null || typeNode === void 0 ? void 0 : typeNode.getText()) === serviceName) { return true; } } return false; } //# sourceMappingURL=is-service-method-call.js.map