diff --git a/lib/6to5/transformation/helpers/react.js b/lib/6to5/transformation/helpers/react.js new file mode 100644 index 0000000000..5913f7466a --- /dev/null +++ b/lib/6to5/transformation/helpers/react.js @@ -0,0 +1,31 @@ +var t = require("../../types"); + +exports.isCreateClassCallExpression = function (node) { + if (!node || !t.isCallExpression(node)) return false; + + var callee = node.callee; + if (!t.isMemberExpression(callee)) return false; + + // not React call member object + if (!t.isIdentifier(callee.object, { name: "React" })) return false; + + // not createClass call member property + if (!t.isIdentifier(callee.property, { name: "createClass" })) return false; + + // no call arguments + var args = node.arguments; + if (args.length !== 1) return false; + + // first node arg is not an object + var first = args[0]; + if (!t.isObjectExpression(first)) return false; + + return true; +}; + +exports.isReactComponentMemberExpression = function (node) { + if (!node || !t.isMemberExpression(node)) return false; + if (!t.isIdentifier(node.object, { name: "React" })) return false; + if (!t.isIdentifier(node.property, { name: "Component" })) return false; + return true; +}; diff --git a/lib/6to5/transformation/transformers/index.js b/lib/6to5/transformation/transformers/index.js index ee1428402c..c47706dcf0 100644 --- a/lib/6to5/transformation/transformers/index.js +++ b/lib/6to5/transformation/transformers/index.js @@ -12,6 +12,7 @@ module.exports = { "playground.objectGetterMemoization": require("./playground/object-getter-memoization"), react: require("./other/react"), + "optimisation.react": require("./optimisation/react"), _modules: require("./internal/modules"), diff --git a/lib/6to5/transformation/transformers/optimisation/react/base.js b/lib/6to5/transformation/transformers/optimisation/react/base.js new file mode 100644 index 0000000000..a649b939cb --- /dev/null +++ b/lib/6to5/transformation/transformers/optimisation/react/base.js @@ -0,0 +1,9 @@ +module.exports = BaseOptimiser; + +function BaseOptimiser() { + +} + +BaseOptimiser.prototype.optimise = function () { + +}; diff --git a/lib/6to5/transformation/transformers/optimisation/react/create-class.js b/lib/6to5/transformation/transformers/optimisation/react/create-class.js new file mode 100644 index 0000000000..423e1cc392 --- /dev/null +++ b/lib/6to5/transformation/transformers/optimisation/react/create-class.js @@ -0,0 +1,10 @@ +module.exports = CreateClassOptimiser; + +var BaseOptimiser = require("./base"); +var util = require("../../../../util"); + +function CreateClassOptimiser() { + +} + +util.inherits(CreateClassOptimiser, BaseOptimiser); diff --git a/lib/6to5/transformation/transformers/optimisation/react/index.js b/lib/6to5/transformation/transformers/optimisation/react/index.js new file mode 100644 index 0000000000..49073065e2 --- /dev/null +++ b/lib/6to5/transformation/transformers/optimisation/react/index.js @@ -0,0 +1,17 @@ +var CreateClassOptimiser = require("./create-class"); +var NativeClassOptimiser = require("./native-class"); +var react = require("../../../helpers/react"); + +exports.optional = true; + +exports.CallExpression = function (node) { + if (react.isCreateClassCallExpression(node)) { + new CreateClassOptimiser(node.arguments[0]).optimise(); + } +}; + +exports.CallExpression = function (node) { + if (react.isReactComponentMemberExpression(node.superClass)) { + new NativeClassOptimiser(node).optimise(); + } +}; diff --git a/lib/6to5/transformation/transformers/optimisation/react/native-class.js b/lib/6to5/transformation/transformers/optimisation/react/native-class.js new file mode 100644 index 0000000000..e86899f362 --- /dev/null +++ b/lib/6to5/transformation/transformers/optimisation/react/native-class.js @@ -0,0 +1,10 @@ +module.exports = NativeClassOptimiser; + +var BaseOptimiser = require("./base"); +var util = require("../../../../util"); + +function NativeClassOptimiser() { + +} + +util.inherits(NativeClassOptimiser, BaseOptimiser); diff --git a/lib/6to5/transformation/transformers/other/react.js b/lib/6to5/transformation/transformers/other/react.js index 43803868af..09c39b0e12 100644 --- a/lib/6to5/transformation/transformers/other/react.js +++ b/lib/6to5/transformation/transformers/other/react.js @@ -6,6 +6,7 @@ // jsx var esutils = require("esutils"); +var react = require("../../helpers/react"); var t = require("../../../types"); exports.JSXIdentifier = function (node, parent) { @@ -197,33 +198,8 @@ var cleanJSXElementLiteralChild = function (child, args) { // display names -var isCreateClass = function (call) { - if (!call || !t.isCallExpression(call)) return false; - - var callee = call.callee; - if (!t.isMemberExpression(callee)) return false; - - // not React call member object - var obj = callee.object; - if (!t.isIdentifier(obj, { name: "React" })) return false; - - // not createClass call member property - var prop = callee.property; - if (!t.isIdentifier(prop, { name: "createClass" })) return false; - - // no call arguments - var args = call.arguments; - if (args.length !== 1) return false; - - // first call arg is not an object - var first = args[0]; - if (!t.isObjectExpression(first)) return; - - return true; -}; - var addDisplayName = function (id, call) { - if (!isCreateClass(call)) return; + if (!react.isCreateClassCallExpression(call)) return; var props = call.arguments[0].properties; var safe = true;