Skip to content

Commit 2a2f9eb

Browse files
authored
fix(devtools-vite): preserve valid syntax when removing parenthesized devtools JSX (#449)
* fix(devtools-vite): preserve valid syntax when removing parenthesized devtools JSX * add changeset
1 parent 45d9b8e commit 2a2f9eb

4 files changed

Lines changed: 45 additions & 5 deletions

File tree

.changeset/small-papers-wink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/devtools-vite': patch
3+
---
4+
5+
Fix invalid syntax generated when removing parenthesized devtools JSX expressions.

packages/devtools-vite/src/ast-utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ export function forEachChild(node: Node, callback: (child: Node) => void) {
4848
/**
4949
* Recursively walk AST nodes, calling `visitor` for each node with a `type`.
5050
*/
51-
export function walk(node: Node, visitor: (node: Node) => void) {
52-
visitor(node)
53-
forEachChild(node, (child) => walk(child, visitor))
51+
export function walk(
52+
node: Node,
53+
visitor: (node: Node, parentNode?: Node) => void,
54+
parentNode?: Node,
55+
) {
56+
visitor(node, parentNode)
57+
forEachChild(node, (child) => walk(child, visitor, node))
5458
}

packages/devtools-vite/src/remove-devtools.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,31 @@ export default function App() {
577577
expect(output!.code).not.toContain('TanStackDevtools')
578578
})
579579
})
580+
581+
test('preserves valid syntax when removing parenthesized devtools return', () => {
582+
const output = removeEmptySpace(
583+
removeDevtools(
584+
`
585+
import { TanStackDevtools } from '@tanstack/react-devtools'
586+
587+
export function DevtoolsProvider() {
588+
return (
589+
<TanStackDevtools />
590+
)
591+
}
592+
`,
593+
'test.tsx',
594+
)!.code,
595+
)
596+
597+
expect(output).toBe(
598+
removeEmptySpace(`
599+
export function DevtoolsProvider() {
600+
return (
601+
null
602+
)
603+
}
604+
`),
605+
)
606+
})
580607
})

packages/devtools-vite/src/remove-devtools.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function removeDevtools(code: string, id: string) {
105105
if (devtoolsNames.size === 0) return
106106

107107
// Pass 2: Find and remove devtools JSX elements, collect plugin references
108-
walk(result.program, (node) => {
108+
walk(result.program, (node, parentNode) => {
109109
if (node.type !== 'JSXElement') return
110110

111111
const opening = node.openingElement
@@ -130,7 +130,11 @@ export function removeDevtools(code: string, id: string) {
130130

131131
let end = node.end
132132
if (code[end] === '\n') end++
133-
s.remove(node.start, end)
133+
if (parentNode?.type === 'ParenthesizedExpression') {
134+
s.overwrite(node.start, end, 'null')
135+
} else {
136+
s.remove(node.start, end)
137+
}
134138
})
135139

136140
// Pass 3: Remove plugin imports that are no longer referenced

0 commit comments

Comments
 (0)