deno.land / x / jotai@v1.8.4 / src / babel / plugin-debug-label.ts

plugin-debug-label.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import path from 'path'import babel, { PluginObj } from '@babel/core'import _templateBuilder from '@babel/template'import { isAtom } from './utils'
const templateBuilder = (_templateBuilder as any).default || _templateBuilder
export default function debugLabelPlugin({ types: t,}: typeof babel): PluginObj { 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))
// ./{module name}/index.js if (displayName === 'index') { displayName = path.basename(path.dirname(filename)) } // Relies on visiting the variable declaration to add the debugLabel const buildExport = templateBuilder(` const %%atomIdentifier%% = %%atom%%; export default %%atomIdentifier%% `) const ast = buildExport({ atomIdentifier: t.identifier(displayName), atom: node.declaration, }) nodePath.replaceWithMultiple(ast as babel.Node[]) } }, VariableDeclarator(path) { if ( t.isIdentifier(path.node.id) && t.isCallExpression(path.node.init) && isAtom(t, path.node.init.callee) ) { path.parentPath.insertAfter( t.expressionStatement( t.assignmentExpression( '=', t.memberExpression( t.identifier(path.node.id.name), t.identifier('debugLabel') ), t.stringLiteral(path.node.id.name) ) ) ) } }, }, }}
jotai

Version Info

Tagged at
2 years ago