Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dotted component name support for babel-plugin-transform-jsx-to-htm #98

Merged
merged 4 commits into from
Jul 4, 2019
Merged
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
51 changes: 31 additions & 20 deletions packages/babel-plugin-transform-jsx-to-htm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,33 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
}));
buffer = '';
}

const FRAGMENT_EXPR = dottedIdentifier('React.Fragment');

function isFragmentName(node) {
return t.isNodesEquivalent(FRAGMENT_EXPR, node);
}

function getName(node) {
switch (node.type) {
case 'JSXMemberExpression':
return `${node.object.name}.${node.property.name}`
default:
return node.name;
function isComponentName(node) {
return !t.isIdentifier(node) || node.name.match(/^[$_A-Z]/);
}

function getNameExpr(node) {
if (!t.isJSXMemberExpression(node)) {
return t.identifier(node.name);
}
return t.memberExpression(
getNameExpr(node.object),
t.identifier(node.property.name)
);
}

function processChildren(node, name, isFragment) {
const children = t.react.buildChildren(node);
if (children && children.length !== 0) {
if (!isFragment) {
raw('>');
}
}
for (let i = 0; i < children.length; i++) {
let child = children[i];
if (t.isStringLiteral(child)) {
Expand All @@ -119,17 +129,17 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
}

if (!isFragment) {
if (name.match(/(^[$_A-Z]|\.)/)) {
if (isComponentName(name)) {
raw('</');
expr(t.identifier(name));
expr(name);
raw('>');
}
else {
raw('</');
raw(name);
raw(name.name);
raw('>');
}
}
}
}
else if (!isFragment) {
raw('/>');
Expand All @@ -138,17 +148,17 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {

function processNode(node, path, isRoot) {
const open = node.openingElement;
const name = getName(open.name);
const isFragment = name === 'React.Fragment';

const name = getNameExpr(open.name);
const isFragment = isFragmentName(name);
if (!isFragment) {
if (name.match(/(^[$_A-Z]|\.)/)) {
if (isComponentName(name)) {
raw('<');
expr(t.identifier(name));
expr(name);
}
else {
raw('<');
raw(name);
raw(name.name);
}

if (open.attributes) {
Expand Down Expand Up @@ -198,12 +208,13 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
expressions.length = 0;

if (isFragment) {
processChildren(path.node, '', true);
processChildren(path.node, null, true);
commit();
const template = t.templateLiteral(quasis, expressions);
const replacement = t.taggedTemplateExpression(tag, template);
path.replaceWith(replacement);
} else {
}
else {
processNode(path.node, path, true);
}

Expand Down
58 changes: 58 additions & 0 deletions test/babel-transform-jsx.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ describe('babel-plugin-transform-jsx-to-htm', () => {
expect(
compile('(<div>a</div>);')
).toBe('html`<div>a</div>`;');

expect(
compile('(<div$ />);')
).toBe('html`<div$/>`;');

expect(
compile('(<div$>a</div$>);')
).toBe('html`<div$>a</div$>`;');

expect(
compile('(<div_ />);')
).toBe('html`<div_/>`;');

expect(
compile('(<div_>a</div_>);')
).toBe('html`<div_>a</div_>`;');
});

test('single component element', () => {
Expand All @@ -77,6 +93,48 @@ describe('babel-plugin-transform-jsx-to-htm', () => {
expect(
compile('(<Foo>a</Foo>);')
).toBe('html`<${Foo}>a</${Foo}>`;');

expect(
compile('(<$ />);')
).toBe('html`<${$}/>`;');

expect(
compile('(<$>a</$>);')
).toBe('html`<${$}>a</${$}>`;');

expect(
compile('(<_ />);')
).toBe('html`<${_}/>`;');

expect(
compile('(<_>a</_>);')
).toBe('html`<${_}>a</${_}>`;');

expect(
compile('(<_foo />);')
).toBe('html`<${_foo}/>`;');

expect(
compile('(<_foo>a</_foo>);')
).toBe('html`<${_foo}>a</${_foo}>`;');

expect(
compile('(<$foo />);')
).toBe('html`<${$foo}/>`;');

expect(
compile('(<$foo>a</$foo>);')
).toBe('html`<${$foo}>a</${$foo}>`;');
});

test('dotted component element', () => {
expect(
compile('(<a.b.c />);')
).toBe('html`<${a.b.c}/>`;');

expect(
compile('(<a.b.c>a</a.b.c>);')
).toBe('html`<${a.b.c}>a</${a.b.c}>`;');
});

test('static text', () => {
Expand Down