Skip to content

Commit

Permalink
Preserve identifier location information when mapping this and argume…
Browse files Browse the repository at this point in the history
…nts. (babel#7312)
  • Loading branch information
loganfsmyth authored and aminmarashi committed Mar 17, 2018
1 parent cf756b0 commit 8a70541
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 11 deletions.
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
}
}]

0 comments on commit 8a70541

Please sign in to comment.