1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
8 | var _core = require("@babel/core");
|
---|
9 | const TRACE_ID = "__source";
|
---|
10 | const FILE_NAME_VAR = "_jsxFileName";
|
---|
11 | const createNodeFromNullish = (val, fn) => val == null ? _core.types.nullLiteral() : fn(val);
|
---|
12 | var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
|
---|
13 | api.assertVersion(7);
|
---|
14 | function makeTrace(fileNameIdentifier, {
|
---|
15 | line,
|
---|
16 | column
|
---|
17 | }) {
|
---|
18 | const fileLineLiteral = createNodeFromNullish(line, _core.types.numericLiteral);
|
---|
19 | const fileColumnLiteral = createNodeFromNullish(column, c => _core.types.numericLiteral(c + 1));
|
---|
20 | return _core.template.expression.ast`{
|
---|
21 | fileName: ${fileNameIdentifier},
|
---|
22 | lineNumber: ${fileLineLiteral},
|
---|
23 | columnNumber: ${fileColumnLiteral},
|
---|
24 | }`;
|
---|
25 | }
|
---|
26 | const isSourceAttr = attr => _core.types.isJSXAttribute(attr) && attr.name.name === TRACE_ID;
|
---|
27 | return {
|
---|
28 | name: "transform-react-jsx-source",
|
---|
29 | visitor: {
|
---|
30 | JSXOpeningElement(path, state) {
|
---|
31 | const {
|
---|
32 | node
|
---|
33 | } = path;
|
---|
34 | if (!node.loc || path.node.attributes.some(isSourceAttr)) {
|
---|
35 | return;
|
---|
36 | }
|
---|
37 | if (!state.fileNameIdentifier) {
|
---|
38 | const fileNameId = path.scope.generateUidIdentifier(FILE_NAME_VAR);
|
---|
39 | state.fileNameIdentifier = fileNameId;
|
---|
40 | path.scope.getProgramParent().push({
|
---|
41 | id: fileNameId,
|
---|
42 | init: _core.types.stringLiteral(state.filename || "")
|
---|
43 | });
|
---|
44 | }
|
---|
45 | node.attributes.push(_core.types.jsxAttribute(_core.types.jsxIdentifier(TRACE_ID), _core.types.jsxExpressionContainer(makeTrace(_core.types.cloneNode(state.fileNameIdentifier), node.loc.start))));
|
---|
46 | }
|
---|
47 | }
|
---|
48 | };
|
---|
49 | });
|
---|
50 |
|
---|
51 | //# sourceMappingURL=index.js.map
|
---|