Skip to content

Commit

Permalink
feat(python): add default !name if exception name couldnt be parsed (#77
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kkoomen committed Oct 8, 2020
1 parent af1c1f2 commit dced707
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
10 changes: 8 additions & 2 deletions src/parsers/python.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,18 @@ export class PythonParserService

private parseExceptions(node: SyntaxNode): void {
if (node.type === 'raise_statement') {
const exceptionName = node.children
const exception: Record<string, any> = { name: null };

const name = node.children
.filter((n: SyntaxNode) => n.type === 'expression_list')
.shift()
?.children.shift()
?.children.shift()?.text;
this.result.exceptions.push({ name: exceptionName });
if (name) {
exception.name = name;
}

this.result.exceptions.push(exception);
}

if (node.childCount > 0) {
Expand Down
16 changes: 9 additions & 7 deletions test/filetypes/python/functions-doc-google.vader
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ Expect python (generated comment with Args and Returns tags):
Given python (function with exceptions being raised in the body):
def myFunc(p1 = 'string'):
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass

Do (trigger doge):
:let b:doge_doc_standard='google'\<CR>
Expand All @@ -115,11 +116,12 @@ Expect python (generated comment with :param and :raises tags):
p1 ([TODO:type], optional): [TODO:description]

Raises:
ValueError: [TODO:description]
[TODO:name]: [TODO:description]
Exception: [TODO:description]
"""
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass
16 changes: 9 additions & 7 deletions test/filetypes/python/functions-doc-numpy.vader
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ Expect python (generated comment with Parameters and Returns tag):
Given python (function with exceptions being raised in the body):
def myFunc(p1 = 'string'):
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass

Do (trigger doge):
:let b:doge_doc_standard='numpy'\<CR>
Expand All @@ -105,13 +106,14 @@ Expect python (generated comment with :param and :raises tags):

Raises
------
ValueError:
[TODO:name]:
[TODO:description]
Exception:
[TODO:description]
"""
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass
16 changes: 9 additions & 7 deletions test/filetypes/python/functions-doc-sphinx.vader
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ Expect python (generated comment with :param, :type, :return and :rtype tags):
Given python (function with exceptions being raised in the body):
def myFunc(p1 = 'string'):
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass

Do (trigger doge):
:let b:doge_doc_standard='sphinx'\<CR>
Expand All @@ -113,11 +114,12 @@ Expect python (generated comment with :param and :raises tags):

:param p1: [TODO:description], defaults to 'string'
:type p1: [TODO:type], optional
:raises ValueError: [TODO:description]
:raises [TODO:name]: [TODO:description]
:raises Exception: [TODO:description]
"""
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass
16 changes: 9 additions & 7 deletions test/filetypes/python/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ Expect python (generated comment with :param tags):
Given python (function with exceptions being raised in the body):
def myFunc(p1 = 'string'):
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass

Do (trigger doge):
\<C-d>
Expand All @@ -178,11 +179,12 @@ Expect python (generated comment with :param and :raises tags):
[TODO:description]

:param p1 [TODO:type]: [TODO:description]
:raises ValueError: [TODO:description]
:raises [TODO:name]: [TODO:description]
:raises Exception: [TODO:description]
"""
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
foo = ValueError()
raise foo
raise Exception()
except Exception as error:
print('Caught this error: ' + repr(error))
pass

0 comments on commit dced707

Please sign in to comment.