diff --git a/__tests__/src/elementType-test.js b/__tests__/src/elementType-test.js index 4cca3a9..c3939ca 100644 --- a/__tests__/src/elementType-test.js +++ b/__tests__/src/elementType-test.js @@ -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 = ''; const node = getOpeningElement(code); @@ -58,4 +58,25 @@ 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 = ''; + const node = getOpeningElement(code); + + const expected = 'UX.Slider.Blue.Light'; + const actual = elementType(node); + + assert.equal(expected, actual); + }); + + it('should return this.Component when given its node object', () => { + const code = ''; + const node = getOpeningElement(code); + + const expected = 'this.Component'; + const actual = elementType(node); + + assert.equal(expected, actual); + }); }); diff --git a/src/elementType.js b/src/elementType.js index 82e1669..b3b847f 100644 --- a/src/elementType.js +++ b/src/elementType.js @@ -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. */ @@ -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}`; }