From 4dfd327473e2586154e275ddbc0eebf3e7068080 Mon Sep 17 00:00:00 2001 From: "kochelmonster@github.com" Date: Fri, 18 Jul 2025 10:05:16 +0200 Subject: [PATCH] Fixed assignment bug in local scope: def count_100(index): for j in range(100): index = index + 1 return index Generated wrong to: export var count_100 = function (index) { for (var j = 0; j < 100; j++) { var index = index + 1; // var is wrong } return index; }; ------ def count_101(index): idx = index for j in range(101): idx = idx + 1 return idx Generated wrong to: export var count_101 = function (index) { var idx = index; for (var j = 0; j < 101; j++) { var idx = idx + 1; // var is wrong } return idx; }; --- transcrypt/modules/org/transcrypt/compiler.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/transcrypt/modules/org/transcrypt/compiler.py b/transcrypt/modules/org/transcrypt/compiler.py index de6b155d..66ed9e98 100644 --- a/transcrypt/modules/org/transcrypt/compiler.py +++ b/transcrypt/modules/org/transcrypt/compiler.py @@ -767,6 +767,7 @@ def inscope (self, node): self.scopes.append (utils.Any ( node = node, nonlocals = set (), + locals_ = set (), containsYield = False )) @@ -1161,15 +1162,17 @@ def emitPathIndices (): self.emit (')') else: if type (target) == ast.Name: - if type (self.getScope () .node) == ast.ClassDef and target.id != self.getTemp ('left'): + scope = self.getScope () + if type (scope.node) == ast.ClassDef and target.id != self.getTemp ('left'): self.emit ('{}.'.format ('.'.join ([scope.node.name for scope in self.getAdjacentClassScopes ()]))) # The target is a class attribute - elif target.id in self.getScope () .nonlocals: + elif target.id in scope .nonlocals or target.id in scope.locals_: pass else: - if type (self.getScope () .node) == ast.Module: # Redundant but regular + if type (scope.node) == ast.Module: # Redundant but regular if hasattr (node, 'parentNode') and type (node.parentNode) == ast.Module and not target.id in self.allOwnNames: self.emit ('export ') self.emit ('var ') + scope.locals_ .add (target.id) # Add to locals, so it can be used in the function body self.visit (target) self.emit (' = ') self.visit (value) @@ -2593,6 +2596,7 @@ def visit_AsyncFunctionDef (self, node): def visit_FunctionDef (self, node, anAsync = False): def emitScopedBody (): self.inscope (node) + self.getScope () .locals_.update(a.arg for a in node.args.args) self.emitBody (node.body) self.dedent () if self.getScope (ast.AsyncFunctionDef if anAsync else ast.FunctionDef) .containsYield: