Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ function fromEntries (iterable) {
.reduce((obj, { 0: key, 1: val }) => Object.assign(obj, { [key]: val }), {})
}

// actual type names
// VoidKeyword -> undefined
const typeNameFromKind = fromEntries([
[SK.TypeLiteral, 'object'],
[SK.TypeReference, 'ref'],
[SK.FalseKeyword, 'false'],
[SK.TrueKeyword, 'true'],
[SK.NullKeyword, 'null'],
[SK.AnyKeyword, 'any'],
[SK.BooleanKeyword, 'boolean'],
[SK.NumberKeyword, 'number'],
[SK.ObjectKeyword, 'object'],
[SK.StringKeyword, 'string'],
[SK.UndefinedKeyword, 'undefined'],
[SK.BigIntKeyword, 'bigint'],
]);

module.exports = class Parser extends EventEmitter {
constructor() {
super();
Expand Down Expand Up @@ -193,34 +210,28 @@ module.exports = class Parser extends EventEmitter {
_readNode(n) {
let name = n.name && n.name.escapedText ? n.name.escapedText : null;

// Get type
// Get type: property type or method return type
const optional = Boolean(n.questionToken);
let type = fromEntries([
[SK.TypeLiteral, 'object'],
[SK.TypeReference, 'ref'],
[SK.FalseKeyword, 'false'],
[SK.TrueKeyword, 'true'],
[SK.NullKeyword, 'null'],
[SK.AnyKeyword, 'any'],
[SK.BooleanKeyword, 'boolean'],
[SK.NumberKeyword, 'number'],
[SK.ObjectKeyword, 'object'],
[SK.StringKeyword, 'string'],
[SK.UndefinedKeyword, 'undefined'],
[SK.BigIntKeyword, 'bigint'],
])[n.type.kind] || undefined;
let type = typeNameFromKind[n.type.kind];

if (type === 'ref') {
type = n.type.typeName.escapedText;
}

// get method parameters
const parameters = n.parameters && n.parameters.map(pn => ({
name: pn.name.escapedText,
type: typeNameFromKind[pn.type.kind],
}));

let levelData = {};
if (name) {
this._currentPath.push(name);
levelData = {
path: this._getCurrentPath(),
name,
type,
parameters,
};
this.emit('level-up', levelData);
}
Expand Down