Skip to content

Commit

Permalink
[Fix] extractProp: support JSXFragment
Browse files Browse the repository at this point in the history
Fixes #132
  • Loading branch information
ljharb committed Jul 25, 2023
1 parent bde3ba9 commit 8798c58
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions __tests__/src/elementType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,27 @@ describe('elementType tests', () => {

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

it('works with nested fragments', () => {
const code = `
<Hello
role="checkbox"
frag={
<>
<div>Hello</div>
<>
<div>There</div>
</>
</>
}
/>
`;
const node = getOpeningElement(code);

const expected = 'Hello';
const actual = elementType(node);

assert.equal(actual, expected);
});
});
});
22 changes: 22 additions & 0 deletions __tests__/src/getPropValue-babelparser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ describe('getPropValue', () => {

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

it('supports a prop value containing nested fragments', () => {
const propCode = `
<>
<div>Hello</div>
<>
<div>There</div>
</>
</>
`;
const code = `
<Hello
role="checkbox"
frag={${propCode}}
/>
`;

const prop = extractProp(code, 'frag');
const actual = getPropValue(prop);

assert.deepEqual(actual, propCode.trim());
});
});

describe('Identifier', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/values/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Literal from './Literal';
import JSXElement from './JSXElement';
import JSXText from './JSXText';
import JSXFragment from './JSXFragment';
import JSXExpressionContainer, { extractLiteral } from './expressions';

// Composition map of types to their extractor functions.
Expand All @@ -9,6 +10,7 @@ const TYPES = {
JSXElement,
JSXExpressionContainer,
JSXText,
JSXFragment,
};

// Composition map of types to their extractor functions to handle literals.
Expand All @@ -28,6 +30,7 @@ const LITERAL_TYPES = {
* @param value - AST Value object on a JSX Attribute.
*/
export default function getValue(value) {
if (!TYPES[value.type]) console.log(value.type);
return TYPES[value.type](value);
}

Expand Down

0 comments on commit 8798c58

Please sign in to comment.