UNPKG

2.48 kBJavaScriptView Raw
1System.register(['path', '@babel/template'], (function (exports) {
2 'use strict';
3 var path, templateBuilder;
4 return {
5 setters: [function (module) {
6 path = module["default"];
7 }, function (module) {
8 templateBuilder = module["default"];
9 }],
10 execute: (function () {
11
12 exports('default', debugLabelPlugin);
13
14 function isAtom(t, callee) {
15 if (t.isIdentifier(callee) && atomFunctionNames.includes(callee.name)) {
16 return true;
17 }
18 if (t.isMemberExpression(callee)) {
19 const { property } = callee;
20 if (t.isIdentifier(property) && atomFunctionNames.includes(property.name)) {
21 return true;
22 }
23 }
24 return false;
25 }
26 const atomFunctionNames = [
27 "atom",
28 "atomFamily",
29 "atomWithDefault",
30 "atomWithObservable",
31 "atomWithReducer",
32 "atomWithReset",
33 "atomWithStorage",
34 "freezeAtom",
35 "loadable",
36 "selectAtom",
37 "splitAtom"
38 ];
39
40 function debugLabelPlugin({
41 types: t
42 }) {
43 return {
44 visitor: {
45 ExportDefaultDeclaration(nodePath, state) {
46 const { node } = nodePath;
47 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
48 const filename = state.filename || "unknown";
49 let displayName = path.basename(filename, path.extname(filename));
50 if (displayName === "index") {
51 displayName = path.basename(path.dirname(filename));
52 }
53 const buildExport = templateBuilder(`
54 const %%atomIdentifier%% = %%atom%%;
55 export default %%atomIdentifier%%
56 `);
57 const ast = buildExport({
58 atomIdentifier: t.identifier(displayName),
59 atom: node.declaration
60 });
61 nodePath.replaceWithMultiple(ast);
62 }
63 },
64 VariableDeclarator(path2) {
65 if (t.isIdentifier(path2.node.id) && t.isCallExpression(path2.node.init) && isAtom(t, path2.node.init.callee)) {
66 path2.parentPath.insertAfter(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(t.identifier(path2.node.id.name), t.identifier("debugLabel")), t.stringLiteral(path2.node.id.name))));
67 }
68 }
69 }
70 };
71 }
72
73 })
74 };
75}));