Skip to content

Commit b249484

Browse files
authored
fix(compiler): support components with dot in name (#972)
This pattern is often used in design systems, eg. `<Card.Title>`.
1 parent 6a841b0 commit b249484

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

.changeset/three-monkeys-smile.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@lingo.dev/_compiler": patch
3+
"lingo.dev": patch
4+
---
5+
6+
support components with dot in name

packages/compiler/src/utils/jsx-element.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ describe("JSX Element Utils", () => {
4141
const path = getJSXElementPath("<MyComponent>Hello</MyComponent>");
4242
expect(getJsxElementName(path)).toBe("MyComponent");
4343
});
44+
45+
it("should return element name for elements with dots", () => {
46+
const path = getJSXElementPath("<My.Component>Hello</My.Component>");
47+
expect(getJsxElementName(path)).toBe("My.Component");
48+
});
49+
50+
it("should return element name for elements with multiple dot", () => {
51+
const path = getJSXElementPath(
52+
"<My.Very.Custom.React.Component>Hello</My.Very.Custom.React.Component>",
53+
);
54+
expect(getJsxElementName(path)).toBe("My.Very.Custom.React.Component");
55+
});
4456
});
4557

4658
describe("getNestedJsxElements", () => {

packages/compiler/src/utils/jsx-element.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@ import { NodePath } from "@babel/traverse";
33

44
export function getJsxElementName(nodePath: NodePath<t.JSXElement>) {
55
const openingElement = nodePath.node.openingElement;
6+
7+
// elements with simple (string) name
68
if (t.isJSXIdentifier(openingElement.name)) {
79
return openingElement.name.name;
810
}
11+
12+
// elements with dots in name
13+
if (t.isJSXMemberExpression(openingElement.name)) {
14+
const memberExpr = openingElement.name;
15+
const parts: string[] = [];
16+
17+
// Traverse the member expression to collect all parts
18+
let current: t.JSXMemberExpression | t.JSXIdentifier = memberExpr;
19+
while (t.isJSXMemberExpression(current)) {
20+
parts.unshift(current.property.name);
21+
current = current.object;
22+
}
23+
24+
// Add the base identifier
25+
if (t.isJSXIdentifier(current)) {
26+
parts.unshift(current.name);
27+
}
28+
29+
return parts.join(".");
30+
}
931
return null;
1032
}
1133

0 commit comments

Comments
 (0)