From bf393c025fc227cce1eb46f3e9436dec3c199a06 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 30 Jan 2015 11:00:37 +1100 Subject: [PATCH] properly transform `XJSIdentifier` nodes referencing `this` into a `ThisExpression` - facebook/react#2927 --- lib/6to5/transformation/transformers/other/react.js | 6 ++++-- .../fixtures/transformation/react/arrow-functions/actual.js | 3 +++ .../transformation/react/arrow-functions/expected.js | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/transformation/react/arrow-functions/actual.js create mode 100644 test/fixtures/transformation/react/arrow-functions/expected.js diff --git a/lib/6to5/transformation/transformers/other/react.js b/lib/6to5/transformation/transformers/other/react.js index 08ccc30356..43803868af 100644 --- a/lib/6to5/transformation/transformers/other/react.js +++ b/lib/6to5/transformation/transformers/other/react.js @@ -8,8 +8,10 @@ var esutils = require("esutils"); var t = require("../../../types"); -exports.JSXIdentifier = function (node) { - if (esutils.keyword.isIdentifierName(node.name)) { +exports.JSXIdentifier = function (node, parent) { + if (node.name === "this" && t.isReferenced(node, parent)) { + return t.thisExpression(); + } else if (esutils.keyword.isIdentifierName(node.name)) { node.type = "Identifier"; } else { return t.literal(node.name); diff --git a/test/fixtures/transformation/react/arrow-functions/actual.js b/test/fixtures/transformation/react/arrow-functions/actual.js new file mode 100644 index 0000000000..fc0906b174 --- /dev/null +++ b/test/fixtures/transformation/react/arrow-functions/actual.js @@ -0,0 +1,3 @@ +var foo = function () { + return () => ; +}; diff --git a/test/fixtures/transformation/react/arrow-functions/expected.js b/test/fixtures/transformation/react/arrow-functions/expected.js new file mode 100644 index 0000000000..ba37738e61 --- /dev/null +++ b/test/fixtures/transformation/react/arrow-functions/expected.js @@ -0,0 +1,6 @@ +var foo = function () { + var _this = this; + return function () { + return React.createElement(_this, null); + }; +};