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.
40 lines
1.1 KiB
40 lines
1.1 KiB
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),
|
|
),
|
|
);
|
|
},
|
|
},
|
|
};
|
|
};
|