UNPKG

jotai

Version:

👻 Next gen state management that will spook you

64 lines (60 loc) • 1.91 kB
import path from 'path'; import templateBuilder from '@babel/template'; function isAtom(t, callee) { if (t.isIdentifier(callee) && atomFunctionNames.includes(callee.name)) { return true; } if (t.isMemberExpression(callee)) { const { property } = callee; if (t.isIdentifier(property) && atomFunctionNames.includes(property.name)) { return true; } } return false; } const atomFunctionNames = [ "atom", "atomFamily", "atomWithDefault", "atomWithObservable", "atomWithReducer", "atomWithReset", "atomWithStorage", "freezeAtom", "loadable", "selectAtom", "splitAtom" ]; function debugLabelPlugin({ types: t }) { return { visitor: { ExportDefaultDeclaration(nodePath, state) { const { node } = nodePath; if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) { const filename = state.filename || "unknown"; let displayName = path.basename(filename, path.extname(filename)); if (displayName === "index") { displayName = path.basename(path.dirname(filename)); } const buildExport = templateBuilder(` const %%atomIdentifier%% = %%atom%%; export default %%atomIdentifier%% `); const ast = buildExport({ atomIdentifier: t.identifier(displayName), atom: node.declaration }); nodePath.replaceWithMultiple(ast); } }, VariableDeclarator(path2) { if (t.isIdentifier(path2.node.id) && t.isCallExpression(path2.node.init) && isAtom(t, path2.node.init.callee)) { path2.parentPath.insertAfter(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(t.identifier(path2.node.id.name), t.identifier("debugLabel")), t.stringLiteral(path2.node.id.name)))); } } } }; } export { debugLabelPlugin as default };