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 arrow transformation when arguments is defined as variable #12344

Merged
merged 4 commits into from Jul 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,9 @@
function fn() {
var foo = () => {
return arguments;
};
}

var bar = () => arguments;

var baz = () => () => arguments;
@@ -0,0 +1,19 @@
var _arguments2 = typeof arguments === "undefined" ? void 0 : arguments;

function fn() {
var _arguments = arguments;

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

var bar = function () {
return _arguments2;
};

var baz = function () {
return function () {
return _arguments2;
};
};
@@ -0,0 +1,10 @@
var arguments = 1;
function fn() {
var foo = () => {
return arguments;
};
}

var bar = () => arguments;

var baz = () => () => arguments;
@@ -0,0 +1,19 @@
var _arguments2 = 1;

function fn() {
var _arguments = _arguments2;

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

var bar = function () {
return _arguments2;
};

var baz = function () {
return function () {
return _arguments2;
};
};
Expand Up @@ -43,3 +43,39 @@ 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 eleven = () => {
var arguments = 2;
return function () {
return () => arguments;
}
};
eleven()(1,2,3)();

var twelve = () => {
var arguments = 2;
return class {
m() { return () => arguments; }
}
};
twelve();
Expand Up @@ -81,3 +81,56 @@ function six(obj) {
}

six();

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

seven();

var eight = function () {
var _arguments7 = 1;
return function () {
return _arguments7;
};
};

eight();

function nine() {
var _arguments8 = 1;

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

nine();

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

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

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

};
};

twelve();
30 changes: 27 additions & 3 deletions packages/babel-traverse/src/path/conversion.ts
Expand Up @@ -231,9 +231,22 @@ function hoistFunctionEnvironment(

// Convert all "arguments" references in the arrow to point at the alias.
if (argumentsPaths.length > 0) {
const argumentsBinding = getBinding(thisEnvFn, "arguments", () =>
t.identifier("arguments"),
);
const argumentsBinding = getBinding(thisEnvFn, "arguments", () => {
const args = () => t.identifier("arguments");
if (thisEnvFn.scope.path.isProgram()) {
return t.conditionalExpression(
t.binaryExpression(
"===",
t.unaryExpression("typeof", args()),
t.stringLiteral("undefined"),
),
thisEnvFn.scope.buildUndefinedNode(),
args(),
);
Comment on lines +236 to +245
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this code to avoid the error described in #7673. It still isn't 100% correct since sometimes it doesn't throw the error if arguments is undeclared, but at least it doesn't throw for valid code.

} else {
return args();
}
});

argumentsPaths.forEach(argumentsChild => {
const argsRef = t.identifier(argumentsBinding);
Expand Down Expand Up @@ -588,6 +601,17 @@ function getScopeInformation(fnPath) {
ReferencedIdentifier(child) {
if (child.node.name !== "arguments") return;

let curr = child.scope;
do {
if (curr.hasOwnBinding("arguments")) {
curr.rename("arguments");
return;
}
if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
break;
}
} while ((curr = curr.parent));

argumentsPaths.push(child);
},
MetaProperty(child) {
Expand Down