@ts-ast-parser/core
Version:
Reflects a simplified version of the TypeScript AST for generating documentation
116 lines • 3.91 kB
JavaScript
import { createType, createTypeFromDeclaration } from '../factories/create-type.js';
import { resolveExpression } from '../utils/resolve-expression.js';
import { BindingElementNode } from './binding-element-node.js';
import { tryAddProperty } from '../utils/try-add-property.js';
import { getDecorators } from '../utils/decorator.js';
import { DecoratorNode } from './decorator-node.js';
import { CommentNode } from './comment-node.js';
import ts from 'typescript';
/**
* Represents the reflected node of a parameter declaration
*/
export class ParameterNode {
constructor(node, symbol, context) {
Object.defineProperty(this, "_node", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_symbol", {
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._symbol = symbol;
this._context = context;
this._jsDoc = new CommentNode(this._node);
}
getName() {
if (this.isNamed()) {
return '__namedParameter';
}
if (this._symbol) {
return this._symbol.getName() ?? '';
}
return this._node.name.getText() ?? '';
}
getTSNode() {
return this._node;
}
getContext() {
return this._context;
}
getLine() {
return this._context.getLinePosition(this._node);
}
getType() {
const checker = this._context.getTypeChecker();
const type = this._symbol ? checker.getTypeOfSymbolAtLocation(this._symbol, this._node) : null;
return type ? createType(type, this._context) : createTypeFromDeclaration(this._node, this._context);
}
getDefault() {
return resolveExpression(this._node.initializer, this._context);
}
getDecorators() {
return getDecorators(this._node).map(d => new DecoratorNode(d, this._context));
}
getNamedElements() {
if (!this.isNamed()) {
return [];
}
const bindings = this._node.name.elements ?? [];
const result = [];
for (const binding of bindings) {
result.push(new BindingElementNode(binding, this._context));
}
return result;
}
getJSDoc() {
return this._jsDoc;
}
isNamed() {
return ts.isObjectBindingPattern(this._node.name);
}
isRest() {
return !!(this._node.dotDotDotToken && this._node.type?.kind === ts.SyntaxKind.ArrayType);
}
isOptional() {
return !!this._context.getTypeChecker().isOptionalParameter(this._node);
}
/**
* Serializes the reflected node
*
* @returns The reflected node as a serializable object
*/
serialize() {
const tmpl = {
name: this.getName(),
type: this.getType().serialize(),
line: this.getLine(),
};
tryAddProperty(tmpl, 'decorators', this.getDecorators().map(d => d.serialize()));
tryAddProperty(tmpl, 'jsDoc', this.getJSDoc().serialize());
tryAddProperty(tmpl, 'optional', this.isOptional());
tryAddProperty(tmpl, 'rest', this.isRest());
tryAddProperty(tmpl, 'named', this.isNamed());
tryAddProperty(tmpl, 'default', this.getDefault());
tryAddProperty(tmpl, 'elements', this.getNamedElements().map(e => e.serialize()));
return tmpl;
}
}
//# sourceMappingURL=parameter-node.js.map