Skip to content

Commit

Permalink
fix: arrow-fn transformation when 'arguments' is defined as var
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 18, 2021
1 parent 368bf89 commit c4b1d30
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 22 deletions.
Expand Up @@ -43,3 +43,47 @@ function six(obj) {
return fn();
}
six();

var seven = () => {
var arguments = 1;
return arguments;
};
seven();

var eight = () => {
var arguments = 1;
return () => arguments;
};
eight();

function nine() {
var arguments = 1;
var foo = () => {
return arguments;
};
}
nine();

var arguments = 1;
function ten() {
var foo = () => {
return arguments;
};
}
ten();

var eleven = () => {
var arguments = 2;
return function () {
return () => arguments;
}
};
eleven()(1,2,3)();

var twelve = () => {
var arguments = 2;
return class {
m() { return () => arguments; }
}
};
twelve();
@@ -1,8 +1,10 @@
var _arguments4 = arguments;

function one() {
var _arguments = arguments;
var _arguments2 = arguments;

var inner = function () {
return _arguments;
return _arguments2;
};

return [].slice.call(inner());
Expand All @@ -11,17 +13,13 @@ function one() {
one(1, 2);

function two() {
var _arguments2 = arguments;

var inner = function () {
return _arguments2;
return _arguments;
};

var another = function () {
var _arguments3 = arguments;

var inner2 = function () {
return _arguments3;
return _arguments;
};
};

Expand All @@ -31,10 +29,8 @@ function two() {
two(1, 2);

function three() {
var _arguments4 = arguments;

var fn = function () {
return _arguments4[0] + "bar";
return _arguments[0] + "bar";
};

return fn();
Expand All @@ -43,10 +39,8 @@ function three() {
three("foo");

function four() {
var _arguments5 = arguments;

var fn = function () {
return _arguments5[0].foo + "bar";
return _arguments[0].foo + "bar";
};

return fn();
Expand All @@ -71,7 +65,7 @@ five({
function six(obj) {
var fn = function () {
var fn2 = function () {
return arguments[0];
return _arguments[0];
};

return fn2("foobar");
Expand All @@ -81,3 +75,66 @@ function six(obj) {
}

six();

var seven = function () {
var arguments = 1;
return arguments;
};

seven();

var eight = function () {
var _arguments3 = 1;
return function () {
return _arguments4;
};
};

eight();

function nine() {
var _arguments6 = arguments;
var _arguments5 = 1;

var foo = function () {
return _arguments6;
};
}

nine();
var _arguments = 1;

function ten() {
var foo = function () {
return _arguments;
};
}

ten();

var eleven = function () {
var _arguments7 = 2;
return function () {
var _arguments8 = arguments;
return function () {
return _arguments8;
};
};
};

eleven()(1, 2, 3)();

var twelve = function () {
var _arguments9 = 2;
return class {
m() {
var _arguments10 = arguments;
return function () {
return _arguments10;
};
}

};
};

twelve();
32 changes: 25 additions & 7 deletions packages/babel-traverse/src/path/conversion.ts
Expand Up @@ -236,10 +236,20 @@ function hoistFunctionEnvironment(
);

argumentsPaths.forEach(argumentsChild => {
const argsRef = t.identifier(argumentsBinding);
argsRef.loc = argumentsChild.node.loc;

argumentsChild.replaceWith(argsRef);
let currentScope = argumentsChild.scope;
do {
if (
!currentScope.hasOwnBinding("arguments") &&
!(
currentScope.path.isFunction() &&
!currentScope.path.isArrowFunctionExpression()
)
) {
const argsRef = t.identifier(argumentsBinding);
argsRef.loc = argumentsChild.node.loc;
argumentsChild.replaceWith(argsRef);
}
} while ((currentScope = currentScope.parent));
});
}

Expand Down Expand Up @@ -586,9 +596,17 @@ function getScopeInformation(fnPath) {
if (child.get("object").isSuper()) superProps.push(child);
},
ReferencedIdentifier(child) {
if (child.node.name !== "arguments") return;

argumentsPaths.push(child);
let curr = child.scope;
do {
if (curr.hasOwnBinding("arguments")) return;
if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
break;
}
if (child.node.name !== "arguments") return;

curr.rename("arguments");
argumentsPaths.push(child);
} while ((curr = curr.parent));
},
MetaProperty(child) {
if (!child.get("meta").isIdentifier({ name: "new" })) return;
Expand Down

0 comments on commit c4b1d30

Please sign in to comment.