UNPKG

1.58 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _helperPluginUtils = require("@babel/helper-plugin-utils");
8var _core = require("@babel/core");
9const TRACE_ID = "__self";
10function getThisFunctionParent(path) {
11 let scope = path.scope;
12 do {
13 const {
14 path
15 } = scope;
16 if (path.isFunctionParent() && !path.isArrowFunctionExpression()) {
17 return path;
18 }
19 } while (scope = scope.parent);
20 return null;
21}
22function isDerivedClass(classPath) {
23 return classPath.node.superClass !== null;
24}
25function isThisAllowed(path) {
26 const parentMethodOrFunction = getThisFunctionParent(path);
27 if (parentMethodOrFunction === null) {
28 return true;
29 }
30 if (!parentMethodOrFunction.isMethod()) {
31 return true;
32 }
33 if (parentMethodOrFunction.node.kind !== "constructor") {
34 return true;
35 }
36 return !isDerivedClass(parentMethodOrFunction.parentPath.parentPath);
37}
38var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
39 api.assertVersion(7);
40 const visitor = {
41 JSXOpeningElement(path) {
42 if (!isThisAllowed(path)) {
43 return;
44 }
45 const node = path.node;
46 const id = _core.types.jsxIdentifier(TRACE_ID);
47 const trace = _core.types.thisExpression();
48 node.attributes.push(_core.types.jsxAttribute(id, _core.types.jsxExpressionContainer(trace)));
49 }
50 };
51 return {
52 name: "transform-react-jsx-self",
53 visitor: {
54 Program(path) {
55 path.traverse(visitor);
56 }
57 }
58 };
59});
60
61//# sourceMappingURL=index.js.map