Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for(let x of y.prop) #69

Merged
merged 3 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/NUglify.Tests/JavaScript/CatchVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public void NoOuter_h()
TestHelper.Instance.RunTest("-rename:all");
}

[Test]
public void ForInLet()
{
TestHelper.Instance.RunTest();
}

[Test]
public void ForInLet_h()
{
TestHelper.Instance.RunTest("-rename:all");
}

[Test]
public void OuterIsGlobal()
{
Expand Down
9 changes: 9 additions & 0 deletions src/NUglify.Tests/NUglify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@
<Content Include="TestData\JS\Expected\BlockOpts\ReturnLiteral.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\CatchVariable\ForInLet.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\CatchVariable\ForInLet_h.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\ControlFlow\BreakNoLabel.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -2516,6 +2522,9 @@
<Content Include="TestData\JS\Input\BlockOpts\ReturnLiteral.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\CatchVariable\ForInLet.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\Classes\ClassDecl.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function go(scope){for(let test in scope.prop)console.log(test)}go({prop:[1,2,3]})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function go(n){for(let t in n.prop)console.log(t)}go({prop:[1,2,3]})
7 changes: 7 additions & 0 deletions src/NUglify.Tests/TestData/JS/Input/CatchVariable/ForInLet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function go(scope) {
for (let test in scope.prop) {
console.log(test);
}
}

go({ prop: [1, 2, 3] });
6 changes: 1 addition & 5 deletions src/NUglify/JavaScript/CodeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,7 @@ public bool HasRenamePairs
/// <returns>new name if it exists, null otherwise</returns>
public string GetNewName(string sourceName)
{
string newName;
if (!m_identifierReplacementMap.TryGetValue(sourceName, out newName))
{
newName = null;
}
m_identifierReplacementMap.TryGetValue(sourceName, out var newName);

return newName;
}
Expand Down
9 changes: 9 additions & 0 deletions src/NUglify/JavaScript/Syntax/ActivationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUglify.Helpers;
using NUglify.JavaScript.Visitors;
Expand Down Expand Up @@ -922,6 +923,14 @@ internal virtual void AutoRenameFields()
|| !(Settings.RemoveFunctionExpressionNames && Settings.IsModificationAllowed(TreeModifications.RemoveFunctionExpressionNames))))
{
localField.CrunchedName = crunchEnum.NextName();

if (localField.Declarations.FirstOrDefault()?.Parent?.Parent?.Parent is ForInStatement forInStatement &&
forInStatement.Collection is MemberExpression memberExpression &&
memberExpression.Root is INameReference nameReference &&
localField.CrunchedName == (nameReference.VariableField.CrunchedName ?? nameReference.VariableField.Name))
{
localField.CrunchedName = crunchEnum.NextName();
}
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/NUglify/JavaScript/Visitors/AnalyzeNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,9 @@ private void CombineExpressions(BlockStatement node)
{
CombineWithPreviousExpression(node, ndx);
}
else
else if (node[ndx - 1] is VarDeclaration previousVar)
{
var previousVar = node[ndx - 1] as VarDeclaration;
if (previousVar != null)
{
CombineWithPreviousVar(node, ndx, previousVar);
}
CombineWithPreviousVar(node, ndx, previousVar);
}
}
}
Expand Down