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 _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
---|
9 | var _core = require("@babel/core");
|
---|
10 | const PURE_CALLS = [["react", new Set(["cloneElement", "createContext", "createElement", "createFactory", "createRef", "forwardRef", "isValidElement", "memo", "lazy"])], ["react-dom", new Set(["createPortal"])]];
|
---|
11 | var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
|
---|
12 | api.assertVersion(7);
|
---|
13 | return {
|
---|
14 | name: "transform-react-pure-annotations",
|
---|
15 | visitor: {
|
---|
16 | CallExpression(path) {
|
---|
17 | if (isReactCall(path)) {
|
---|
18 | (0, _helperAnnotateAsPure.default)(path);
|
---|
19 | }
|
---|
20 | }
|
---|
21 | }
|
---|
22 | };
|
---|
23 | });
|
---|
24 | function isReactCall(path) {
|
---|
25 | const calleePath = path.get("callee");
|
---|
26 | if (!calleePath.isMemberExpression()) {
|
---|
27 | for (const [module, methods] of PURE_CALLS) {
|
---|
28 | for (const method of methods) {
|
---|
29 | if (calleePath.referencesImport(module, method)) {
|
---|
30 | return true;
|
---|
31 | }
|
---|
32 | }
|
---|
33 | }
|
---|
34 | return false;
|
---|
35 | }
|
---|
36 | const object = calleePath.get("object");
|
---|
37 | const callee = calleePath.node;
|
---|
38 | if (!callee.computed && _core.types.isIdentifier(callee.property)) {
|
---|
39 | const propertyName = callee.property.name;
|
---|
40 | for (const [module, methods] of PURE_CALLS) {
|
---|
41 | if (object.referencesImport(module, "default") || object.referencesImport(module, "*")) {
|
---|
42 | return methods.has(propertyName);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | //# sourceMappingURL=index.js.map
|
---|