UNPKG

@ts-ast-parser/core

Version:

Reflects a simplified version of the TypeScript AST for generating documentation

104 lines 3.26 kB
import { tryAddProperty } from '../utils/try-add-property.js'; import { createType } from '../factories/create-type.js'; import { MemberKind } from '../models/member-kind.js'; import { ParameterNode } from './parameter-node.js'; import { CommentNode } from './comment-node.js'; /** * Represents the reflected node of an index signature in an interface or in a type literal. * For example: `{ [key: string]: number }` */ export class IndexSignatureNode { constructor(node, member, context) { Object.defineProperty(this, "_node", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_member", { 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, "_parameter", { 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._member = member; this._context = context; this._parameter = this._getParameter(); this._jsDoc = new CommentNode(this._node); } getName() { return this._parameter?.getName() ?? ''; } getContext() { return this._context; } getKind() { return MemberKind.IndexSignature; } getTSNode() { return this._node; } getJSDoc() { return this._jsDoc; } getLine() { return this._context.getLinePosition(this._node); } getType() { return createType(this._node.type, this._context); } getIndexType() { return this._parameter?.getType() ?? null; } isOptional() { return !!this._parameter?.isOptional(); } /** * Serializes the reflected node * * @returns The reflected node as a serializable object */ serialize() { const tmpl = { name: this.getName(), line: this.getLine(), kind: MemberKind.IndexSignature, type: this.getType().serialize(), }; tryAddProperty(tmpl, 'indexType', this.getIndexType()?.serialize()); tryAddProperty(tmpl, 'optional', this.isOptional()); tryAddProperty(tmpl, 'jsDoc', this.getJSDoc().serialize()); return tmpl; } _getParameter() { const callSignature = this._member.type?.getCallSignatures()?.[0]; const nodeParameters = this._node.parameters ?? []; const symbolParameters = callSignature?.parameters ?? []; const nodeParam = nodeParameters[0]; const symbolParam = symbolParameters[0] ?? null; if (!nodeParam) { return null; } return new ParameterNode(nodeParam, symbolParam, this._context); } } //# sourceMappingURL=index-signature-node.js.map