Skip to content

Commit add64e8

Browse files
Don't resolve shadowed arguments variables from functions (#14036)
1 parent dfcfbf3 commit add64e8

6 files changed

Lines changed: 51 additions & 1 deletion

File tree

packages/babel-plugin-transform-arrow-functions/test/fixtures/arrow-functions/arguments-global-var/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var _arguments2 = 1;
22

33
function fn() {
4-
var _arguments = _arguments2;
4+
var _arguments = arguments;
55

66
var foo = function () {
77
return _arguments;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var arguments = [1, 2, 3];
2+
var arr = () => arguments[0];
3+
4+
expect(arr()).toStrictEqual(1)
5+
6+
function foo(n) {
7+
var f = () => arguments[0] + n; // foo's implicit arguments binding. arguments[0] is n
8+
return f();
9+
}
10+
11+
expect(foo(3)).toStrictEqual(6)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var arguments = [1, 2, 3];
2+
var arr = (n) => arguments[0];
3+
4+
arr(4); // 1
5+
6+
function foo(n) {
7+
var f = () => arguments[0] + n; // foo's implicit arguments binding. arguments[0] is n
8+
return f();
9+
}
10+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sourceType": "script",
3+
"plugins": ["transform-arrow-functions"]
4+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var _arguments = [1, 2, 3];
2+
3+
var arr = function (n) {
4+
return _arguments[0];
5+
};
6+
7+
arr(4); // 1
8+
9+
function foo(n) {
10+
var _arguments2 = arguments;
11+
12+
var f = function () {
13+
return _arguments2[0] + n;
14+
}; // foo's implicit arguments binding. arguments[0] is n
15+
16+
17+
return f();
18+
}

packages/babel-traverse/src/scope/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,13 @@ export default class Scope {
11381138
} else {
11391139
return binding;
11401140
}
1141+
} else if (
1142+
!binding &&
1143+
name === "arguments" &&
1144+
scope.path.isFunction() &&
1145+
!scope.path.isArrowFunctionExpression()
1146+
) {
1147+
break;
11411148
}
11421149
previousPath = scope.path;
11431150
} while ((scope = scope.parent));

0 commit comments

Comments
 (0)