JSXAttributeMock.js 665 B

123456789101112131415161718192021222324
  1. import toAST from 'to-ast'; // eslint-disable-line import/no-extraneous-dependencies
  2. import JSXExpressionContainerMock from './JSXExpressionContainerMock';
  3. export default function JSXAttributeMock(prop, value, isExpressionContainer = false) {
  4. let astValue;
  5. if (value && value.type !== undefined) {
  6. astValue = value;
  7. } else {
  8. astValue = toAST(value);
  9. }
  10. let attributeValue = astValue;
  11. if (isExpressionContainer || astValue.type !== 'Literal') {
  12. attributeValue = JSXExpressionContainerMock(astValue);
  13. }
  14. return {
  15. type: 'JSXAttribute',
  16. name: {
  17. type: 'JSXIdentifier',
  18. name: prop,
  19. },
  20. value: attributeValue,
  21. };
  22. }