Skip to content

Commit 12af51c

Browse files
authored
Merge pull request #207 from developit/support-namespaces
[transform-jsx-to-htm] Add support for namespaces in JSX
2 parents ee50239 + 59b0624 commit 12af51c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"devDependencies": {
108108
"@babel/core": "^7.2.2",
109109
"@babel/preset-env": "^7.1.6",
110+
"@types/jest": "^26.0.24",
110111
"babel-jest": "^24.1.0",
111112
"babel-preset-env": "^1.7.0",
112113
"eslint": "^5.2.0",

packages/babel-plugin-transform-jsx-to-htm/index.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,14 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
8989
}
9090

9191
function isComponentName(node) {
92+
if (t.isJSXNamespacedName(node)) return false;
9293
return !t.isIdentifier(node) || node.name.match(/^[$_A-Z]/);
9394
}
9495

9596
function getNameExpr(node) {
97+
if (t.isJSXNamespacedName(node)) {
98+
return t.identifier(node.namespace.name + ':' + node.name.name);
99+
}
96100
if (!t.isJSXMemberExpression(node)) {
97101
return t.identifier(node.name);
98102
}
@@ -169,7 +173,12 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
169173
continue;
170174
}
171175
const { name, value } = attr;
172-
raw(name.name);
176+
if (t.isJSXNamespacedName(name)) {
177+
raw(name.namespace.name + ':' + name.name.name);
178+
}
179+
else {
180+
raw(name.name);
181+
}
173182
if (value) {
174183
raw('=');
175184
if (value.expression) {

test/babel-transform-jsx.test.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ describe('babel-plugin-transform-jsx-to-htm', () => {
137137
).toBe('html`<${a.b.c}>a</${a.b.c}>`;');
138138
});
139139

140+
test('namespaced element names', () => {
141+
expect(
142+
compile('(<a:b/>);')
143+
).toBe('html`<a:b/>`;');
144+
145+
expect(
146+
compile('(<a:b><x:y/></a:b>);')
147+
).toBe('html`<a:b><x:y/></a:b>`;');
148+
});
149+
150+
test('namespaced attributes', () => {
151+
expect(
152+
compile('(<a b:c="d"/>);')
153+
).toBe('html`<a b:c="d"/>`;');
154+
155+
expect(
156+
compile('(<a b:c="d" e:f={1} g>h</a>);')
157+
).toBe('html`<a b:c="d" e:f=${1} g>h</a>`;');
158+
});
159+
140160
test('static text', () => {
141161
expect(
142162
compile(`(<div>Hello</div>);`)

0 commit comments

Comments
 (0)