@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
66 lines • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoerceComponentInput = CoerceComponentInput;
const ts_morph_1 = require("ts-morph");
const coerce_accessor_declaration_1 = require("../coerce-accessor-declaration");
const coerce_decorator_1 = require("../coerce-decorator");
const coerce_imports_1 = require("../coerce-imports");
const coerce_property_declaration_1 = require("../coerce-property-declaration");
const has_constructor_parameter_1 = require("../has-constructor-parameter");
const write_type_1 = require("../write-type");
const get_component_class_1 = require("./get-component-class");
function CoerceComponentInput(sourceFileOrClassDeclaration, name, type, { initializer, alias, isRequired, asSetAccessor } = {}) {
const classDeclaration = sourceFileOrClassDeclaration.isKind(ts_morph_1.SyntaxKind.ClassDeclaration) ? sourceFileOrClassDeclaration : (0, get_component_class_1.GetComponentClass)(sourceFileOrClassDeclaration);
const sourceFile = sourceFileOrClassDeclaration.isKind(ts_morph_1.SyntaxKind.SourceFile) ? sourceFileOrClassDeclaration : sourceFileOrClassDeclaration.getSourceFile();
if ((0, has_constructor_parameter_1.HasConstructorParameter)(classDeclaration, name, true)) {
throw new Error(`The component '${classDeclaration.getName()}' already has a constructor parameter that is a class member with the name '${name}'. Cannot add an input with the same name!`);
}
(0, coerce_imports_1.CoerceImports)(sourceFile, {
namedImports: ['Input'],
moduleSpecifier: '@angular/core',
});
const inputDecoratorStructure = {
name: 'Input',
arguments: [],
};
if (alias || isRequired) {
const inputArgument = {};
if (alias) {
inputArgument['alias'] = w => w.quote(alias);
}
if (isRequired) {
inputArgument['required'] = 'true';
}
inputDecoratorStructure.arguments = [ts_morph_1.Writers.object(inputArgument)];
}
let propertyNode;
if (asSetAccessor) {
propertyNode = (0, coerce_accessor_declaration_1.CoerceSetAccessorDeclaration)(classDeclaration, name, { statements: `this._${name} = ${name};` }).set({
scope: ts_morph_1.Scope.Public,
parameters: [
{
name: name,
type: (0, write_type_1.WriteType)(type, sourceFile),
},
],
});
(0, coerce_property_declaration_1.CoercePropertyDeclaration)(classDeclaration, '_' + name, { scope: ts_morph_1.Scope.Private }).set({
type: (0, write_type_1.WriteType)(type, sourceFile),
initializer: initializer,
hasQuestionToken: !initializer && !isRequired,
hasExclamationToken: !initializer && isRequired,
});
}
else {
propertyNode = (0, coerce_property_declaration_1.CoercePropertyDeclaration)(classDeclaration, name).set({
scope: ts_morph_1.Scope.Public,
type: (0, write_type_1.WriteType)(type, sourceFile),
initializer,
hasQuestionToken: !initializer && !isRequired,
hasExclamationToken: !initializer && isRequired,
});
}
(0, coerce_decorator_1.CoerceDecorator)(propertyNode, 'Input', inputDecoratorStructure);
return propertyNode;
}
//# sourceMappingURL=coerce-component-input.js.map