Skip to content

Commit

Permalink
Add support for debugLabels for atom utils (pmndrs#1051)
Browse files Browse the repository at this point in the history
* Add support for debugLabels for atom utils

* use common isAtom util
  • Loading branch information
Thisen authored Mar 9, 2022
1 parent cbcb131 commit baf5918
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
20 changes: 2 additions & 18 deletions src/babel/plugin-debug-label.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import babel, { PluginObj, types } from '@babel/core'
import babel, { PluginObj } from '@babel/core'
import templateBuilder from '@babel/template'
import { isAtom } from './utils'

export default function debugLabelPlugin({
types: t,
Expand Down Expand Up @@ -56,20 +57,3 @@ export default function debugLabelPlugin({
},
}
}

function isAtom(
t: typeof types,
callee: babel.types.Expression | babel.types.V8IntrinsicIdentifier
) {
if (t.isIdentifier(callee) && callee.name === 'atom') {
return true
}

if (t.isMemberExpression(callee)) {
const { property } = callee
if (t.isIdentifier(property) && property.name === 'atom') {
return true
}
}
return false
}
18 changes: 16 additions & 2 deletions src/babel/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@ export function isAtom(
t: typeof types,
callee: babel.types.Expression | babel.types.V8IntrinsicIdentifier
) {
if (t.isIdentifier(callee) && callee.name === 'atom') {
if (t.isIdentifier(callee) && atomFunctionNames.includes(callee.name)) {
return true
}

if (t.isMemberExpression(callee)) {
const { property } = callee
if (t.isIdentifier(property) && property.name === 'atom') {
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',
]
59 changes: 59 additions & 0 deletions tests/babel/plugin-debug-label.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,62 @@ it('Should handle all types of exports', () => {
export default atoms;"
`)
})

it('Should handle all atom types', () => {
expect(
transform(
`
export const countAtom = atom(0);
const myFamily = atomFamily((param) => atom(param));
const countAtomWithDefault = atomWithDefault((get) => get(countAtom) * 2);
const observableAtom = atomWithObservable(() => {});
const reducerAtom = atomWithReducer(0, () => {});
const resetAtom = atomWithReset(0);
const storageAtom = atomWithStorage('count', 1);
const freezedAtom = freezeAtom(atom({ count: 0 }));
const loadedAtom = loadable(countAtom);
const selectedValueAtom = selectAtom(atom({ a: 0, b: 'othervalue' }), (v) => v.a);
const splittedAtom = splitAtom(atom([]));
`,
'atoms/index.ts'
)
).toMatchInlineSnapshot(`
"export const countAtom = atom(0);
countAtom.debugLabel = \\"countAtom\\";
const myFamily = atomFamily(param => atom(param));
myFamily.debugLabel = \\"myFamily\\";
const countAtomWithDefault = atomWithDefault(get => get(countAtom) * 2);
countAtomWithDefault.debugLabel = \\"countAtomWithDefault\\";
const observableAtom = atomWithObservable(() => {});
observableAtom.debugLabel = \\"observableAtom\\";
const reducerAtom = atomWithReducer(0, () => {});
reducerAtom.debugLabel = \\"reducerAtom\\";
const resetAtom = atomWithReset(0);
resetAtom.debugLabel = \\"resetAtom\\";
const storageAtom = atomWithStorage('count', 1);
storageAtom.debugLabel = \\"storageAtom\\";
const freezedAtom = freezeAtom(atom({
count: 0
}));
freezedAtom.debugLabel = \\"freezedAtom\\";
const loadedAtom = loadable(countAtom);
loadedAtom.debugLabel = \\"loadedAtom\\";
const selectedValueAtom = selectAtom(atom({
a: 0,
b: 'othervalue'
}), v => v.a);
selectedValueAtom.debugLabel = \\"selectedValueAtom\\";
const splittedAtom = splitAtom(atom([]));
splittedAtom.debugLabel = \\"splittedAtom\\";"
`)
})

0 comments on commit baf5918

Please sign in to comment.