module.exports = function addDataLocator({ types: t }) { return { name: "add-data-locator", visitor: { JSXOpeningElement(path, state) { if (process.env.NODE_ENV !== "development") { return; } const filePath = state.file.opts.filename; if (!filePath || filePath.includes("node_modules")) { return; } const attributeExists = path.node.attributes.some( (attribute) => t.isJSXAttribute(attribute) && t.isJSXIdentifier(attribute.name) && attribute.name.name === "data-locator", ); if (attributeExists) { return; } const lineNumber = path.node.loc?.start.line ?? "unknown"; const columnNumber = path.node.loc?.start.column ?? "unknown"; const locatorValue = `${filePath}:${lineNumber}:${columnNumber}`; path.node.attributes.push( t.jsxAttribute( t.jsxIdentifier("data-locator"), t.stringLiteral(locatorValue), ), ); }, }, }; };