Commits

Ben Firshman committed 5adea68

Correct assignment with var

  • Participants
  • Parent commits bd40bb4

Comments (0)

Files changed (2)

File kugelblitz/tests/__init__.py

             lambda: 1
             """,
             """
-            function() {
+            function () {
                 return 1;
             };
             """
             lambda a: a + 1
             """,
             """
-            function(a) {
+            function (a) {
                 return (a + 1);
             };
             """
             a = x if y else z
             """,
             """
-            a = y ? x : z;
+            var a = y ? x : z;
             """
         )
     
             """,
         )
     
+    def test_assign(self):
+        self.assertCompilesTo('x = 1', 'var x = 1;')
+        self.assertCompilesTo('this.x = 1', 'this.x = 1;')
+    
     def test_tuple_assignment(self):
         self.assertCompilesTo(
             """
             x, y = 1, 2
             """,
             """
-            x = 1;
-            y = 2;
+            var x = 1;
+            var y = 2;
             """,
         )
     
             x = y = 1
             """,
             """
-            x = 1;
-            y = 1;
+            var x = 1;
+            var y = 1;
             """,
         )
     
             y = x[1:2]
             """,
             """
-            x = [1, 2, 3];
-            y = x.slice(1, 2);
+            var x = [1, 2, 3];
+            var y = x.slice(1, 2);
             """,
         )
     

File kugelblitz/translator/__init__.py

         ast.Lambda: translate_lambda,
         ast.IfExp: translate_if_exp,
         ast.Dict: None,
-        # ast.Set: None,
+        # ast.Set: None, (not supported on Python 2.5)
         ast.ListComp: None,
-        # ast.SetComp: None,
-        # ast.DictComp: None,
+        # ast.SetComp: None, (not supported on Python 2.5)
+        # ast.DictComp: None, (not supported on Python 2.5)
         ast.GeneratorExp: None,
         ast.Yield: None,
         ast.Compare: translate_compare,
 def translate_delete(node):
     return ';\n'.join('delete %s' % translate(n) for n in node.targets)
 
+def translate_single_assign(target, value):
+    if isinstance(target, ast.Name):
+        return "var %(target)s = %(value)s" % {
+            'target': translate(target),
+            'value': translate(value),
+        }
+    else:
+        return "%(target)s = %(value)s" % {
+            'target': translate(target),
+            'value': translate(value),
+        }
+    
 def translate_assign(node):
     # For each target...
     statements = []
                 if len(target.elts) != len(node.value.elts):
                     raise CompileError("Assigning one tuple to another of different length.")
                 for t, v in zip(target.elts, node.value.elts):
-                    statements.append("%(target)s = %(value)s" % {
-                        'value': translate(v),
-                        'target': translate(t),
-                    })
+                    statements.append(translate_single_assign(t, v))
             # No? Raise an error for now.
             else:
                 raise CompileError("Assigning a non-tuple to a tuple.")
         else:
-            statements.append("%(target)s = %(value)s" % {
-                'value': translate(node.value),
-                'target': translate(target),
-            })
+            statements.append(translate_single_assign(target, node.value))
     return ";\n".join(statements)
 
 def translate_aug_assign(node):
     }
 
 def translate_lambda(node):
-    return "function(%(args_def)s) {\nreturn %(body_def)s\n}" % {
+    return "function (%(args_def)s) {\nreturn %(body_def)s\n}" % {
         'args_def': ", ".join([arg.id for arg in node.args.args]),
         'body_def': translate_body([node.body]),
     }