UNPKG

@ts-ast-parser/core

Version:

Reflects a simplified version of the TypeScript AST for generating documentation

142 lines 4.1 kB
import { DeclarationKind } from '../models/declaration-kind.js'; import { tryAddProperty } from '../utils/try-add-property.js'; import { EnumMemberNode } from './enum-member-node.js'; import { getNamespace } from '../utils/namespace.js'; import { RootNodeType } from '../models/node.js'; import { CommentNode } from './comment-node.js'; /** * Represents the reflected node of an enumerable declaration */ export class EnumNode { constructor(node, context) { Object.defineProperty(this, "_node", { 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._context = context; this._jsDoc = new CommentNode(this._node); } /** * The reflected node kind * * @returns A declaration kind node */ getNodeType() { return RootNodeType.Declaration; } /** * The reflected declaration kind * * @returns An Enum kind node */ getKind() { return DeclarationKind.Enum; } /** * The original TypeScript node * * @returns The TypeScript AST node related to this reflected node */ getTSNode() { return this._node; } /** * The context includes useful APIs that are shared across * all the reflected symbols. * * Some APIs include the parsed configuration options, the * system interface, the type checker * * @returns The analyser context */ getContext() { return this._context; } /** * Gets the name of the enum declaration * * @returns The name of the enum declaration */ getName() { return this._node.name.getText() ?? ''; } /** * The line position where the node is defined * * @returns The start line position number */ getLine() { return this._context.getLinePosition(this._node); } /** * The namespace where the enum declaration is defined. * * @returns The namespace name if found one, otherwise an empty string */ getNamespace() { return getNamespace(this._node); } /** * The reflected documentation comment * * @returns The JSDoc node */ getJSDoc() { return this._jsDoc; } /** * The reflected members of the enum declaration * * @returns The array of reflected enum members */ getMembers() { let defaultInitializer = 0; return this._node.members.map(member => { let value = member.initializer?.getText() ?? ''; if (value !== '') { const possibleNumericValue = parseInt(value); if (!isNaN(possibleNumericValue)) { defaultInitializer = possibleNumericValue + 1; value = possibleNumericValue; } } else { value = defaultInitializer++; } const callback = () => new EnumMemberNode(member, value, this._context); return this._context.registerReflectedNode(member, callback); }); } /** * Serializes the reflected node * * @returns The reflected node as a serializable object */ serialize() { const tmpl = { kind: this.getKind(), name: this.getName(), line: this.getLine(), }; tryAddProperty(tmpl, 'namespace', this.getNamespace()); tryAddProperty(tmpl, 'members', this.getMembers().map(member => member.serialize())); tryAddProperty(tmpl, 'jsDoc', this.getJSDoc().serialize()); return tmpl; } } //# sourceMappingURL=enum-node.js.map