UNPKG

4.33 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nimport { ConfigAPI, types as t, NodePath, template } from '@babel/core'\n\nexport interface Attribute {\n name: string\n value?: boolean | number | string | null\n spread?: boolean\n literal?: boolean\n position?: 'start' | 'end'\n}\n\nexport interface Options {\n elements: string[]\n attributes: Attribute[]\n}\n\nconst positionMethod = {\n start: 'unshiftContainer',\n end: 'pushContainer',\n} as const\n\nconst addJSXAttribute = (_: ConfigAPI, opts: Options) => {\n function getAttributeValue({\n literal,\n value,\n }: {\n literal?: Attribute['literal']\n value: Attribute['value']\n }) {\n if (typeof value === 'boolean') {\n return t.jsxExpressionContainer(t.booleanLiteral(value))\n }\n\n if (typeof value === 'number') {\n return t.jsxExpressionContainer(t.numericLiteral(value))\n }\n\n if (typeof value === 'string' && literal) {\n return t.jsxExpressionContainer(\n (template.ast(value) as t.ExpressionStatement).expression,\n )\n }\n\n if (typeof value === 'string') {\n return t.stringLiteral(value)\n }\n\n return null\n }\n\n function getAttribute({ spread, name, value, literal }: Attribute) {\n if (spread) {\n return t.jsxSpreadAttribute(t.identifier(name))\n }\n\n return t.jsxAttribute(\n t.jsxIdentifier(name),\n getAttributeValue({ value, literal }),\n )\n }\n\n return {\n visitor: {\n JSXOpeningElement(path: NodePath<t.JSXOpeningElement>) {\n if (!t.isJSXIdentifier(path.node.name)) return\n if (!opts.elements.includes(path.node.name.name)) return\n\n opts.attributes.forEach(\n ({\n name,\n value = null,\n spread = false,\n literal = false,\n position = 'end',\n }) => {\n const method = positionMethod[position]\n const newAttribute = getAttribute({ spread, name, value, literal })\n const attributes = path.get('attributes')\n\n const isEqualAttribute = (\n attribute: NodePath<t.JSXSpreadAttribute | t.JSXAttribute>,\n ) => {\n if (spread)\n return (\n attribute.isJSXSpreadAttribute() &&\n attribute.get('argument').isIdentifier({ name })\n )\n return (\n attribute.isJSXAttribute() &&\n attribute.get('name').isJSXIdentifier({ name })\n )\n }\n\n const replaced = attributes.some((attribute) => {\n if (!isEqualAttribute(attribute)) return false\n attribute.replaceWith(newAttribute)\n return true\n })\n\n if (!replaced) {\n path[method]('attributes', newAttribute)\n }\n },\n )\n },\n },\n }\n}\n\nexport default addJSXAttribute\n"],"names":["t","template"],"mappings":";;;;AAgBA,MAAM,iBAAiB;AAAA,EACrB,OAAO;AAAA,EACP,KAAK;AAAA;MAGD,kBAAkB,CAAC,GAAc,SAAkB;AACvD,6BAA2B;AAAA,IACzB;AAAA,IACA;AAAA,KAIC;AACD,QAAI,OAAO,UAAU,WAAW;AAC9B,aAAOA,WAAE,uBAAuBA,WAAE,eAAe;AAAA;AAGnD,QAAI,OAAO,UAAU,UAAU;AAC7B,aAAOA,WAAE,uBAAuBA,WAAE,eAAe;AAAA;AAGnD,QAAI,OAAO,UAAU,YAAY,SAAS;AACxC,aAAOA,WAAE,uBACNC,cAAS,IAAI,OAAiC;AAAA;AAInD,QAAI,OAAO,UAAU,UAAU;AAC7B,aAAOD,WAAE,cAAc;AAAA;AAGzB,WAAO;AAAA;AAGT,wBAAsB,EAAE,QAAQ,MAAM,OAAO,WAAsB;AACjE,QAAI,QAAQ;AACV,aAAOA,WAAE,mBAAmBA,WAAE,WAAW;AAAA;AAG3C,WAAOA,WAAE,aACPA,WAAE,cAAc,OAChB,kBAAkB,EAAE,OAAO;AAAA;AAI/B,SAAO;AAAA,IACL,SAAS;AAAA,MACP,kBAAkB,MAAqC;AACrD,YAAI,CAACA,WAAE,gBAAgB,KAAK,KAAK;AAAO;AACxC,YAAI,CAAC,KAAK,SAAS,SAAS,KAAK,KAAK,KAAK;AAAO;AAElD,aAAK,WAAW,QACd,CAAC;AAAA,UACC;AAAA,UACA,QAAQ;AAAA,UACR,SAAS;AAAA,UACT,UAAU;AAAA,UACV,WAAW;AAAA,cACP;AACJ,gBAAM,SAAS,eAAe;AAC9B,gBAAM,eAAe,aAAa,EAAE,QAAQ,MAAM,OAAO;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