@babel/plugin-transform-react-display-name
Advanced tools
Comparing version 7.14.5 to 7.15.0
136
lib/index.js
@@ -14,27 +14,92 @@ "use strict"; | ||
var _default = (0, _helperPluginUtils.declare)(api => { | ||
api.assertVersion(7); | ||
function addDisplayNameInCreateClass(id, call) { | ||
const props = call.arguments[0].properties; | ||
let safe = true; | ||
function addDisplayName(id, call) { | ||
const props = call.arguments[0].properties; | ||
let safe = true; | ||
for (let i = 0; i < props.length; i++) { | ||
const prop = props[i]; | ||
for (let i = 0; i < props.length; i++) { | ||
const prop = props[i]; | ||
const key = _core.types.toComputedKey(prop); | ||
const key = _core.types.toComputedKey(prop); | ||
if (_core.types.isLiteral(key, { | ||
value: "displayName" | ||
})) { | ||
safe = false; | ||
break; | ||
} | ||
} | ||
if (_core.types.isLiteral(key, { | ||
value: "displayName" | ||
})) { | ||
safe = false; | ||
break; | ||
} | ||
if (safe) { | ||
props.unshift(_core.types.objectProperty(_core.types.identifier("displayName"), _core.types.stringLiteral(id))); | ||
} | ||
} | ||
function getDisplayNameReferenceIdentifier(path) { | ||
let id; | ||
path.find(function (path) { | ||
if (path.isAssignmentExpression()) { | ||
id = path.node.left; | ||
} else if (path.isObjectProperty()) { | ||
id = path.node.key; | ||
} else if (path.isVariableDeclarator()) { | ||
id = path.node.id; | ||
} else if (path.isStatement()) { | ||
return true; | ||
} | ||
if (safe) { | ||
props.unshift(_core.types.objectProperty(_core.types.identifier("displayName"), _core.types.stringLiteral(id))); | ||
} | ||
if (id) return true; | ||
}); | ||
if (!id) return; | ||
if (_core.types.isMemberExpression(id)) { | ||
id = id.property; | ||
} | ||
if (!_core.types.isIdentifier(id)) return; | ||
return id; | ||
} | ||
function isCreateContext(node) { | ||
let callee; | ||
return _core.types.isCallExpression(node) && _core.types.isMemberExpression(callee = node.callee) && _core.types.isIdentifier(callee.object, { | ||
name: "React" | ||
}) && (!callee.computed && _core.types.isIdentifier(callee.property, { | ||
name: "createContext" | ||
}) || _core.types.isStringLiteral(callee.property, { | ||
value: "createContext" | ||
})); | ||
} | ||
function buildDisplayNameAssignment(ref, displayName) { | ||
return _core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(ref), _core.types.identifier("displayName")), _core.types.stringLiteral(displayName)); | ||
} | ||
function addDisplayNameAfterCreateContext(id, path) { | ||
const { | ||
parentPath | ||
} = path; | ||
if (parentPath.isVariableDeclarator()) { | ||
const ref = parentPath.node.id; | ||
parentPath.parentPath.insertAfter(buildDisplayNameAssignment(ref, id)); | ||
} else if (parentPath.isAssignmentExpression()) { | ||
const ref = parentPath.node.left; | ||
parentPath.insertAfter(buildDisplayNameAssignment(ref, id)); | ||
} else { | ||
const { | ||
scope | ||
} = path; | ||
const ref = scope.generateUidIdentifier("ref"); | ||
scope.push({ | ||
id: ref | ||
}); | ||
path.replaceWith(_core.types.sequenceExpression([_core.types.assignmentExpression("=", _core.types.cloneNode(ref), path.node), buildDisplayNameAssignment(ref, id), _core.types.cloneNode(ref)])); | ||
} | ||
} | ||
const createContextVisited = new WeakSet(); | ||
var _default = (0, _helperPluginUtils.declare)(api => { | ||
api.assertVersion(7); | ||
const isCreateClassCallExpression = _core.types.buildMatchMemberExpression("React.createClass"); | ||
@@ -73,3 +138,3 @@ | ||
addDisplayName(displayName, node.declaration); | ||
addDisplayNameInCreateClass(displayName, node.declaration); | ||
} | ||
@@ -82,26 +147,21 @@ }, | ||
} = path; | ||
if (!isCreateClass(node)) return; | ||
let id; | ||
path.find(function (path) { | ||
if (path.isAssignmentExpression()) { | ||
id = path.node.left; | ||
} else if (path.isObjectProperty()) { | ||
id = path.node.key; | ||
} else if (path.isVariableDeclarator()) { | ||
id = path.node.id; | ||
} else if (path.isStatement()) { | ||
return true; | ||
if (isCreateClass(node)) { | ||
const id = getDisplayNameReferenceIdentifier(path); | ||
if (id) { | ||
addDisplayNameInCreateClass(id.name, node); | ||
} | ||
} else if (isCreateContext(node)) { | ||
if (createContextVisited.has(node)) { | ||
return; | ||
} | ||
if (id) return true; | ||
}); | ||
if (!id) return; | ||
createContextVisited.add(node); | ||
const id = getDisplayNameReferenceIdentifier(path); | ||
if (_core.types.isMemberExpression(id)) { | ||
id = id.property; | ||
if (id) { | ||
addDisplayNameAfterCreateContext(id.name, path); | ||
} | ||
} | ||
if (_core.types.isIdentifier(id)) { | ||
addDisplayName(id.name, node); | ||
} | ||
} | ||
@@ -108,0 +168,0 @@ |
{ | ||
"name": "@babel/plugin-transform-react-display-name", | ||
"version": "7.14.5", | ||
"version": "7.15.0", | ||
"description": "Add displayName to React.createClass calls", | ||
@@ -26,3 +26,3 @@ "repository": { | ||
"devDependencies": { | ||
"@babel/core": "7.14.5", | ||
"@babel/core": "7.15.0", | ||
"@babel/helper-plugin-test-runner": "7.14.5" | ||
@@ -29,0 +29,0 @@ }, |
6950
134