@metagptx/vite-plugin-source-locator
Version:
A Vite plugin for source code location tracking
1 lines • 8 kB
Source Map (JSON)
{"version":3,"file":"rn.mjs","sources":["../src/rn.ts"],"sourcesContent":["import fPath from 'path';\nimport type { NodePath } from '@babel/traverse';\nimport * as t from '@babel/types';\nimport {\n createJsxLocatorAttributes,\n dataAttributeNameToDataSetKey,\n getJSXTagName,\n hasInjectedDataSetAttribute,\n isFragmentElement,\n isSvgChild,\n shouldInjectTag,\n} from './jsx-locator';\nimport type { PluginOptions, SourceLocatorAttributes } from './type';\nimport { findWorkspaceRoot, isIncludedFile } from './utils';\n\nconst PREFIX = 'locator';\nconst DEFAULT_INCLUDE = ['**/*.{jsx,tsx,js,ts}'];\n\nconst createObjectProperty = (key: string, value: string) => {\n const objectKey = t.isValidIdentifier(key) ? t.identifier(key) : t.stringLiteral(key);\n return t.objectProperty(objectKey, t.stringLiteral(value));\n};\n\nconst createDataSetProperties = (attrs: SourceLocatorAttributes) => {\n return Object.entries(attrs).map(([key, value]) => {\n return createObjectProperty(dataAttributeNameToDataSetKey(key), value);\n });\n};\n\nconst insertObjectPropertiesBeforeSpread = (expression: t.ObjectExpression, properties: t.ObjectProperty[]) => {\n const spreadIndex = expression.properties.findIndex((property) => t.isSpreadElement(property));\n\n if (spreadIndex === -1) {\n expression.properties.push(...properties);\n } else {\n expression.properties.splice(spreadIndex, 0, ...properties);\n }\n};\n\nconst mergeDataSetAttribute = (attribute: t.JSXAttribute, properties: t.ObjectProperty[]) => {\n if (attribute.value && t.isJSXExpressionContainer(attribute.value)) {\n if (t.isObjectExpression(attribute.value.expression)) {\n insertObjectPropertiesBeforeSpread(attribute.value.expression, properties);\n return;\n }\n\n attribute.value.expression = t.objectExpression([...properties, t.spreadElement(attribute.value.expression)]);\n return;\n }\n\n attribute.value = t.jsxExpressionContainer(t.objectExpression(properties));\n};\n\nexport function rnSourceLocator(options: PluginOptions = {}) {\n const { include = DEFAULT_INCLUDE, exclude = [], prefix = PREFIX, enable = true, attributes, filterTag } = options;\n\n return function sourceLocatorRnPlugin() {\n return {\n name: '@metagptx/source-locator-rn',\n visitor: {\n JSXOpeningElement(path: NodePath<t.JSXOpeningElement>, state: { filename?: string; file?: { code?: string; opts?: { filename?: string } } }) {\n if (!enable) return;\n\n const filename = state.filename || state.file?.opts?.filename;\n const code = state.file?.code;\n if (!filename || !code) return;\n\n const relativePathFromCwd = fPath.relative(process.cwd(), filename);\n if (!isIncludedFile(relativePathFromCwd, include, exclude)) return;\n if (hasInjectedDataSetAttribute(path, prefix)) return;\n if (isFragmentElement(path)) return;\n if (isSvgChild(path)) return;\n\n const tagName = getJSXTagName(path.node.name);\n if (!shouldInjectTag(tagName, filterTag)) return;\n\n const projectRoot = findWorkspaceRoot(process.cwd());\n const relativePath = fPath.relative(projectRoot, filename);\n const attrResult = createJsxLocatorAttributes({\n code,\n path,\n prefix,\n relativePath,\n attributes,\n });\n if (!attrResult) return;\n\n const dataSetProperties = createDataSetProperties(attrResult.attrs);\n const dataSetAttribute = path.node.attributes.find((attribute) => {\n return t.isJSXAttribute(attribute) && t.isJSXIdentifier(attribute.name) && attribute.name.name === 'dataSet';\n });\n\n if (dataSetAttribute && t.isJSXAttribute(dataSetAttribute)) {\n mergeDataSetAttribute(dataSetAttribute, dataSetProperties);\n return;\n }\n\n const spreadIndex = path.node.attributes.findIndex((attribute) => t.isJSXSpreadAttribute(attribute));\n const newDataSetAttribute = t.jsxAttribute(\n t.jsxIdentifier('dataSet'),\n t.jsxExpressionContainer(t.objectExpression(dataSetProperties))\n );\n\n if (spreadIndex === -1) {\n path.node.attributes.push(newDataSetAttribute);\n } else {\n path.node.attributes.splice(spreadIndex, 0, newDataSetAttribute);\n }\n },\n },\n };\n };\n}\n"],"names":["_optionalChain","ops","lastAccessLHS","value","i","length","op","fn","args","call","undefined","PREFIX","DEFAULT_INCLUDE","createDataSetProperties","attrs","Object","entries","map","key","objectKey","t","isValidIdentifier","identifier","stringLiteral","objectProperty","createObjectProperty","dataAttributeNameToDataSetKey","mergeDataSetAttribute","attribute","properties","isJSXExpressionContainer","isObjectExpression","expression","spreadIndex","findIndex","property","isSpreadElement","push","splice","insertObjectPropertiesBeforeSpread","objectExpression","spreadElement","jsxExpressionContainer","rnSourceLocator","options","include","exclude","prefix","enable","attributes","filterTag","name","visitor","JSXOpeningElement","path","state","filename","_","file","_2","opts","code","_4","relativePathFromCwd","fPath","relative","process","cwd","isIncludedFile","hasInjectedDataSetAttribute","isFragmentElement","isSvgChild","tagName","getJSXTagName","node","shouldInjectTag","projectRoot","findWorkspaceRoot","relativePath","attrResult","createJsxLocatorAttributes","dataSetProperties","dataSetAttribute","find","isJSXAttribute","isJSXIdentifier","isJSXSpreadAttribute","newDataSetAttribute","jsxAttribute","jsxIdentifier"],"mappings":";;;;iKAAA,SAAAA,EAAAC,GAAA,IAAAC,EAAAC,EAAAF,EAAA,GAAAG,EAAA,EAAA,KAAAA,EAAAH,EAAAI,QAAA,CAAA,MAAAC,EAAAL,EAAAG,GAAAG,EAAAN,EAAAG,EAAA,GAAA,GAAAA,GAAA,GAAA,mBAAAE,GAAA,iBAAAA,IAAA,MAAAH,EAAA,OAAA,WAAAG,GAAA,mBAAAA,GAAAJ,EAAAC,EAAAA,EAAAI,EAAAJ,IAAA,SAAAG,GAAA,iBAAAA,IAAAH,EAAAI,EAAA,IAAAC,IAAAL,EAAAM,KAAAP,KAAAM,IAAAN,OAAAQ,EAAA,CAAA,OAAAP,CAAA,CAeA,MAAMQ,EAAS,UACTC,EAAkB,CAAC,wBAOnBC,EAA2BC,GACxBC,OAAOC,QAAQF,GAAOG,IAAI,EAAEC,EAAKf,KANb,EAACe,EAAaf,KACzC,MAAMgB,EAAYC,EAAEC,kBAAkBH,GAAOE,EAAEE,WAAWJ,GAAOE,EAAEG,cAAcL,GACjF,OAAOE,EAAEI,eAAeL,EAAWC,EAAEG,cAAcpB,KAK1CsB,CAAqBC,EAA8BR,GAAMf,IAc9DwB,EAAwB,CAACC,EAA2BC,KACxD,GAAID,E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