Skip to content

Commit 8798c58

Browse files
committed
[Fix] extractProp: support JSXFragment
Fixes #132
1 parent bde3ba9 commit 8798c58

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

__tests__/src/elementType-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,27 @@ describe('elementType tests', () => {
9292

9393
assert.equal(actual, expected);
9494
});
95+
96+
it('works with nested fragments', () => {
97+
const code = `
98+
<Hello
99+
role="checkbox"
100+
frag={
101+
<>
102+
<div>Hello</div>
103+
<>
104+
<div>There</div>
105+
</>
106+
</>
107+
}
108+
/>
109+
`;
110+
const node = getOpeningElement(code);
111+
112+
const expected = 'Hello';
113+
const actual = elementType(node);
114+
115+
assert.equal(actual, expected);
116+
});
95117
});
96118
});

__tests__/src/getPropValue-babelparser-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ describe('getPropValue', () => {
150150

151151
assert.equal(actual, expected);
152152
});
153+
154+
it('supports a prop value containing nested fragments', () => {
155+
const propCode = `
156+
<>
157+
<div>Hello</div>
158+
<>
159+
<div>There</div>
160+
</>
161+
</>
162+
`;
163+
const code = `
164+
<Hello
165+
role="checkbox"
166+
frag={${propCode}}
167+
/>
168+
`;
169+
170+
const prop = extractProp(code, 'frag');
171+
const actual = getPropValue(prop);
172+
173+
assert.deepEqual(actual, propCode.trim());
174+
});
153175
});
154176

155177
describe('Identifier', () => {

src/values/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Literal from './Literal';
22
import JSXElement from './JSXElement';
33
import JSXText from './JSXText';
4+
import JSXFragment from './JSXFragment';
45
import JSXExpressionContainer, { extractLiteral } from './expressions';
56

67
// Composition map of types to their extractor functions.
@@ -9,6 +10,7 @@ const TYPES = {
910
JSXElement,
1011
JSXExpressionContainer,
1112
JSXText,
13+
JSXFragment,
1214
};
1315

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

0 commit comments

Comments
 (0)