UNPKG

1.76 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("path"),require("@babel/template")):"function"==typeof define&&define.amd?define(["path","@babel/template"],t):(e="undefined"!=typeof globalThis?globalThis:e||self)["jotaiPlugin-debug-label"]=t(e.path,e._templateBuilder)}(this,(function(e,t){"use strict";function i(e,t,i){void 0===i&&(i=[]);var n=[].concat(a,i);if(e.isIdentifier(t)&&n.includes(t.name))return!0;if(e.isMemberExpression(t)){var o=t.property;if(e.isIdentifier(o)&&n.includes(o.name))return!0}return!1}var a=["abortableAtom","atom","atomFamily","atomWithDefault","atomWithHash","atomWithImmer","atomWithInfiniteQuery","atomWithMachine","atomWithMutation","atomWithObservable","atomWithProxy","atomWithQuery","atomWithReducer","atomWithReset","atomWithSubscription","atomWithStorage","atomWithStore","freezeAtom","loadable","selectAtom","splitAtom"],n=t.default||t;return function(t,a){var o=t.types;return{visitor:{ExportDefaultDeclaration:function(t,r){var m=t.node;if(o.isCallExpression(m.declaration)&&i(o,m.declaration.callee,null==a?void 0:a.customAtomNames)){var l=r.filename||"unknown",s=e.basename(l,e.extname(l));"index"===s&&(s=e.basename(e.dirname(l)));var d=n("\n const %%atomIdentifier%% = %%atom%%;\n export default %%atomIdentifier%%\n ")({atomIdentifier:o.identifier(s),atom:m.declaration});t.replaceWithMultiple(d)}},VariableDeclarator:function(e){o.isIdentifier(e.node.id)&&o.isCallExpression(e.node.init)&&i(o,e.node.init.callee,null==a?void 0:a.customAtomNames)&&e.parentPath.insertAfter(o.expressionStatement(o.assignmentExpression("=",o.memberExpression(o.identifier(e.node.id.name),o.identifier("debugLabel")),o.stringLiteral(e.node.id.name))))}}}}}));