UNPKG

@ts-ast-parser/core

Version:

Reflects a simplified version of the TypeScript AST for generating documentation

100 lines 3.22 kB
import { createTypeFromDeclaration } from '../factories/create-type.js'; import { resolveExpression } from '../utils/resolve-expression.js'; import { DeclarationKind } from '../models/declaration-kind.js'; import { tryAddProperty } from '../utils/try-add-property.js'; import { getDecorators } from '../utils/decorator.js'; import { getNamespace } from '../utils/namespace.js'; import { DecoratorNode } from './decorator-node.js'; import { RootNodeType } from '../models/node.js'; import { CommentNode } from './comment-node.js'; /** * Represents the reflected node of a variable declaration */ export class VariableNode { constructor(node, declaration, context) { Object.defineProperty(this, "_node", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_declaration", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_context", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_jsDoc", { enumerable: true, configurable: true, writable: true, value: void 0 }); this._node = node; this._declaration = declaration; this._context = context; this._jsDoc = new CommentNode(this._node); } getContext() { return this._context; } getName() { return this._declaration.name.getText() ?? ''; } getTSNode() { return this._declaration; } getNodeType() { return RootNodeType.Declaration; } getKind() { return DeclarationKind.Variable; } getDecorators() { return getDecorators(this._node).map(d => new DecoratorNode(d, this._context)); } getLine() { return this._context.getLinePosition(this._node); } getType() { return createTypeFromDeclaration(this._declaration, this._context); } getValue() { const jsDocDefaultValue = this.getJSDoc().getTag('default')?.text; return jsDocDefaultValue ?? resolveExpression(this._declaration.initializer, this._context); } getNamespace() { return getNamespace(this._node); } getJSDoc() { return this._jsDoc; } /** * Serializes the reflected node * * @returns The reflected node as a serializable object */ serialize() { const defaultValue = this.getValue(); const tmpl = { name: this.getName(), kind: this.getKind(), line: this.getLine(), type: this.getType().serialize(), }; if (defaultValue !== '') { tmpl.default = defaultValue; } tryAddProperty(tmpl, 'jsDoc', this.getJSDoc().serialize()); tryAddProperty(tmpl, 'decorators', this.getDecorators().map(d => d.serialize())); tryAddProperty(tmpl, 'namespace', this.getNamespace()); return tmpl; } } //# sourceMappingURL=variable-node.js.map