Skip to content

Commit

Permalink
Merge pull request #219 from trullock/bug_216
Browse files Browse the repository at this point in the history
fixes #79 and #216
  • Loading branch information
trullock authored Dec 28, 2020
2 parents a8f28ad + 70e66cd commit ce67dc0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
6 changes: 6 additions & 0 deletions src/NUglify.Tests/JavaScript/Bugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,11 @@ public void Bug214()
{
TestHelper.Instance.RunTest("-rename:all");
}

[Test]
public void Bug216()
{
TestHelper.Instance.RunTest("-rename:all");
}
}
}
6 changes: 6 additions & 0 deletions src/NUglify.Tests/NUglify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@
<Content Include="TestData\JS\Expected\Bugs\Bug199JSON.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\Bugs\Bug216.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\Bugs\Bug214.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -2765,6 +2768,9 @@
<Content Include="TestData\JS\Input\Bugs\Bug214.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\Bugs\Bug216.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\Bugs\Bug57.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
1 change: 1 addition & 0 deletions src/NUglify.Tests/TestData/JS/Expected/Bugs/Bug216.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function test(){const n=new Map;for(const[t,i]of n)alert(t+i)}test()
6 changes: 6 additions & 0 deletions src/NUglify.Tests/TestData/JS/Input/Bugs/Bug216.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

function test() {
const map = new Map();
for (const [key, value] of map) alert(key + value);
}
test();
35 changes: 11 additions & 24 deletions src/NUglify/JavaScript/Syntax/ActivationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,20 @@ protected void DefineLexicalDeclarations()
foreach (var lexDecl in LexicallyDeclaredNames)
{
// use the function as the field value if its parent is a function
// or the class node if its a class
AstNode fieldValue = lexDecl.Parent as FunctionObject;
if (fieldValue == null)
if (lexDecl.Parent is FunctionObject)
{
fieldValue = lexDecl.Parent as ClassNode;
DefineField(lexDecl, lexDecl.Parent);
continue;
}

DefineField(lexDecl, fieldValue);
// Use the class as the field value if its a class
if (lexDecl.Parent is ClassNode)
{
DefineField(lexDecl, lexDecl.Parent);
continue;
}

DefineField(lexDecl, lexDecl.Parent);
}
}

Expand Down Expand Up @@ -931,25 +937,6 @@ internal virtual void AutoRenameFields()
|| !(Settings.RemoveFunctionExpressionNames && Settings.IsModificationAllowed(TreeModifications.RemoveFunctionExpressionNames))))
{
localField.CrunchedName = crunchEnum.NextName();

// This whole block feels like a horrible hack, and is almost certainly missing many other cases.
// The following tests currently cover this block:
// CatchVariable/ForInLet
// Bugs/Bug79
if (localField.Declarations.FirstOrDefault()?.Parent?.Parent?.Parent is ForInStatement forInStatement)
{
if (forInStatement.Collection is MemberExpression memberExpression &&
memberExpression.Root is INameReference nameReference &&
localField.CrunchedName == (nameReference.VariableField.CrunchedName ?? nameReference.VariableField.Name))
{
localField.CrunchedName = crunchEnum.NextName();
}
else if (forInStatement.Collection is LookupExpression lookupExpression &&
localField.CrunchedName == (lookupExpression.VariableField.CrunchedName ?? lookupExpression.VariableField.Name))
{
localField.CrunchedName = crunchEnum.NextName();
}
}
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/NUglify/JavaScript/Visitors/ResolutionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,10 +1059,6 @@ public void Visit(ForInStatement node)
{
node.Index = NextOrderIndex;

if (node.Collection != null)
{
node.Collection.Accept(this);
}

if (node.Variable != null)
{
Expand All @@ -1083,17 +1079,13 @@ public void Visit(ForInStatement node)
}
}

node.Collection?.Accept(this);

try
{
if (node.Variable != null)
{
node.Variable.Accept(this);
}
node.Variable?.Accept(this);

if (node.Body != null)
{
node.Body.Accept(this);
}
node.Body?.Accept(this);
}
finally
{
Expand Down

0 comments on commit ce67dc0

Please sign in to comment.