@rxap/ts-morph
Version:
Provides utilities for manipulating TypeScript code using the ts-morph library. It offers a fluent API to add, modify, and remove code elements such as classes, decorators, imports, and properties in both Angular and NestJS projects. This package simplifi
24 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoerceVariableDeclaration = CoerceVariableDeclaration;
const ts_morph_1 = require("ts-morph");
function CoerceVariableDeclaration(sourceFile, name, defaultDeclaration = {}, variableStatementStructure) {
var _a, _b;
const declaration = Object.assign(Object.assign({}, defaultDeclaration), { name });
let variableStatement = sourceFile.getVariableStatement(name);
if (!variableStatement) {
variableStatementStructure !== null && variableStatementStructure !== void 0 ? variableStatementStructure : (variableStatementStructure = {
isExported: true,
declarationKind: ts_morph_1.VariableDeclarationKind.Const,
});
(_a = variableStatementStructure.isExported) !== null && _a !== void 0 ? _a : (variableStatementStructure.isExported = true);
(_b = variableStatementStructure.declarationKind) !== null && _b !== void 0 ? _b : (variableStatementStructure.declarationKind = ts_morph_1.VariableDeclarationKind.Const);
variableStatement = sourceFile.addVariableStatement(Object.assign(Object.assign({}, variableStatementStructure), { declarations: [declaration] }));
}
let variableDeclaration = variableStatement.getDeclarations()[0];
if (!variableDeclaration) {
variableDeclaration = variableStatement.addDeclaration(declaration);
}
return variableDeclaration;
}
//# sourceMappingURL=coerce-variable-declaration.js.map