UNPKG

constatic

Version:

Constatic is a CLI for creating and managing modern TypeScript projects, providing an organized structure and features that streamline development.

37 lines (36 loc) 1.21 kB
// src/helpers/morph/functions.ts import { SyntaxKind } from "ts-morph"; async function modifyObjArg(props) { const { source, callname, index = 0, modify } = props; const callExpr = source.getFirstDescendant((node) => node.isKind(SyntaxKind.CallExpression) && node.getExpression().getText().endsWith(callname)); if (!callExpr) return false; const arg = callExpr.getArguments()[index]; if (!arg || !arg.isKind(SyntaxKind.ObjectLiteralExpression)) { return false; } modify(arg); await source.save(); return source.isSaved(); } async function addPropToFnObjArg(props) { const { project, file, callname, key, value, argIndex = 0 } = props; const sourceFile = project.addSourceFileAtPath(file); const callExpr = sourceFile.getFirstDescendant((node) => node.isKind(SyntaxKind.CallExpression) && node.getExpression().getText().endsWith(callname)); if (!callExpr) return false; const arg = callExpr.getArguments()[argIndex]; if (!arg || !arg.isKind(SyntaxKind.ObjectLiteralExpression)) { return false; } arg.addPropertyAssignment({ name: key, initializer: value }); await sourceFile.save(); return true; } export { modifyObjArg, addPropToFnObjArg };