/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/babel-plugin-transform-react-display-name/src/index.ts
126 строк
3 KB
Huáng Jùnliàng
chore: inline REQUIRED_VERSION(7) special case (#17897)
17 апр 2026, 16:26
Не верифицирован
17 апр 2026, 16:26
3b1954b
Код
Авторство
О чём код?
import { declare } from "@babel/helper-plugin-utils"; import path from "node:path"; import { types as t } from "@babel/core"; type ReactCreateClassCall = t.CallExpression & { arguments: [t.ObjectExpression]; }; export default declare(api => { api.assertVersion(REQUIRED_VERSION("^7.0.0-0 || ^8.0.0")); function addDisplayName(id: string, call: ReactCreateClassCall) { const props = call.arguments[0].properties; let safe = true; for (let i = 0; i < props.length; i++) { const prop = props[i]; if (t.isSpreadElement(prop)) { continue; } const key = t.toComputedKey(prop); if (t.isStringLiteral(key, { value: "displayName" })) { safe = false; break; } } if (safe) { props.unshift( t.objectProperty(t.identifier("displayName"), t.stringLiteral(id)), ); } } const isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass"); const isCreateClassAddon = (callee: t.CallExpression["callee"]) => t.isIdentifier(callee, { name: "createReactClass" }); function isCreateClass(node?: t.Node): node is ReactCreateClassCall { if (!node || !t.isCallExpression(node)) return false; // not createReactClass nor React.createClass call member object if ( !isCreateClassCallExpression(node.callee) && !isCreateClassAddon(node.callee) ) { return false; } // no call arguments const args = node.arguments; if (args.length !== 1) return false; // first node arg is not an object const first = args[0]; if (!t.isObjectExpression(first)) return false; return true; } return { name: "transform-react-display-name", visitor: { ExportDefaultDeclaration({ node }, state) { if (isCreateClass(node.declaration)) { const filename = state.filename || "unknown"; let displayName = path.basename(filename, path.extname(filename)); // ./{module name}/index.js if (displayName === "index") { displayName = path.basename(path.dirname(filename)); } addDisplayName(displayName, node.declaration); } }, CallExpression(path) { const { node } = path; if (!isCreateClass(node)) return; let id: | t.LVal | t.Expression | t.PrivateName | t.VoidPattern | null | undefined; // 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; } // we've got an id! no need to continue if (id) return true; return false; }); // 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); } }, }, }; });