/
index.js
66 lines (56 loc) 路 1.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* This adds {fileName, lineNumber} annotations to React component definitions
* and to jsx tag literals.
*
*
* == JSX Literals ==
*
* <sometag />
*
* becomes:
*
* var __jsxFileName = 'this/file.js';
* <sometag __source={{fileName: __jsxFileName, lineNumber: 10}}/>
*/
const TRACE_ID = "__source";
const FILE_NAME_VAR = "_jsxFileName";
export default function ({ types: t }) {
function makeTrace(fileNameIdentifier, lineNumber) {
const fileLineLiteral = lineNumber != null ? t.numericLiteral(lineNumber) : t.nullLiteral();
const fileNameProperty = t.objectProperty(t.identifier("fileName"), fileNameIdentifier);
const lineNumberProperty = t.objectProperty(t.identifier("lineNumber"), fileLineLiteral);
return t.objectExpression([fileNameProperty, lineNumberProperty]);
}
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
: null;
const fileNameIdentifier = path.scope.generateUidIdentifier(FILE_NAME_VAR);
const scope = path.hub.getScope();
scope && scope.push({id: fileNameIdentifier, init: t.stringLiteral(fileName)});
state.fileNameIdentifier = fileNameIdentifier;
}
const trace = makeTrace(state.fileNameIdentifier, location.start.line);
attributes.push(t.jSXAttribute(id, t.jSXExpressionContainer(trace)));
}
};
return {
visitor
};
}