Skip to content

Commit

Permalink
Backport babel#7312 for 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Apr 25, 2018
1 parent 1c3445a commit 7360a30
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 17 deletions.
Expand Up @@ -91,29 +91,33 @@ function remap(path, key) {
// binding since arrow function syntax already does that.
if (!passedShadowFunction) return;

const cached = fnPath.getData(key);
if (cached) return path.replaceWith(cached);
let cached = fnPath.getData(key);
if (!cached) {
const id = path.scope.generateUidIdentifier(key);

const id = path.scope.generateUidIdentifier(key);
fnPath.setData(key, id);
cached = id;

fnPath.setData(key, id);
const classPath = fnPath.findParent((p) => p.isClass());
const hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass);

const classPath = fnPath.findParent((p) => p.isClass());
const hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass);
if (key === "this" && fnPath.isMethod({ kind: "constructor" }) && hasSuperClass) {
fnPath.scope.push({ id });

if (key === "this" && fnPath.isMethod({ kind: "constructor" }) && hasSuperClass) {
fnPath.scope.push({ id });
fnPath.traverse(superVisitor, { id });
} else {
const init = key === "this" ? t.thisExpression() : t.identifier(key);

fnPath.traverse(superVisitor, { id });
} else {
const init = key === "this" ? t.thisExpression() : t.identifier(key);

// Forward the shadowed function, so that the identifiers do not get hoisted
// up to the first non shadow function but rather up to the bound shadow function
if (shadowFunction) init._shadowedFunctionLiteral = shadowFunction;
// Forward the shadowed function, so that the identifiers do not get hoisted
// up to the first non shadow function but rather up to the bound shadow function
if (shadowFunction) init._shadowedFunctionLiteral = shadowFunction;

fnPath.scope.push({ id, init });
fnPath.scope.push({ id, init });
}
}

return path.replaceWith(id);
const node = t.cloneDeep(cached);
node.loc = path.node.loc;

return path.replaceWith(node);
}
@@ -0,0 +1,5 @@
function fn() {
var inner = () => {
console.log(arguments);
};
}
@@ -0,0 +1,7 @@
function fn() {
var _arguments = arguments;

var inner = function () {
console.log(_arguments);
};
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-es2015-arrow-functions"]
}
@@ -0,0 +1,18 @@
{
"file": "arrow-functions/arguments-source-maps/expected.js",
"mappings": "AAAA,SAASA,EAAT,GAAc;AAAA;;AACZ,MAAIC,QAAQ,YAAM;AAChBC,YAAQC,GAAR,CAAYC,UAAZ;AACD,GAFD;AAGD",
"names": [
"fn",
"inner",
"console",
"log",
"arguments"
],
"sources": [
"arrow-functions/arguments-source-maps/actual.js"
],
"sourcesContent": [
"function fn() {\n var inner = () => {\n console.log(arguments);\n };\n}"
],
"version": 3
}
@@ -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,7 @@
function fn() {
var _this = this;

var inner = function () {
console.log(_this);
};
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-es2015-arrow-functions"]
}
@@ -0,0 +1,17 @@
{
"file": "arrow-functions/this-source-maps/expected.js",
"mappings": "AAAA,SAASA,EAAT,GAAc;AAAA;;AACZ,MAAIC,QAAQ,YAAM;AAChBC,YAAQC,GAAR,CAAY,KAAZ;AACD,GAFD;AAGD",
"names": [
"fn",
"inner",
"console",
"log"
],
"sources": [
"arrow-functions/this-source-maps/actual.js"
],
"sourcesContent": [
"function fn() {\n var inner = () => {\n console.log(this);\n };\n}"
],
"version": 3
}
@@ -0,0 +1,8 @@
[{
"generated": {
"line": 5, "column": 16
},
"original": {
"line": 3, "column": 16
}
}]

0 comments on commit 7360a30

Please sign in to comment.