MCPcopy Create free account
hub / github.com/scottrogowski/code2flow / process_assign

Function process_assign

code2flow/javascript.py:116–171  ·  view source on GitHub ↗

Given an element from the ast which is an assignment statement, return a Variable that points_to the type of object being assigned. The points_to is often a string but that is resolved later. :param element ast: :rtype: Variable

(element)

Source from the content-addressed store, hash-verified

114
115
116def process_assign(element):
117 """
118 Given an element from the ast which is an assignment statement, return a
119 Variable that points_to the type of object being assigned. The
120 points_to is often a string but that is resolved later.
121
122 :param element ast:
123 :rtype: Variable
124 """
125
126 if len(element['declarations']) > 1:
127 return []
128 target = element['declarations'][0]
129 assert target['type'] == 'VariableDeclarator'
130 if target['init'] is None:
131 return []
132
133 if target['init']['type'] == 'NewExpression':
134 token = target['id']['name']
135 call = get_call_from_func_element(target['init'])
136 if call:
137 return [Variable(token, call, lineno(element))]
138
139 # this block is for require (as in: import) expressions
140 if target['init']['type'] == 'CallExpression' \
141 and target['init']['callee'].get('name') == 'require':
142 import_src_str = target['init']['arguments'][0]['value']
143 if 'name' in target['id']:
144 imported_name = target['id']['name']
145 points_to_str = djoin(import_src_str, imported_name)
146 return [Variable(imported_name, points_to_str, lineno(element))]
147 ret = []
148 for prop in target['id'].get('properties', []):
149 imported_name = prop['key']['name']
150 points_to_str = djoin(import_src_str, imported_name)
151 ret.append(Variable(imported_name, points_to_str, lineno(element)))
152 return ret
153
154 # For the other type of import expressions
155 if target['init']['type'] == 'ImportExpression':
156 import_src_str = target['init']['source']['raw']
157 imported_name = target['id']['name']
158 points_to_str = djoin(import_src_str, imported_name)
159 return [Variable(imported_name, points_to_str, lineno(element))]
160
161 if target['init']['type'] == 'CallExpression':
162 if 'name' not in target['id']:
163 return []
164 call = get_call_from_func_element(target['init'])
165 if call:
166 return [Variable(target['id']['name'], call, lineno(element))]
167
168 if target['init']['type'] == 'ThisExpression':
169 assert set(target['init'].keys()) == {'start', 'end', 'loc', 'type'}
170 return []
171 return []
172
173

Callers 1

make_local_variablesFunction · 0.70

Calls 6

VariableClass · 0.85
djoinFunction · 0.85
setFunction · 0.85
keysMethod · 0.80
linenoFunction · 0.70

Tested by

no test coverage detected