Skip to content

Commit

Permalink
Revert "Add display name after create context (#13501)" (#13637)
Browse files Browse the repository at this point in the history
This reverts commit e9bc7c1.
  • Loading branch information
nicolo-ribaudo committed Aug 5, 2021
1 parent e891601 commit da2168e
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 146 deletions.
169 changes: 49 additions & 120 deletions packages/babel-plugin-transform-react-display-name/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,116 +2,28 @@ import { declare } from "@babel/helper-plugin-utils";
import path from "path";
import { types as t } from "@babel/core";

function addDisplayNameInCreateClass(id, call) {
const props = call.arguments[0].properties;
let safe = true;

for (let i = 0; i < props.length; i++) {
const prop = props[i];
const key = t.toComputedKey(prop);
if (t.isLiteral(key, { value: "displayName" })) {
safe = false;
break;
}
}
export default declare(api => {
api.assertVersion(7);

if (safe) {
props.unshift(
t.objectProperty(t.identifier("displayName"), t.stringLiteral(id)),
);
}
}

function getDisplayNameReferenceIdentifier(
path: NodePath<t.CallExpression>,
): ?t.Identifier {
let id;

// crawl up the ancestry looking for possible candidates for displayName inference
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()) {
// we've hit a statement, we should stop crawling up
return 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];
const key = t.toComputedKey(prop);
if (t.isLiteral(key, { value: "displayName" })) {
safe = false;
break;
}
}

// we've got an id! no need to continue
if (id) return true;
});

// ensure that we have an identifier we can inherit from
if (!id) return;

// foo.bar -> bar
if (t.isMemberExpression(id)) {
id = id.property;
}

// identifiers are the only thing we can reliably get a name from
if (!t.isIdentifier(id)) return;

return id;
}

function isCreateContext(node) {
let callee;
return (
t.isCallExpression(node) &&
t.isMemberExpression((callee = node.callee)) &&
t.isIdentifier(callee.object, { name: "React" }) &&
((!callee.computed &&
t.isIdentifier(callee.property, { name: "createContext" })) ||
t.isStringLiteral(callee.property, { value: "createContext" }))
);
}

function buildDisplayNameAssignment(ref, displayName) {
return t.assignmentExpression(
"=",
t.memberExpression(t.cloneNode(ref), t.identifier("displayName")),
t.stringLiteral(displayName),
);
}

function addDisplayNameAfterCreateContext(
id,
path: t.NodePath<t.CallExpression>,
) {
const { parentPath } = path;
if (parentPath.isVariableDeclarator()) {
// FooContext = React.createContext()
const ref = parentPath.node.id;
// parentPath.parentPath must be a VariableDeclaration because getDisplayNameReferenceIdentifier
// does not support patterns
parentPath.parentPath.insertAfter(buildDisplayNameAssignment(ref, id));
} else if (parentPath.isAssignmentExpression()) {
// var FooContext = React.createContext()
const ref = parentPath.node.left;
parentPath.insertAfter(buildDisplayNameAssignment(ref, id));
} else {
// (ref = React.createContext(), ref.displayName = "id", ref)
const { scope } = path;
const ref = scope.generateUidIdentifier("ref");
scope.push({ id: ref });
path.replaceWith(
t.sequenceExpression([
t.assignmentExpression("=", t.cloneNode(ref), path.node),
buildDisplayNameAssignment(ref, id),
t.cloneNode(ref),
]),
);
if (safe) {
props.unshift(
t.objectProperty(t.identifier("displayName"), t.stringLiteral(id)),
);
}
}
}

const createContextVisited = new WeakSet();

export default declare(api => {
api.assertVersion(7);

const isCreateClassCallExpression =
t.buildMatchMemberExpression("React.createClass");
Expand Down Expand Up @@ -154,27 +66,44 @@ export default declare(api => {
displayName = path.basename(path.dirname(filename));
}

addDisplayNameInCreateClass(displayName, node.declaration);
addDisplayName(displayName, node.declaration);
}
},

CallExpression(path) {
const { node } = path;
if (isCreateClass(node)) {
const id = getDisplayNameReferenceIdentifier(path);
if (id) {
addDisplayNameInCreateClass(id.name, node);
}
} else if (isCreateContext(node)) {
if (createContextVisited.has(node)) {
return;
if (!isCreateClass(node)) return;

let id;

// crawl up the ancestry looking for possible candidates for displayName inference
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()) {
// we've hit a statement, we should stop crawling up
return true;
}
createContextVisited.add(node);
const id = getDisplayNameReferenceIdentifier(path);

if (id) {
addDisplayNameAfterCreateContext(id.name, path);
}
// we've got an id! no need to continue
if (id) return true;
});

// ensure that we have an identifier we can inherit from
if (!id) return;

// foo.bar -> bar
if (t.isMemberExpression(id)) {
id = id.property;
}

// identifiers are the only thing we can reliably get a name from
if (t.isIdentifier(id)) {
addDisplayName(id.name, node);
}
},
},
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit da2168e

Please sign in to comment.