Skip to content

Commit 344fd6c

Browse files
committed
Fix #313
1 parent 286ccf0 commit 344fd6c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

NiL.JS/Statements/VariableDefinition.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using NiL.JS.Core;
45
using NiL.JS.Expressions;
56

@@ -153,9 +154,18 @@ internal static CodeNode Parse(ParseInfo state, ref int index, bool forForLoop)
153154
else
154155
position = s;
155156

157+
names.Sort();
158+
156159
var variables = new VariableDescriptor[names.Count];
157-
for (int i = 0, skiped = 0; i < names.Count; i++)
160+
var skiped = 0;
161+
for (var i = 0; i < names.Count; i++)
158162
{
163+
if (i > 0 && names[i] == names[i - 1])
164+
{
165+
skiped++;
166+
continue;
167+
}
168+
159169
bool skip = false;
160170
for (var j = 0; j < state.Variables.Count - i + skiped; j++)
161171
{
@@ -183,9 +193,13 @@ internal static CodeNode Parse(ParseInfo state, ref int index, bool forForLoop)
183193
state.Variables.Add(variables[i]);
184194
}
185195

196+
if (skiped > 0 && variables.Any(static x => x == null))
197+
variables = [.. variables.Where(static x => x != null)];
198+
199+
186200
var pos = index;
187201
index = position;
188-
return new VariableDefinition(variables, initializers.ToArray(), mode)
202+
return new VariableDefinition(variables, [.. initializers], mode)
189203
{
190204
Position = pos,
191205
Length = index - pos

Tests/Fuzz/Bug_313.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var someModule = (function () {
2+
var storage = { foo: 123, bar: 456 };
3+
4+
function SomeType() { }
5+
6+
SomeType.prototype = {
7+
doSomething: function () {
8+
var result = '',
9+
nums = [9, 8, 7]
10+
;
11+
12+
// Declares a `storage` variable twice, which leads to a global scope violation
13+
for (var i = 0, storage = undefined, storage = nums.length; i < storage; i++) {
14+
result += nums[i];
15+
}
16+
17+
result += this.getItemFromStorage("foo");
18+
19+
return result;
20+
},
21+
getItemFromStorage: function (key) {
22+
return storage[key];
23+
}
24+
};
25+
26+
return new SomeType();
27+
}());
28+
29+
someModule.doSomething();

0 commit comments

Comments
 (0)