Skip to content

Commit

Permalink
[fix] - Fix resolving element type with multiple member expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Cohen committed Nov 15, 2016
1 parent 9a8397f commit c35c2f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 12 additions & 1 deletion __tests__/src/elementType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('elementType tests', () => {
assert.equal(expected, actual);
});

it('should return the correct type of the custom object element given its node object', () => {
it('should return the correct type of the namespaced element given its node object', () => {
const code = '<UX:Slider />';
const node = getOpeningElement(code);

Expand All @@ -58,4 +58,15 @@ describe('elementType tests', () => {

assert.equal(expected, actual);
});

it('should return the correct type of the multiple custom object element given its node object',
() => {
const code = '<UX.Slider.Blue.Light />';
const node = getOpeningElement(code);

const expected = 'UX.Slider.Blue.Light';
const actual = elementType(node);

assert.equal(expected, actual);
});
});
12 changes: 10 additions & 2 deletions src/elementType.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function resolveMemberExpressions(object = {}, property = {}) {
if (object.type === 'JSXMemberExpression') {
return `${resolveMemberExpressions(object.object, object.property)}.${property.name}`;
}

return `${object.name}.${property.name}`;
}

/**
* Returns the tagName associated with a JSXElement.
*/
Expand All @@ -9,8 +17,8 @@ export default function elementType(node = {}) {
}

if (name.type === 'JSXMemberExpression') {
const { object, property } = name;
return `${object.name}.${property.name}`;
const { object = {}, property = {} } = name;
return resolveMemberExpressions(object, property);
} else if (name.type === 'JSXNamespacedName') {
return `${name.namespace.name}:${name.name.name}`;
}
Expand Down

0 comments on commit c35c2f5

Please sign in to comment.