1 | 'use strict';
|
2 |
|
3 | var core = require('@babel/core');
|
4 |
|
5 | const elements = ["svg", "Svg"];
|
6 | const createTagElement = (tag, children = [], attributes = []) => {
|
7 | const eleName = core.types.jsxIdentifier(tag);
|
8 | return core.types.jsxElement(core.types.jsxOpeningElement(eleName, attributes), core.types.jsxClosingElement(eleName), children);
|
9 | };
|
10 | const createTagIdAttribute = (tag) => core.types.jsxAttribute(core.types.jsxIdentifier("id"), core.types.jsxExpressionContainer(core.types.identifier(`${tag}Id`)));
|
11 | const addTagIdAttribute = (tag, attributes) => {
|
12 | const existingId = attributes.find((attribute) => core.types.isJSXAttribute(attribute) && attribute.name.name === "id");
|
13 | if (!existingId) {
|
14 | return [...attributes, createTagIdAttribute(tag)];
|
15 | }
|
16 | existingId.value = core.types.jsxExpressionContainer(core.types.isStringLiteral(existingId.value) ? core.types.logicalExpression("||", core.types.identifier(`${tag}Id`), existingId.value) : core.types.identifier(`${tag}Id`));
|
17 | return attributes;
|
18 | };
|
19 | const plugin = () => ({
|
20 | visitor: {
|
21 | JSXElement(path, state) {
|
22 | const tag = state.opts.tag || "title";
|
23 | if (!elements.length)
|
24 | return;
|
25 | const openingElement = path.get("openingElement");
|
26 | const openingElementName = openingElement.get("name");
|
27 | if (!elements.some((element) => openingElementName.isJSXIdentifier({ name: element }))) {
|
28 | return;
|
29 | }
|
30 | const getTagElement = (existingTitle) => {
|
31 | var _a;
|
32 | const tagExpression = core.types.identifier(tag);
|
33 | if (existingTitle) {
|
34 | existingTitle.openingElement.attributes = addTagIdAttribute(tag, existingTitle.openingElement.attributes);
|
35 | }
|
36 | const conditionalTitle = core.types.conditionalExpression(tagExpression, createTagElement(tag, [core.types.jsxExpressionContainer(tagExpression)], existingTitle ? existingTitle.openingElement.attributes : [createTagIdAttribute(tag)]), core.types.nullLiteral());
|
37 | if ((_a = existingTitle == null ? void 0 : existingTitle.children) == null ? void 0 : _a.length) {
|
38 | return core.types.jsxExpressionContainer(core.types.conditionalExpression(core.types.binaryExpression("===", tagExpression, core.types.identifier("undefined")), existingTitle, conditionalTitle));
|
39 | }
|
40 | return core.types.jsxExpressionContainer(conditionalTitle);
|
41 | };
|
42 | let tagElement = null;
|
43 | const hasTitle = path.get("children").some((childPath) => {
|
44 | if (childPath.node === tagElement)
|
45 | return false;
|
46 | if (!childPath.isJSXElement())
|
47 | return false;
|
48 | const name = childPath.get("openingElement").get("name");
|
49 | if (!name.isJSXIdentifier())
|
50 | return false;
|
51 | if (name.node.name !== tag)
|
52 | return false;
|
53 | tagElement = getTagElement(childPath.node);
|
54 | childPath.replaceWith(tagElement);
|
55 | return true;
|
56 | });
|
57 | tagElement = tagElement || getTagElement();
|
58 | if (!hasTitle) {
|
59 | path.node.children.unshift(tagElement);
|
60 | path.replaceWith(path.node);
|
61 | }
|
62 | }
|
63 | }
|
64 | });
|
65 |
|
66 | module.exports = plugin;
|
67 |
|