diff --git a/.changeset/happy-clubs-wave.md b/.changeset/happy-clubs-wave.md new file mode 100644 index 00000000..2ad297f2 --- /dev/null +++ b/.changeset/happy-clubs-wave.md @@ -0,0 +1,5 @@ +--- +'@tanstack/devtools-vite': minor +--- + +Fix devtools JSX removal logic. diff --git a/packages/devtools-vite/src/remove-devtools.test.ts b/packages/devtools-vite/src/remove-devtools.test.ts index 297fc444..e8b927bb 100644 --- a/packages/devtools-vite/src/remove-devtools.test.ts +++ b/packages/devtools-vite/src/remove-devtools.test.ts @@ -601,6 +601,94 @@ export function DevtoolsProvider() { null ) } +`), + ) + }) + + test('replaces removed devtools expressions with null', () => { + const output = removeEmptySpace( + removeDevtools( + ` +import { TanStackDevtools } from '@tanstack/react-devtools' + +function functionCall(value: unknown) { + return value +} + +export function DevtoolsProvider() { + const devtools1 = + const devtools2 = functionCall() + const devtools3 = true ? : <>fallback + const devtools4 = { + devtools: + } + const devtools5 = (
+ {} +
) + return { devtools1, devtools2, devtools3, devtools4, devtools5 } +} +`, + 'test.tsx', + )!.code, + ) + + expect(output).toBe( + removeEmptySpace(` +function functionCall(value: unknown) { + return value +} + +export function DevtoolsProvider() { + const devtools1 = null + const devtools2 = functionCall(null) + const devtools3 = true ? null : <>fallback + const devtools4 = { + devtools: null + } + const devtools5 = (
+ {null} +
) + return { devtools1, devtools2, devtools3, devtools4, devtools5 } +} +`), + ) + }) + + test('removes devtools jsx children entirely', () => { + const output = removeEmptySpace( + removeDevtools( + ` +import { TanStackDevtools } from '@tanstack/react-devtools' + +export function DevtoolsProvider() { + return ( + <> +
+
before
+ +
after
+
+ + + ) +} +`, + 'test.tsx', + )!.code, + ) + + expect(output).toBe( + removeEmptySpace(` +export function DevtoolsProvider() { + return ( + <> +
+
before
+
after
+
+ + ) +} `), ) }) diff --git a/packages/devtools-vite/src/remove-devtools.ts b/packages/devtools-vite/src/remove-devtools.ts index 26af2972..ba15ad6d 100644 --- a/packages/devtools-vite/src/remove-devtools.ts +++ b/packages/devtools-vite/src/remove-devtools.ts @@ -130,10 +130,17 @@ export function removeDevtools(code: string, id: string) { let end = node.end if (code[end] === '\n') end++ - if (parentNode?.type === 'ParenthesizedExpression') { - s.overwrite(node.start, end, 'null') - } else { + /** + * Devtools nodes can be removed safely when nested in JSX. + * In all other contexts, replace them with `null` to avoid leaving invalid syntax. + */ + if ( + parentNode?.type === 'JSXElement' || + parentNode?.type === 'JSXFragment' + ) { s.remove(node.start, end) + } else { + s.overwrite(node.start, end, 'null') } })