-
Notifications
You must be signed in to change notification settings - Fork 3
/
javascript_handler.py
222 lines (170 loc) · 8.76 KB
/
javascript_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# pylint: disable="import-error"
from class_info_builder_a import ClassInfoBuilderA
from class_info_builder_b import ClassInfoBuilderB
from class_director import ClassDirector
from esprima import tokenize, parseScript
from dot_formatter import DotFormatter
class JavascriptHandler():
"""Responsible for converting"""
def __init__(self, the_js: str, current_cmd: str):
self.js_code = the_js
self.current_cmd = current_cmd
# Azez method's
def extract_javascript_a(self):
""" 1 """
my_ast = parseScript(self.js_code)
# print(my_ast)
my_classes = []
overall_class_attributes = []
count = 0
for a_class in my_ast.body:
builder_a = ClassInfoBuilderA()
director = ClassDirector(builder_a)
current_input = {
"name":"", # Done
"attribute_types": [],
"attribute_values": [],
"method": { # done
"methods": [],
"values": [],
"params": {}
},
"parent": "", # Done
"associations": [] # Done
}
current_input["name"] = a_class.id.name
# my_classes.append(a_class.id.name)
my_methods = my_ast.body[count].body.body
# my_attributes = my_ast.body[count].body.body[count]
current_class_methods = []
current_class_method_return_values = []
current_class_method_params = {}
current_class_attributes = []
current_class_attribute_values = []
current_class_associations = []
count = count + 1
method_count = 0
if a_class.superClass != None:
current_input["parent"] = a_class.superClass.name
# print(current_class["class_parent"])
for method in my_methods:
current_method_value = ""
# if method.key.name == "constructor":
for e in method.value.body.body:
if method.key.name == "constructor":
if e.expression.left.object.type == "ThisExpression":
current_attribute = e.expression.left.property.name
current_attribute_value = e.expression.right.value
current_class_attributes.append(current_attribute)
current_class_attribute_values.append(
current_attribute_value)
is_var = e.type == "VariableDeclaration"
if is_var and e.declarations[0].init.type == "NewExpression":
current_input["associations"].append(
e.declarations[0].init.callee.name)
if e.expression != None:
if e.expression.right != None:
# print(e.expression.right)
if e.expression.right.type == "NewExpression":
# print(e.expression.right.callee.name)
current_input["associations"].append(
e.expression.right.callee.name)
current_input["attribute_types"] = current_class_attributes
current_input["attribute_values"] = current_class_attribute_values
current_input["associations"] = current_class_associations
overall_class_attributes.append(current_class_attributes)
current_class_methods.append(method.key.name)
# if method.value.params != []:
current_params = method.value.params
# print(current_params)
# empty_list = []
# if current_params != empty_list:
# if method.key.name != "constructor":
current_class_method_params[f'{method.key.name}'] = []
for p in current_params:
# param = p.name
# print(p.name)
# print(method.key.name)
current_class_method_params[f'{method.key.name}'].append(
p.name)
method_count = method_count + 1
for e in method.value.body.body:
if e.type == "ReturnStatement":
current_method_value = type(e.argument.value).__name__
if current_method_value == "":
current_method_value = "void"
# print(current_method_value)
current_class_method_return_values.append(current_method_value)
current_input["method"]["methods"] = current_class_methods
current_input["method"]["values"] = current_class_method_return_values
# print(current_class_method_params)
current_input["method"]["params"] = current_class_method_params
# overall_class_methods.append(current_class_methods)
director.construct(current_input)
# my_classes.append(current_class)
my_classes.append(builder_a.get_result())
self.js_code = my_classes
# Ethan method's
def extract_javascript_b(self):
js_ast = parseScript(self.js_code)
all_js_classes = []
for a_class in js_ast.body:
builder_b = ClassInfoBuilderB()
director = ClassDirector(builder_b)
current_input = {
"name":"", # Done
"attribute_types": [],
"attribute_values": [],
"method": [],
"parent": "", # Done
"associations": [] # Done
}
if a_class.type == "ClassDeclaration":
current_input["name"] = a_class.id.name
if a_class.superClass != None:
current_input["parent"] = a_class.superClass.name
for a_method in a_class.body.body:
new_method = {"name": "",
"parameters": [], "return_type": ""}
if a_method.type == "MethodDefinition":
new_method["name"] = a_method.key.name
for a_param in a_method.value.params:
new_method["parameters"].append(a_param.name)
for an_expression in a_method.value.body.body:
if an_expression.type == "ReturnStatement":
new_method["return_type"] = f' : {type(an_expression.argument.value).__name__}'
e_type = an_expression.type == "VariableDeclaration"
if e_type and an_expression.declarations[0].init.type == "NewExpression":
current_input["associations"].append(
an_expression.declarations[0].init.callee.name)
if an_expression.expression != None:
left_value = an_expression.expression.left
right_value = an_expression.expression.right
if a_method.key.name == "constructor": # Grab Attributes of constructor
if left_value.object.type == "ThisExpression":
current_input["attribute_values"].append(
left_value.property.name)
# Grabs attribute types - Gives empty string if no type value given
attribute_value = right_value.value
if isinstance(attribute_value, type(None)):
value_type = ""
else:
value_type = f' : {type(attribute_value).__name__}'
current_input["attribute_types"].append(
value_type)
if right_value != None and right_value.type == "NewExpression":
current_input["associations"].append(
right_value.callee.name)
current_input["method"].append(new_method)
director.construct(current_input)
all_js_classes.append(builder_b.get_result())
# all_js_classes.append(class_dict)
self.js_code = all_js_classes
# Shared method's
def create_puml(self):
my_dot_formatter = DotFormatter(self.js_code)
if self.current_cmd == "a":
my_dot_formatter.convert_to_dot_a()
else:
my_dot_formatter.convert_to_dot_b()
my_dot_formatter.handle_dot_file(self.current_cmd)