Skip to content

Commit 2d15807

Browse files
Fix/fragment transform (#73)
Fixes #72 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 2145027 commit 2d15807

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

.changeset/khaki-lions-join.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 issue with ast transform

packages/devtools-vite/src/inject-source.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ const getPropsNameFromFunctionDeclaration = (
9494
return propsName
9595
}
9696

97+
const getNameOfElement = (
98+
element: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName,
99+
): string => {
100+
if (element.type === 'JSXIdentifier') {
101+
return element.name
102+
}
103+
if (element.type === 'JSXMemberExpression') {
104+
return `${getNameOfElement(element.object)}.${getNameOfElement(element.property)}`
105+
}
106+
107+
return `${element.namespace.name}:${element.name.name}`
108+
}
109+
97110
const transformJSX = (
98111
element: NodePath<t.JSXOpeningElement>,
99112
propsName: string | null,
@@ -103,7 +116,11 @@ const transformJSX = (
103116
if (!loc) return
104117
const line = loc.start.line
105118
const column = loc.start.column
119+
const nameOfElement = getNameOfElement(element.node.name)
106120

121+
if (nameOfElement === 'Fragment' || nameOfElement === 'React.Fragment') {
122+
return
123+
}
107124
const hasDataSource = element.node.attributes.some(
108125
(attr) =>
109126
attr.type === 'JSXAttribute' &&

packages/devtools-vite/tests/inject-source.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,64 @@ const removeEmptySpace = (str: string) => {
66
}
77

88
describe('inject source', () => {
9+
it("shouldn't augment react fragments", () => {
10+
const output = removeEmptySpace(
11+
addSourceToJsx(
12+
`
13+
export const Route = createFileRoute("/test")({
14+
component: function() { return <>Hello World</> },
15+
})
16+
`,
17+
'test.jsx',
18+
).code,
19+
)
20+
expect(output).toBe(
21+
removeEmptySpace(`
22+
export const Route = createFileRoute("/test")({
23+
component: function() { return <>Hello World</> },
24+
})
25+
`),
26+
)
27+
})
28+
29+
it("shouldn't augment react fragments if they start with Fragment ", () => {
30+
const output = removeEmptySpace(
31+
addSourceToJsx(
32+
`
33+
export const Route = createFileRoute("/test")({
34+
component: function() { return <Fragment>Hello World</Fragment> },
35+
})
36+
`,
37+
'test.jsx',
38+
).code,
39+
)
40+
expect(output).toBe(
41+
removeEmptySpace(`
42+
export const Route = createFileRoute("/test")({
43+
component: function() { return <Fragment>Hello World</Fragment> },
44+
})
45+
`),
46+
)
47+
})
48+
it("shouldn't augment react fragments if they start with React.Fragment ", () => {
49+
const output = removeEmptySpace(
50+
addSourceToJsx(
51+
`
52+
export const Route = createFileRoute("/test")({
53+
component: function() { return <React.Fragment>Hello World</React.Fragment> },
54+
})
55+
`,
56+
'test.jsx',
57+
).code,
58+
)
59+
expect(output).toBe(
60+
removeEmptySpace(`
61+
export const Route = createFileRoute("/test")({
62+
component: function() { return <React.Fragment>Hello World</React.Fragment> },
63+
})
64+
`),
65+
)
66+
})
967
describe('FunctionExpression', () => {
1068
it('should work with deeply nested custom JSX syntax', () => {
1169
const output = removeEmptySpace(

0 commit comments

Comments
 (0)