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