/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/babel-plugin-transform-react-inline-elements/src/index.ts
74 строки
2 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 helper from "@babel/helper-builder-react-jsx"; import { types as t } from "@babel/core"; export default declare(api => { api.assertVersion(REQUIRED_VERSION("^7.0.0-0 || ^8.0.0")); function hasRefOrSpread(attrs: t.JSXOpeningElement["attributes"]) { for (let i = 0; i < attrs.length; i++) { const attr = attrs[i]; if (t.isJSXSpreadAttribute(attr)) return true; if (isJSXAttributeOfName(attr, "ref")) return true; } return false; } function isJSXAttributeOfName(attr: t.JSXAttribute, name: string) { return ( t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name, { name: name }) ); } const visitor = helper({ filter(node) { return ( node.type === "JSXElement" && !hasRefOrSpread(node.openingElement.attributes) ); }, pre(state) { const tagName = state.tagName; const args = state.args; if (t.react.isCompatTag(tagName)) { args.push(t.stringLiteral(tagName!)); } else { args.push(state.tagExpr); } }, post(state, pass) { state.callee = pass.addHelper("jsx"); // NOTE: The arguments passed to the "jsx" helper are: // (element, props, key, ...children) or (element, props) // The argument generated by the helper are: // (element, { ...props, key }, ...children) const props = state.args[1]; let hasKey = false; if (t.isObjectExpression(props)) { const keyIndex = props.properties.findIndex(prop => // @ts-expect-error todo(flow->ts) key does not exist on SpreadElement t.isIdentifier(prop.key, { name: "key" }), ); if (keyIndex > -1) { // @ts-expect-error todo(flow->ts) value does not exist on ObjectMethod state.args.splice(2, 0, props.properties[keyIndex].value); props.properties.splice(keyIndex, 1); hasKey = true; } } else if (t.isNullLiteral(props)) { state.args.splice(1, 1, t.objectExpression([])); } if (!hasKey && state.args.length > 2) { state.args.splice(2, 0, t.buildUndefinedNode()); } state.pure = true; }, }); return { name: "transform-react-inline-elements", visitor, }; });