Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-papers-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/devtools-vite': patch
---

Fix invalid syntax generated when removing parenthesized devtools JSX expressions.
10 changes: 7 additions & 3 deletions packages/devtools-vite/src/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export function forEachChild(node: Node, callback: (child: Node) => void) {
/**
* Recursively walk AST nodes, calling `visitor` for each node with a `type`.
*/
export function walk(node: Node, visitor: (node: Node) => void) {
visitor(node)
forEachChild(node, (child) => walk(child, visitor))
export function walk(
node: Node,
visitor: (node: Node, parentNode?: Node) => void,
parentNode?: Node,
) {
visitor(node, parentNode)
forEachChild(node, (child) => walk(child, visitor, node))
}
27 changes: 27 additions & 0 deletions packages/devtools-vite/src/remove-devtools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,31 @@ export default function App() {
expect(output!.code).not.toContain('TanStackDevtools')
})
})

test('preserves valid syntax when removing parenthesized devtools return', () => {
const output = removeEmptySpace(
removeDevtools(
`
import { TanStackDevtools } from '@tanstack/react-devtools'

export function DevtoolsProvider() {
return (
<TanStackDevtools />
)
}
`,
'test.tsx',
)!.code,
)

expect(output).toBe(
removeEmptySpace(`
export function DevtoolsProvider() {
return (
null
)
}
`),
)
})
})
8 changes: 6 additions & 2 deletions packages/devtools-vite/src/remove-devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function removeDevtools(code: string, id: string) {
if (devtoolsNames.size === 0) return

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

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

let end = node.end
if (code[end] === '\n') end++
s.remove(node.start, end)
if (parentNode?.type === 'ParenthesizedExpression') {
s.overwrite(node.start, end, 'null')
} else {
s.remove(node.start, end)
}
})

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