UNPKG

2.91 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path'), require('@babel/template')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'path', '@babel/template'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["jotaiPlugin-debug-label"] = {}, global.path, global.templateBuilder));
5})(this, (function (exports, path, templateBuilder) { 'use strict';
6
7 function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
9 var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
10 var templateBuilder__default = /*#__PURE__*/_interopDefaultLegacy(templateBuilder);
11
12 function isAtom(t, callee) {
13 if (t.isIdentifier(callee) && atomFunctionNames.includes(callee.name)) {
14 return true;
15 }
16
17 if (t.isMemberExpression(callee)) {
18 var property = callee.property;
19
20 if (t.isIdentifier(property) && atomFunctionNames.includes(property.name)) {
21 return true;
22 }
23 }
24
25 return false;
26 }
27 var atomFunctionNames = ['atom', 'atomFamily', 'atomWithDefault', 'atomWithObservable', 'atomWithReducer', 'atomWithReset', 'atomWithStorage', 'freezeAtom', 'loadable', 'selectAtom', 'splitAtom'];
28
29 function debugLabelPlugin(_ref) {
30 var t = _ref.types;
31 return {
32 visitor: {
33 ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
34 var node = nodePath.node;
35
36 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
37 var filename = state.filename || 'unknown';
38 var displayName = path__default["default"].basename(filename, path__default["default"].extname(filename));
39
40 if (displayName === 'index') {
41 displayName = path__default["default"].basename(path__default["default"].dirname(filename));
42 }
43
44 var buildExport = templateBuilder__default["default"]("\n const %%atomIdentifier%% = %%atom%%;\n export default %%atomIdentifier%%\n ");
45 var ast = buildExport({
46 atomIdentifier: t.identifier(displayName),
47 atom: node.declaration
48 });
49 nodePath.replaceWithMultiple(ast);
50 }
51 },
52 VariableDeclarator: function VariableDeclarator(path) {
53 if (t.isIdentifier(path.node.id) && t.isCallExpression(path.node.init) && isAtom(t, path.node.init.callee)) {
54 path.parentPath.insertAfter(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(path.node.id.name), t.identifier('debugLabel')), t.stringLiteral(path.node.id.name))));
55 }
56 }
57 }
58 };
59 }
60
61 exports["default"] = debugLabelPlugin;
62
63 Object.defineProperty(exports, '__esModule', { value: true });
64
65}));