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

This commit is contained in:
Logan Smyth 2018-02-01 18:54:15 -08:00 committed by GitHub
parent 96c0415c86
commit 278cd5e572
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 67 additions and 11 deletions

View File

@ -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.
@ -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;
@ -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);
});
}
@ -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);
});
}

View File

@ -0,0 +1,5 @@
function fn() {
var inner = () => {
console.log(arguments);
};
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-arrow-functions"]
}

View File

@ -0,0 +1,7 @@
function fn() {
var _arguments = arguments;
var inner = function () {
console.log(_arguments);
};
}

View File

@ -0,0 +1,8 @@
[{
"generated": {
"line": 5, "column": 16
},
"original": {
"line": 3, "column": 16
}
}]

View File

@ -0,0 +1,5 @@
function fn() {
var inner = () => {
console.log(this);
};
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-arrow-functions"]
}

View File

@ -0,0 +1,7 @@
function fn() {
var _this = this;
var inner = function () {
console.log(_this);
};
}

View File

@ -0,0 +1,8 @@
[{
"generated": {
"line": 5, "column": 16
},
"original": {
"line": 3, "column": 16
}
}]