You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.7 KiB
60 lines
1.7 KiB
module.exports = function addDataLocator({ types: t }) {
|
|
return {
|
|
name: "add-data-locator",
|
|
visitor: {
|
|
JSXOpeningElement(path, state) {
|
|
if (process.env.NODE_ENV === "production") {
|
|
return;
|
|
}
|
|
|
|
const rawFilePath = state.file.opts.filename;
|
|
|
|
if (!rawFilePath || rawFilePath.includes("node_modules")) {
|
|
return;
|
|
}
|
|
|
|
const filePath = rawFilePath.split("?")[0].replace(/\\/g, "/");
|
|
|
|
if (filePath.includes("dev-click-to-component")) {
|
|
return;
|
|
}
|
|
|
|
// Skip React.Fragment / Fragment to avoid invalid prop warnings in React
|
|
if (t.isJSXIdentifier(path.node.name)) {
|
|
if (path.node.name.name === "Fragment") {
|
|
return;
|
|
}
|
|
} else if (t.isJSXMemberExpression(path.node.name)) {
|
|
if (
|
|
t.isJSXIdentifier(path.node.name.property) &&
|
|
path.node.name.property.name === "Fragment"
|
|
) {
|
|
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),
|
|
),
|
|
);
|
|
},
|
|
},
|
|
};
|
|
};
|