From e712c5225b5b285a2f6895b82d3d2736eebc72f9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 2 Feb 2015 23:48:03 +1100 Subject: [PATCH] use the current file basename for the displayName of export default React.createClass - 6to5/6to5-sublime#21 --- lib/6to5/file.js | 11 +++++++---- lib/6to5/transformation/transformers/other/react.js | 10 +++++++--- .../react/display-name-export-default/actual.js | 5 +++++ .../react/display-name-export-default/expected.js | 6 ++++++ 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/transformation/react/display-name-export-default/actual.js create mode 100644 test/fixtures/transformation/react/display-name-export-default/expected.js diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 20625f465c..3486f41a2f 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -4,16 +4,17 @@ module.exports = File; var SHEBANG_REGEX = /^\#\!.*/; +var isFunction = require("lodash/lang/isFunction"); var transform = require("./transformation"); var generate = require("./generation"); +var defaults = require("lodash/object/defaults"); +var contains = require("lodash/collection/contains"); var clone = require("./helpers/clone"); var Scope = require("./traverse/scope"); var util = require("./util"); -var t = require("./types"); -var contains = require("lodash/collection/contains"); +var path = require("path"); var each = require("lodash/collection/each"); -var defaults = require("lodash/object/defaults"); -var isFunction = require("lodash/lang/isFunction"); +var t = require("./types"); function File(opts) { this.dynamicImportIds = {}; @@ -118,6 +119,8 @@ File.prototype.normaliseOptions = function (opts) { // normalise windows path separators to unix opts.filename = opts.filename.replace(/\\/g, "/"); + opts.basename = path.basename(opts.filename, path.extname(opts.filename)); + opts.blacklist = util.arrayify(opts.blacklist); opts.whitelist = util.arrayify(opts.whitelist); opts.optional = util.arrayify(opts.optional); diff --git a/lib/6to5/transformation/transformers/other/react.js b/lib/6to5/transformation/transformers/other/react.js index e2e43dcd2d..6d2401b265 100644 --- a/lib/6to5/transformation/transformers/other/react.js +++ b/lib/6to5/transformation/transformers/other/react.js @@ -199,8 +199,6 @@ var cleanJSXElementLiteralChild = function (child, args) { // display names var addDisplayName = function (id, call) { - if (!react.isCreateClass(call)) return; - var props = call.arguments[0].properties; var safe = true; @@ -217,6 +215,12 @@ var addDisplayName = function (id, call) { } }; +exports.ExportDeclaration = function (node, parent, scope, context, file) { + if (node.default && react.isCreateClass(node.declaration)) { + addDisplayName(file.opts.basename, node.declaration); + } +}; + exports.AssignmentExpression = exports.Property = exports.VariableDeclarator = function (node) { @@ -237,7 +241,7 @@ exports.VariableDeclarator = function (node) { left = left.property; } - if (t.isIdentifier(left)) { + if (t.isIdentifier(left) && react.isCreateClass(right)) { addDisplayName(left.name, right); } }; diff --git a/test/fixtures/transformation/react/display-name-export-default/actual.js b/test/fixtures/transformation/react/display-name-export-default/actual.js new file mode 100644 index 0000000000..370f77173c --- /dev/null +++ b/test/fixtures/transformation/react/display-name-export-default/actual.js @@ -0,0 +1,5 @@ +export default React.createClass({ + render: function () { + return null; + } +}); diff --git a/test/fixtures/transformation/react/display-name-export-default/expected.js b/test/fixtures/transformation/react/display-name-export-default/expected.js new file mode 100644 index 0000000000..182f4fe315 --- /dev/null +++ b/test/fixtures/transformation/react/display-name-export-default/expected.js @@ -0,0 +1,6 @@ +module.exports = React.createClass({ + displayName: "actual", + render: function () { + return null; + } +});