@ts-ast-parser/core
Version:
Reflects a simplified version of the TypeScript AST for generating documentation
165 lines • 5.95 kB
JavaScript
import { ExpressionWithTypeArgumentsNode } from './expression-with-type-arguments-node.js';
import { DeclarationKind } from '../models/declaration-kind.js';
import { IndexSignatureNode } from './index-signature-node.js';
import { tryAddProperty } from '../utils/try-add-property.js';
import { TypeParameterNode } from './type-parameter-node.js';
import { getInstanceMembers } from '../utils/member.js';
import { getNamespace } from '../utils/namespace.js';
import { PropertyNode } from './property-node.js';
import { FunctionNode } from './function-node.js';
import { RootNodeType } from '../models/node.js';
import { CommentNode } from './comment-node.js';
import ts from 'typescript';
/**
* Represents the reflected node of an interface declaration
*/
export class InterfaceNode {
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, "_members", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(this, "_jsDoc", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_heritage", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
this._node = node;
this._context = context;
this._members = getInstanceMembers(this._node, this._context);
this._jsDoc = new CommentNode(this._node);
this._heritage = (this._node.heritageClauses ?? []).flatMap(h => {
return h.types.map(t => new ExpressionWithTypeArgumentsNode(t, this._context));
});
}
getName() {
return this._node.name.getText() ?? '';
}
getNodeType() {
return RootNodeType.Declaration;
}
getKind() {
return DeclarationKind.Interface;
}
getContext() {
return this._context;
}
getTSNode() {
return this._node;
}
getLine() {
return this._context.getLinePosition(this._node);
}
getIndexSignature() {
const checker = this._context.getTypeChecker();
const indexSymbol = this._context.getSymbol(this._node)?.members?.get('__index');
const decl = indexSymbol?.getDeclarations()?.[0];
if (!decl || !ts.isIndexSignatureDeclaration(decl)) {
return null;
}
const symbolWithContext = {
symbol: indexSymbol,
type: checker.getTypeOfSymbolAtLocation(indexSymbol, this._node),
};
const callback = () => new IndexSignatureNode(decl, symbolWithContext, this._context);
return this._context.registerReflectedNode(decl, callback);
}
getProperties() {
const result = [];
for (const member of this._members) {
const { symbol } = member;
const decl = symbol?.getDeclarations()?.[0];
if (!decl) {
continue;
}
const isPropertyMethod = ts.isPropertySignature(decl) &&
decl.type &&
ts.isFunctionTypeNode(decl.type);
if ((ts.isPropertySignature(decl) ||
ts.isGetAccessor(decl) ||
ts.isSetAccessor(decl)) && !isPropertyMethod) {
const reflectedNode = new PropertyNode(decl, member, this._context);
result.push(reflectedNode);
}
}
return result;
}
getPropertyWithName(name) {
return this.getProperties().find(m => m.getName() === name) ?? null;
}
getMethods() {
const result = [];
for (const member of this._members) {
const { symbol } = member;
const decl = symbol?.getDeclarations()?.[0];
if (!decl) {
continue;
}
const isPropertyMethod = ts.isPropertySignature(decl) &&
decl.type &&
ts.isFunctionTypeNode(decl.type);
if (ts.isMethodSignature(decl) || isPropertyMethod) {
const reflectedNode = new FunctionNode(decl, member, this._context);
result.push(reflectedNode);
}
}
return result;
}
getMethodWithName(name) {
return this.getMethods().find(m => m.getName() === name) ?? null;
}
getTypeParameters() {
return (this._node.typeParameters ?? []).map(tp => new TypeParameterNode(tp, this._context));
}
getNamespace() {
return getNamespace(this._node);
}
getJSDoc() {
return this._jsDoc;
}
getHeritage() {
return this._heritage;
}
/**
* Serializes the reflected node
*
* @returns The reflected node as a serializable object
*/
serialize() {
const tmpl = {
name: this.getName(),
kind: this.getKind(),
line: this.getLine(),
};
tryAddProperty(tmpl, 'heritage', this.getHeritage().map(h => h.serialize()));
tryAddProperty(tmpl, 'typeParameters', this.getTypeParameters().map(tp => tp.serialize()));
tryAddProperty(tmpl, 'jsDoc', this.getJSDoc().serialize());
tryAddProperty(tmpl, 'namespace', this.getNamespace());
tryAddProperty(tmpl, 'indexSignature', this.getIndexSignature()?.serialize());
tryAddProperty(tmpl, 'properties', this.getProperties().map(p => p.serialize()));
tryAddProperty(tmpl, 'methods', this.getMethods().map(m => m.serialize()));
return tmpl;
}
}
//# sourceMappingURL=interface-node.js.map