Skip to content

Commit 99e6575

Browse files
committed
Add Javascript undeclared functions
1 parent 848d66d commit 99e6575

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/codetext/parser/javascript_parser.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ def get_comment_node(function_node):
4848

4949
@staticmethod
5050
def get_function_list(node):
51-
function_types = ['function_declaration', 'function', 'method_definition', 'generator_function_declaration']
51+
function_types = ['function_declaration',
52+
'function',
53+
'method_definition',
54+
'generator_function_declaration',
55+
'arrow_function',
56+
'generator_function']
5257
res = get_node_by_kind(node, function_types)
5358
for node in res[:]:
5459
if not node.children:
@@ -87,6 +92,16 @@ def get_function_metadata(function_node, blob: str=None) -> Dict[str, str]:
8792
return_statement = get_node_by_kind(function_node, ['return_statement'])
8893
if len(return_statement) > 0:
8994
metadata['return_type'] = '<not_specific>'
95+
96+
if function_node.type in ["function",
97+
"arrow_function",
98+
"generator_function"]:
99+
# function inside object property or variable declarator
100+
identifier = function_node.prev_named_sibling
101+
if identifier:
102+
if identifier.type in ["identifier"]:
103+
metadata["identifier"] = identifier.text.decode()
104+
90105
return metadata
91106

92107
@staticmethod

tests/test_parser/test_javascript.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class Car {
7676
def test_get_function_metadata(self):
7777
root = self.root_node
7878

79-
function = JavascriptParser.get_function_list(root)[1]
80-
metadata = JavascriptParser.get_function_metadata(function)
79+
_function = JavascriptParser.get_function_list(root)[1]
80+
metadata = JavascriptParser.get_function_metadata(_function)
8181

8282
for key in ['identifier', 'parameters', 'return_type']:
8383
self.assertTrue(key in metadata.keys())
@@ -109,6 +109,57 @@ def test_get_class_metadata(self):
109109
def test_extract_docstring(self):
110110
pass
111111

112+
113+
def test_metadata_with_arrow_function(self):
114+
code_sample = '''
115+
export const parseModel = async (mesh) =>
116+
new Promise((resolve) => {
117+
exporter.parse(
118+
mesh,
119+
(gltf) => {
120+
const blob = new Blob([gltf], { type: "application/octet-stream" });
121+
resolve(blob);
122+
return blob;
123+
},
124+
(error) => {
125+
console.log(error);
126+
return error;
127+
128+
}
129+
);
130+
});
131+
'''
132+
root = parse_code(code_sample, 'javascript').root_node
133+
fn = JavascriptParser.get_function_list(root)[0]
134+
metadata = JavascriptParser.get_function_metadata(fn)
135+
136+
identifier = metadata['identifier']
137+
self.assertEqual(identifier, 'parseModel')
138+
139+
def test_metadata_with_undecleared_functions(self):
140+
code_sample = """
141+
const asyncFunctionExpression = async function() {
142+
// async function expression definition
143+
return a
144+
};
145+
146+
const generatorFunctionExpression = function*() {
147+
// generator function expression definition
148+
return b
149+
};
150+
"""
151+
root = parse_code(code_sample, 'javascript').root_node
152+
fn1, fn2 = JavascriptParser.get_function_list(root)
153+
154+
self.assertEqual(fn1.type, 'function')
155+
self.assertEqual(fn2.type, 'generator_function')
156+
157+
metadata1 = JavascriptParser.get_function_metadata(fn1)
158+
metadata2 = JavascriptParser.get_function_metadata(fn2)
159+
160+
self.assertEqual(metadata1['identifier'], 'asyncFunctionExpression')
161+
self.assertEqual(metadata2['identifier'], 'generatorFunctionExpression')
162+
112163

113164
if __name__ == '__main__':
114165
unittest.main()

0 commit comments

Comments
 (0)