Skip to content

Commit 2ae2a45

Browse files
committed
potential fix
1 parent 7d3e6ff commit 2ae2a45

3 files changed

Lines changed: 73 additions & 22 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Button } from './button'
22

33
export const ButtonWithProps = (props: { children: React.ReactNode }) => {
4-
return <Button children={props.children} />
4+
return <Button {...props} />
55
}

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

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const transformJSX = (
6161
const line = loc.start.line
6262
const column = loc.start.column
6363

64-
// Check if props are spread and element name starts with lowercase
64+
// Check if props are spread
6565
const hasSpread = element.node.attributes.some(
6666
(attr) =>
6767
attr.type === 'JSXSpreadAttribute' &&
@@ -70,31 +70,54 @@ const transformJSX = (
7070
)
7171

7272
if (hasSpread) {
73-
// Do not inject if props are spread and element is lowercase (native HTML)
73+
// Do not inject if props are spread
7474
return
7575
}
76-
if (propsName) {
77-
// inject data-source either via props or default to a string "<file>:<line>:<column>"
78-
// Inject data-source via props
79-
element.node.attributes.push(
80-
t.jsxAttribute(
81-
t.jsxIdentifier('data-tsd-source'),
82-
t.jsxExpressionContainer(
83-
t.logicalExpression(
84-
'??',
85-
t.memberExpression(
86-
t.identifier(propsName),
87-
t.stringLiteral('data-tsd-source'),
88-
true,
76+
77+
/* const isLowercase = element.node.name.type === "JSXIdentifier" ? element.node.name.name.charAt(0) === element.node.name.name.charAt(0).toLowerCase() : false
78+
// Check if the JSX element is imported from a third-party library
79+
let externalImport = false;
80+
if (element.node.name.type === "JSXIdentifier") {
81+
const jsxName = element.node.name.name;
82+
const program = element.findParent((p) => p.isProgram());
83+
if (program && program.node.type === "Program") {
84+
const importDecl = program.node.body.find(
85+
(stmt) =>
86+
stmt.type === "ImportDeclaration" &&
87+
stmt.specifiers.some(
88+
(spec) =>
89+
(spec.type === "ImportSpecifier" || spec.type === "ImportDefaultSpecifier" ) &&
90+
spec.local.name === jsxName
91+
) &&
92+
!stmt.source.value.startsWith(".") // not a relative import
93+
&& !stmt.source.value.startsWith("@/")
94+
&& !stmt.source.value.startsWith("~")
95+
);
96+
externalImport = !!importDecl;
97+
}
98+
}
99+
if (propsName && !(externalImport)) {
100+
// inject data-source either via props or default to a string "<file>:<line>:<column>"
101+
// Inject data-source via props
102+
element.node.attributes.push(
103+
t.jsxAttribute(
104+
t.jsxIdentifier('data-tsd-source'),
105+
t.jsxExpressionContainer(
106+
t.logicalExpression(
107+
'??',
108+
t.memberExpression(
109+
t.identifier(propsName),
110+
t.stringLiteral('data-tsd-source'),
111+
true,
112+
),
113+
t.stringLiteral(`${file}:${line}:${column}`),
89114
),
90-
t.stringLiteral(`${file}:${line}:${column}`),
91115
),
92116
),
93-
),
94-
)
95-
96-
return true
97-
}
117+
)
118+
119+
return true
120+
} */
98121

99122
// Inject data-source as a string: "<file>:<line>:<column>"
100123
element.node.attributes.push(

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ const removeEmptySpace = (str: string) => {
77

88
describe('inject source', () => {
99
describe('function declarations', () => {
10+
it("should not apply data-tsd-source from parent props if not an external import", () => {
11+
const output = removeEmptySpace(addSourceToJsx(`
12+
function test(props) {
13+
return <Custom children={props.children} />
14+
}
15+
`, "test.tsx").code)
16+
expect(output).toBe(removeEmptySpace(`
17+
function test(props) {
18+
return <Custom children={props.children} />
19+
}
20+
`))
21+
})
22+
it("should apply data-tsd-source from parent props if an external import", () => {
23+
const output = removeEmptySpace(addSourceToJsx(`
24+
25+
import Custom from "external";
26+
27+
function test({...props }) {
28+
return <Custom children={props.children} />
29+
}
30+
`, "test.tsx").code)
31+
expect(output).toBe(removeEmptySpace(`
32+
import Custom from "external";
33+
34+
function test({...props }) {
35+
return <Custom children={props.children} data-tsd-source="test.tsx:6:9" />;
36+
}`))
37+
})
1038
it(' props not destructured', () => {
1139
const output = removeEmptySpace(
1240
addSourceToJsx(

0 commit comments

Comments
 (0)