Skip to content

Commit

Permalink
support for parameter destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaswelter committed Sep 28, 2020
1 parent 9b7d707 commit 45254d6
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,34 @@
import json
import copy

def unpackParam(param):
if param.type == 'Identifier':
return param.name

elif param.type == 'AssignmentPattern':
return unpackParam(param.left)

elif param.type == 'ArrayPattern':
return '[' + ', '.join(map(unpackParam, param.elements)) + ']'

elif param.type == 'ObjectPattern':
args = []
for prop in param.properties:
if prop.type == 'Property':
args.append(unpackParam(prop))

return '{' + ', '.join(args) + '}'

elif param.type == 'RestElement':
return '...' + unpackParam(param.argument)

return ''

def formatFunction(moduleName, name, params):
args = []
if params:
for param in params:
if param.type == 'Identifier':
args.append(param.name)

if param.type == 'AssignmentPattern' and param.left.type == 'Identifier':
args.append(param.left.name)
args.append(unpackParam(param))

suggestion = '{}({})\t{}'.format(name, ', '.join(args), moduleName)

Expand Down

0 comments on commit 45254d6

Please sign in to comment.