Skip to content

Commit

Permalink
feat(javascript): optimise optional param parsinga and generator func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
kkoomen committed Oct 2, 2020
1 parent f7bfb09 commit 6ed3f90
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 216 deletions.
4 changes: 2 additions & 2 deletions ftplugin/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ call doge#buffer#register_doc_standard('jsdoc', [
\ ],
\ },
\ {
\ 'node_types': ['arrow_function', 'function', 'function_declaration', 'function_signature', 'method_definition', 'generator_function_declaration'],
\ 'node_types': ['arrow_function', 'function', 'function_declaration', 'function_signature', 'method_definition', 'generator_function', 'generator_function_declaration'],
\ 'parameters': {
\ 'format': '@param {{type|!type}} %(default|[)%{name|!name}%(default|])% - !description',
\ 'format': '@param {{type|!type}} %(optional|[)%{name|!name}%(optional|])% - !description',
\ },
\ 'typeParameters': {
\ 'format': '@template {name|!name} - !description',
Expand Down
76 changes: 43 additions & 33 deletions parsers/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,17 @@ traverse(tree.rootNode, lineNumber);
// METHODS
// =============================================================================

function parserHandler(parser, node, result) {
parser(node, result);
console.log(JSON.stringify(result));
done = true;
}

function traverse(node, lineNumber) {
if (node.startPosition.row === lineNumber && nodeTypes.includes(node.type) && done === false) {
switch (node.type) {
case NodeType.ARROW_FUNCTION:
case NodeType.FUNCTION:
case NodeType.FUNCTION_DECLARATION:
case NodeType.FUNCTION_SIGNATURE:
case NodeType.METHOD_DEFINITION:
case NodeType.GENERATOR_FUNCTION_DECLARATION: {
var result = {
visibility: null,
static: false,
generator: false,
async: false,
name: null,
typeParameters: [],
parameters: [],
returnType: null,
};
parseFunction(node, result);
console.log(JSON.stringify(result));
done = true;
break;
}

case NodeType.MEMBER_EXPRESSION: {
var result = {
const result = {
functionName: null,
propertyName: null,
generator: false,
Expand All @@ -68,23 +52,43 @@ function traverse(node, lineNumber) {
parameters: [],
returnType: null,
};
parsePrototypeFunction(node, result);
console.log(JSON.stringify(result));
done = true;
const prototypeIdentifier = node.child(0).children.pop();
if (prototypeIdentifier && prototypeIdentifier.text === 'prototype') {
parserHandler(parsePrototypeFunction, node, result);
}
break;
}

case NodeType.CLASS:
case NodeType.CLASS_DECLARATION: {
var result = {
const result = {
name: null,
typeParameters: [],
parentName: null,
interfaceName: null,
};
parseClass(node, result);
console.log(JSON.stringify(result));
done = true;
parserHandler(parseClass, node, result);
break;
}

case NodeType.ARROW_FUNCTION:
case NodeType.FUNCTION:
case NodeType.FUNCTION_DECLARATION:
case NodeType.FUNCTION_SIGNATURE:
case NodeType.METHOD_DEFINITION:
case NodeType.GENERATOR_FUNCTION:
case NodeType.GENERATOR_FUNCTION_DECLARATION: {
const result = {
visibility: null,
static: false,
generator: false,
async: false,
name: null,
typeParameters: [],
parameters: [],
returnType: null,
};
parserHandler(parseFunction, node, result);
break;
}

Expand Down Expand Up @@ -116,7 +120,7 @@ function parseClass(node, result) {
.children
.filter((n) => n.type === 'type_parameter')
.forEach((cn) => {
var typeparam = { name: null, default: null };
const typeparam = { name: null, default: null };

cn.children.forEach((tpn) => {
if (tpn.type === 'type_identifier') {
Expand Down Expand Up @@ -243,7 +247,7 @@ function parseFunction(node, result) {
.children
.filter((n) => n.type === 'type_parameter')
.forEach((cn) => {
var typeparam = { name: null, default: null };
const typeparam = { name: null, default: null };

cn.children.forEach((tpn) => {
if (tpn.type === 'type_identifier') {
Expand All @@ -269,7 +273,12 @@ function parseFunction(node, result) {
.children
.filter((cn) => ['required_parameter', 'rest_parameter', 'optional_parameter'].includes(cn.type))
.forEach((cn) => {
const param = { name: null, type: null, default: null };
const param = {
name: null,
type: null,
default: null,
optional: cn.type === 'optional_parameter',
};

cn.children.forEach((pn) => {
if (pn.type === 'identifier') {
Expand All @@ -282,6 +291,7 @@ function parseFunction(node, result) {

if (pn.previousSibling && pn.previousSibling.type === '=') {
param.default = pn.text;
param.optional = true;
}
});

Expand Down
Loading

0 comments on commit 6ed3f90

Please sign in to comment.