Skip to content

Commit 197ae2c

Browse files
committed
backfill codemod
1 parent 4a3a968 commit 197ae2c

2 files changed

Lines changed: 83 additions & 12 deletions

File tree

packages/upgrade/src/codemods/__tests__/__fixtures__/transform-protect-to-show.fixtures.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,42 @@ function Component() {
362362
);
363363
}`,
364364
},
365+
{
366+
name: 'Bare Protect defaults to signedIn',
367+
source: `
368+
import { Protect } from "@clerk/react"
369+
370+
function App() {
371+
return (
372+
<Protect>
373+
<Content />
374+
</Protect>
375+
)
376+
}
377+
`,
378+
output: `
379+
import { Show } from "@clerk/react"
380+
381+
function App() {
382+
return (
383+
<Show when="signedIn">
384+
<Content />
385+
</Show>
386+
);
387+
}
388+
`,
389+
},
390+
{
391+
name: 'ProtectProps import rewrites to ShowProps',
392+
source: `
393+
import { ProtectProps } from "@clerk/react";
394+
395+
type Props = ProtectProps;
396+
`,
397+
output: `
398+
import { ShowProps } from "@clerk/react";
399+
400+
type Props = ShowProps;
401+
`,
402+
},
365403
];

packages/upgrade/src/codemods/transform-protect-to-show.cjs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ module.exports = function transformProtectToShow({ source }, { jscodeshift: j })
6767
const root = j(source);
6868
let dirtyFlag = false;
6969
const protectLocalNames = [];
70+
const protectPropsLocalsToRename = [];
7071

7172
const isClientComponent = hasUseClientDirective(root, j);
7273

@@ -87,29 +88,61 @@ module.exports = function transformProtectToShow({ source }, { jscodeshift: j })
8788
return undefined;
8889
}
8990

90-
// Transform imports: Protect → Show
91+
// Transform imports: Protect → Show, ProtectProps → ShowProps
9192
const allPackages = [...CLIENT_ONLY_PACKAGES, ...HYBRID_PACKAGES];
9293
allPackages.forEach(packageName => {
9394
root.find(j.ImportDeclaration, { source: { value: packageName } }).forEach(path => {
9495
const node = path.node;
9596
const specifiers = node.specifiers || [];
9697

9798
specifiers.forEach(spec => {
98-
if (j.ImportSpecifier.check(spec) && spec.imported.name === 'Protect') {
99-
const effectiveLocalName = spec.local ? spec.local.name : spec.imported.name;
100-
spec.imported.name = 'Show';
101-
if (spec.local && spec.local.name === 'Protect') {
102-
spec.local.name = 'Show';
99+
if (j.ImportSpecifier.check(spec)) {
100+
if (spec.imported.name === 'Protect') {
101+
const effectiveLocalName = spec.local ? spec.local.name : spec.imported.name;
102+
spec.imported.name = 'Show';
103+
if (spec.local && spec.local.name === 'Protect') {
104+
spec.local.name = 'Show';
105+
}
106+
if (!protectLocalNames.includes(effectiveLocalName)) {
107+
protectLocalNames.push(effectiveLocalName);
108+
}
109+
dirtyFlag = true;
103110
}
104-
if (!protectLocalNames.includes(effectiveLocalName)) {
105-
protectLocalNames.push(effectiveLocalName);
111+
112+
if (spec.imported.name === 'ProtectProps') {
113+
const effectiveLocalName = spec.local ? spec.local.name : spec.imported.name;
114+
spec.imported.name = 'ShowProps';
115+
if (spec.local && spec.local.name === 'ProtectProps') {
116+
spec.local.name = 'ShowProps';
117+
}
118+
if (effectiveLocalName === 'ProtectProps') {
119+
protectPropsLocalsToRename.push(effectiveLocalName);
120+
}
121+
dirtyFlag = true;
106122
}
107-
dirtyFlag = true;
108123
}
109124
});
110125
});
111126
});
112127

128+
// Rename references to ProtectProps (only when local name was ProtectProps)
129+
if (protectPropsLocalsToRename.length > 0) {
130+
root
131+
.find(j.TSTypeReference, {
132+
typeName: {
133+
type: 'Identifier',
134+
name: 'ProtectProps',
135+
},
136+
})
137+
.forEach(path => {
138+
const typeName = path.node.typeName;
139+
if (j.Identifier.check(typeName) && typeName.name === 'ProtectProps') {
140+
typeName.name = 'ShowProps';
141+
dirtyFlag = true;
142+
}
143+
});
144+
}
145+
113146
// Transform JSX: <Protect ...> → <Show when={...}>
114147
root.find(j.JSXElement).forEach(path => {
115148
const openingElement = path.node.openingElement;
@@ -185,9 +218,9 @@ module.exports = function transformProtectToShow({ source }, { jscodeshift: j })
185218
// Reconstruct attributes with `when` prop
186219
const newAttributes = [];
187220

188-
if (whenValue) {
189-
newAttributes.push(j.jsxAttribute(j.jsxIdentifier('when'), whenValue));
190-
}
221+
const finalWhenValue = whenValue || j.stringLiteral('signedIn');
222+
223+
newAttributes.push(j.jsxAttribute(j.jsxIdentifier('when'), finalWhenValue));
191224

192225
// Add remaining attributes (fallback, etc.)
193226
otherAttributes.forEach(attr => newAttributes.push(attr));

0 commit comments

Comments
 (0)