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

Preserve identifier location information when mapping this and arguments. #7312

Merged
merged 1 commit into from Feb 2, 2018
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
32 changes: 21 additions & 11 deletions packages/babel-traverse/src/path/conversion.js
Expand Up @@ -208,9 +208,12 @@ function hoistFunctionEnvironment(
},
});
const superBinding = getSuperBinding(thisEnvFn);
allSuperCalls.forEach(superCall =>
superCall.get("callee").replaceWith(t.identifier(superBinding)),
);
allSuperCalls.forEach(superCall => {
const callee = t.identifier(superBinding);
callee.loc = superCall.node.callee.loc;

superCall.get("callee").replaceWith(callee);
});
}

// Convert all "this" references in the arrow to point at the alias.
Expand All @@ -225,11 +228,12 @@ function hoistFunctionEnvironment(
(inConstructor && hasSuperClass(thisEnvFn))
) {
thisPaths.forEach(thisChild => {
thisChild.replaceWith(
thisChild.isJSX()
? t.jsxIdentifier(thisBinding)
: t.identifier(thisBinding),
);
const thisRef = thisChild.isJSX()
? t.jsxIdentifier(thisBinding)
: t.identifier(thisBinding);

thisRef.loc = thisChild.node.loc;
thisChild.replaceWith(thisRef);
});

if (specCompliant) thisBinding = null;
Expand All @@ -243,7 +247,10 @@ function hoistFunctionEnvironment(
);

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

argumentsChild.replaceWith(argsRef);
});
}

Expand All @@ -253,8 +260,11 @@ function hoistFunctionEnvironment(
t.metaProperty(t.identifier("new"), t.identifier("target")),
);

newTargetPaths.forEach(argumentsChild => {
argumentsChild.replaceWith(t.identifier(newTargetBinding));
newTargetPaths.forEach(targetChild => {
const targetRef = t.identifier(newTargetBinding);
targetRef.loc = targetChild.node.loc;

targetChild.replaceWith(targetRef);
});
}

Expand Down
@@ -0,0 +1,5 @@
function fn() {
var inner = () => {
console.log(arguments);
};
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-arrow-functions"]
}
@@ -0,0 +1,7 @@
function fn() {
var _arguments = arguments;

var inner = function () {
console.log(_arguments);
};
}
@@ -0,0 +1,8 @@
[{
"generated": {
"line": 5, "column": 16
},
"original": {
"line": 3, "column": 16
}
}]
@@ -0,0 +1,5 @@
function fn() {
var inner = () => {
console.log(this);
};
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-arrow-functions"]
}
@@ -0,0 +1,7 @@
function fn() {
var _this = this;

var inner = function () {
console.log(_this);
};
}
@@ -0,0 +1,8 @@
[{
"generated": {
"line": 5, "column": 16
},
"original": {
"line": 3, "column": 16
}
}]