Ejemplo n.º 1
0
def _define_var(traverser, node):
    "Creates a local context variable"

    traverser._debug("VARIABLE_DECLARATION")
    traverser.debug_level += 1

    declarations = (node["declarations"] if "declarations" in node
                    else node["head"])

    kind = node.get("kind", "let")
    for declaration in declarations:

        # It could be deconstruction of variables :(
        if declaration["id"]["type"] == "ArrayPattern":

            vars = []
            for element in declaration["id"]["elements"]:
                # NOTE : Multi-level array destructuring sucks. Maybe implement
                # it someday if you're bored, but it's so rarely used and it's
                # so utterly complex, there's probably no need to ever code it
                # up.
                if element is None or element["type"] != "Identifier":
                    vars.append(None)
                    continue
                vars.append(element["name"])

            # The variables are not initialized
            if declaration["init"] is None:
                # Simple instantiation; no initialization
                for var in vars:
                    if not var:
                        continue
                    traverser._declare_variable(var, None)

            # The variables are declared inline
            elif declaration["init"]["type"] == "ArrayPattern":
                # TODO : Test to make sure len(values) == len(vars)
                for value in declaration["init"]["elements"]:
                    if vars[0]:
                        traverser._declare_variable(
                            vars[0], JSWrapper(traverser._traverse_node(value),
                                               traverser=traverser))
                    vars = vars[1:]  # Pop off the first value

            # It's being assigned by a JSArray (presumably)
            elif declaration["init"]["type"] == "ArrayExpression":

                assigner = traverser._traverse_node(declaration["init"])
                for value in assigner.value.elements:
                    if vars[0]:
                        traverser._declare_variable(vars[0], value)
                    vars = vars[1:]

        elif declaration["id"]["type"] == "ObjectPattern":

            init = traverser._traverse_node(declaration["init"])

            def _proc_objpattern(init_obj, properties):
                for prop in properties:
                    # Get the name of the init obj's member
                    if prop["key"]["type"] == "Literal":
                        prop_name = prop["key"]["value"]
                    elif prop["key"]["type"] == "Identifier":
                        prop_name = prop["key"]["name"]
                    else:
                        continue

                    if prop["value"]["type"] == "Identifier":
                        traverser._declare_variable(
                            prop["value"]["name"],
                            init_obj.get(traverser, prop_name))
                    elif prop["value"]["type"] == "ObjectPattern":
                        _proc_objpattern(init_obj.get(traverser, prop_name),
                                         prop["value"]["properties"])

            if init is not None:
                _proc_objpattern(init_obj=init,
                                 properties=declaration["id"]["properties"])

        else:
            var_name = declaration["id"]["name"]
            traverser._debug("NAME>>%s" % var_name)

            var_value = traverser._traverse_node(declaration["init"])
            traverser._debug("VALUE>>%s" % (var_value.output()
                                            if var_value is not None
                                            else "None"))

            if not isinstance(var_value, JSWrapper):
                var = JSWrapper(value=var_value,
                                const=kind == "const",
                                traverser=traverser)
            else:
                var = var_value
                var.const = kind == "const"

            traverser._declare_variable(var_name, var, type_=kind)

    if "body" in node:
        traverser._traverse_node(node["body"])

    traverser.debug_level -= 1

    # The "Declarations" branch contains custom elements.
    return True
Ejemplo n.º 2
0
def _define_var(traverser, node):
    'Creates a local context variable'

    traverser._debug('VARIABLE_DECLARATION')
    traverser.debug_level += 1

    declarations = (node['declarations']
                    if 'declarations' in node else node['head'])

    kind = node.get('kind', 'let')
    for declaration in declarations:

        # It could be deconstruction of variables :(
        if declaration['id']['type'] == 'ArrayPattern':

            vars = []
            for element in declaration['id']['elements']:
                # NOTE : Multi-level array destructuring sucks. Maybe implement
                # it someday if you're bored, but it's so rarely used and it's
                # so utterly complex, there's probably no need to ever code it
                # up.
                if element is None or element['type'] != 'Identifier':
                    vars.append(None)
                    continue
                vars.append(element['name'])

            # The variables are not initialized
            if declaration['init'] is None:
                # Simple instantiation; no initialization
                for var in vars:
                    if not var:
                        continue
                    traverser._declare_variable(var, None)

            # The variables are declared inline
            elif declaration['init']['type'] == 'ArrayPattern':
                # TODO : Test to make sure len(values) == len(vars)
                for value in declaration['init']['elements']:
                    if vars[0]:
                        traverser._declare_variable(
                            vars[0],
                            JSWrapper(traverser._traverse_node(value),
                                      traverser=traverser))
                    vars = vars[1:]  # Pop off the first value

            # It's being assigned by a JSArray (presumably)
            elif declaration['init']['type'] == 'ArrayExpression':

                assigner = traverser._traverse_node(declaration['init'])
                for value in assigner.value.elements:
                    if vars[0]:
                        traverser._declare_variable(vars[0], value)
                    vars = vars[1:]

        elif declaration['id']['type'] == 'ObjectPattern':

            init = traverser._traverse_node(declaration['init'])

            def _proc_objpattern(init_obj, properties):
                for prop in properties:
                    # Get the name of the init obj's member
                    if prop['key']['type'] == 'Literal':
                        prop_name = prop['key']['value']
                    elif prop['key']['type'] == 'Identifier':
                        prop_name = prop['key']['name']
                    else:
                        continue

                    if prop['value']['type'] == 'Identifier':
                        traverser._declare_variable(
                            prop['value']['name'],
                            init_obj.get(traverser, prop_name))
                    elif prop['value']['type'] == 'ObjectPattern':
                        _proc_objpattern(init_obj.get(traverser, prop_name),
                                         prop['value']['properties'])

            if init is not None:
                _proc_objpattern(init_obj=init,
                                 properties=declaration['id']['properties'])

        else:
            var_name = declaration['id']['name']
            traverser._debug('NAME>>%s' % var_name)

            var_value = traverser._traverse_node(declaration['init'])
            traverser._debug(
                'VALUE>>%s' %
                (var_value.output() if var_value is not None else 'None'))

            if not isinstance(var_value, JSWrapper):
                var = JSWrapper(value=var_value,
                                const=kind == 'const',
                                traverser=traverser)
            else:
                var = var_value
                var.const = kind == 'const'

            traverser._declare_variable(var_name, var, type_=kind)

    if 'body' in node:
        traverser._traverse_node(node['body'])

    traverser.debug_level -= 1

    # The "Declarations" branch contains custom elements.
    return True
Ejemplo n.º 3
0
def _define_var(traverser, node):
    'Creates a local context variable'

    traverser._debug('VARIABLE_DECLARATION')
    traverser.debug_level += 1

    declarations = (node['declarations'] if 'declarations' in node
                    else node['head'])

    kind = node.get('kind', 'let')
    for declaration in declarations:

        # It could be deconstruction of variables :(
        if declaration['id']['type'] == 'ArrayPattern':

            vars = []
            for element in declaration['id']['elements']:
                # NOTE : Multi-level array destructuring sucks. Maybe implement
                # it someday if you're bored, but it's so rarely used and it's
                # so utterly complex, there's probably no need to ever code it
                # up.
                if element is None or element['type'] != 'Identifier':
                    vars.append(None)
                    continue
                vars.append(element['name'])

            # The variables are not initialized
            if declaration['init'] is None:
                # Simple instantiation; no initialization
                for var in vars:
                    if not var:
                        continue
                    traverser._declare_variable(var, None)

            # The variables are declared inline
            elif declaration['init']['type'] == 'ArrayPattern':
                # TODO : Test to make sure len(values) == len(vars)
                for value in declaration['init']['elements']:
                    if vars[0]:
                        traverser._declare_variable(
                            vars[0], JSWrapper(traverser._traverse_node(value),
                                               traverser=traverser))
                    vars = vars[1:]  # Pop off the first value

            # It's being assigned by a JSArray (presumably)
            elif declaration['init']['type'] == 'ArrayExpression':

                assigner = traverser._traverse_node(declaration['init'])
                for value in assigner.value.elements:
                    if vars[0]:
                        traverser._declare_variable(vars[0], value)
                    vars = vars[1:]

        elif declaration['id']['type'] == 'ObjectPattern':

            init = traverser._traverse_node(declaration['init'])

            def _proc_objpattern(init_obj, properties):
                for prop in properties:
                    # Get the name of the init obj's member
                    if prop['key']['type'] == 'Literal':
                        prop_name = prop['key']['value']
                    elif prop['key']['type'] == 'Identifier':
                        prop_name = prop['key']['name']
                    else:
                        continue

                    if prop['value']['type'] == 'Identifier':
                        traverser._declare_variable(
                            prop['value']['name'],
                            init_obj.get(traverser, prop_name))
                    elif prop['value']['type'] == 'ObjectPattern':
                        _proc_objpattern(init_obj.get(traverser, prop_name),
                                         prop['value']['properties'])

            if init is not None:
                _proc_objpattern(init_obj=init,
                                 properties=declaration['id']['properties'])

        else:
            var_name = declaration['id']['name']
            traverser._debug('NAME>>%s' % var_name)

            var_value = traverser._traverse_node(declaration['init'])
            traverser._debug('VALUE>>%s' % (var_value.output()
                                            if var_value is not None
                                            else 'None'))

            if not isinstance(var_value, JSWrapper):
                var = JSWrapper(value=var_value,
                                const=kind == 'const',
                                traverser=traverser)
            else:
                var = var_value
                var.const = kind == 'const'

            traverser._declare_variable(var_name, var, type_=kind)

    if 'body' in node:
        traverser._traverse_node(node['body'])

    traverser.debug_level -= 1

    # The "Declarations" branch contains custom elements.
    return True