Skip adding __source if it already exists (#3504)

This commit is contained in:
Alexander Kotliarskyi
2016-05-16 13:45:33 -07:00
committed by Henry Zhu
parent cdb465a834
commit 8fa1174b32
2 changed files with 26 additions and 6 deletions

View File

@@ -28,6 +28,22 @@ export default function ({ types: t }) {
let visitor = {
JSXOpeningElement(path, state) {
const id = t.jSXIdentifier(TRACE_ID);
const location = path.container.openingElement.loc;
if (!location) {
// the element was generated and doesn't have location information
return;
}
const attributes = path.container.openingElement.attributes;
for (let i = 0; i < attributes.length; i++) {
const name = attributes[i].name;
if (name && name.name === TRACE_ID) {
// The __source attibute already exists
return;
}
}
if (!state.fileNameIdentifier) {
const fileName = state.file.log.filename !== "unknown"
? state.file.log.filename
@@ -38,12 +54,8 @@ export default function ({ types: t }) {
state.fileNameIdentifier = fileNameIdentifier;
}
const id = t.jSXIdentifier(TRACE_ID);
const location = path.container.openingElement.loc; // undefined for generated elements
if (location) {
const trace = makeTrace(state.fileNameIdentifier, location.start.line);
path.container.openingElement.attributes.push(t.jSXAttribute(id, t.jSXExpressionContainer(trace)));
}
const trace = makeTrace(state.fileNameIdentifier, location.start.line);
attributes.push(t.jSXAttribute(id, t.jSXExpressionContainer(trace)));
}
};