Skip to content

Commit 278cd5e

Browse files
authored
Preserve identifier location information when mapping this and arguments. (babel#7312)
1 parent 96c0415 commit 278cd5e

File tree

9 files changed

+67
-11
lines changed

9 files changed

+67
-11
lines changed

packages/babel-traverse/src/path/conversion.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,12 @@ function hoistFunctionEnvironment(
208208
},
209209
});
210210
const superBinding = getSuperBinding(thisEnvFn);
211-
allSuperCalls.forEach(superCall =>
212-
superCall.get("callee").replaceWith(t.identifier(superBinding)),
213-
);
211+
allSuperCalls.forEach(superCall => {
212+
const callee = t.identifier(superBinding);
213+
callee.loc = superCall.node.callee.loc;
214+
215+
superCall.get("callee").replaceWith(callee);
216+
});
214217
}
215218

216219
// Convert all "this" references in the arrow to point at the alias.
@@ -225,11 +228,12 @@ function hoistFunctionEnvironment(
225228
(inConstructor && hasSuperClass(thisEnvFn))
226229
) {
227230
thisPaths.forEach(thisChild => {
228-
thisChild.replaceWith(
229-
thisChild.isJSX()
230-
? t.jsxIdentifier(thisBinding)
231-
: t.identifier(thisBinding),
232-
);
231+
const thisRef = thisChild.isJSX()
232+
? t.jsxIdentifier(thisBinding)
233+
: t.identifier(thisBinding);
234+
235+
thisRef.loc = thisChild.node.loc;
236+
thisChild.replaceWith(thisRef);
233237
});
234238

235239
if (specCompliant) thisBinding = null;
@@ -243,7 +247,10 @@ function hoistFunctionEnvironment(
243247
);
244248

245249
argumentsPaths.forEach(argumentsChild => {
246-
argumentsChild.replaceWith(t.identifier(argumentsBinding));
250+
const argsRef = t.identifier(argumentsBinding);
251+
argsRef.loc = argumentsChild.node.loc;
252+
253+
argumentsChild.replaceWith(argsRef);
247254
});
248255
}
249256

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

256-
newTargetPaths.forEach(argumentsChild => {
257-
argumentsChild.replaceWith(t.identifier(newTargetBinding));
263+
newTargetPaths.forEach(targetChild => {
264+
const targetRef = t.identifier(newTargetBinding);
265+
targetRef.loc = targetChild.node.loc;
266+
267+
targetChild.replaceWith(targetRef);
258268
});
259269
}
260270

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function fn() {
2+
var inner = () => {
3+
console.log(arguments);
4+
};
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["transform-arrow-functions"]
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function fn() {
2+
var _arguments = arguments;
3+
4+
var inner = function () {
5+
console.log(_arguments);
6+
};
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[{
2+
"generated": {
3+
"line": 5, "column": 16
4+
},
5+
"original": {
6+
"line": 3, "column": 16
7+
}
8+
}]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function fn() {
2+
var inner = () => {
3+
console.log(this);
4+
};
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["transform-arrow-functions"]
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function fn() {
2+
var _this = this;
3+
4+
var inner = function () {
5+
console.log(_this);
6+
};
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[{
2+
"generated": {
3+
"line": 5, "column": 16
4+
},
5+
"original": {
6+
"line": 3, "column": 16
7+
}
8+
}]

0 commit comments

Comments
 (0)