transform.js 973 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const adapt = require('../../../src/index');
  2. const describe = require('jscodeshift-helper').describe;
  3. module.exports = adapt((fileInfo, api, options) => {
  4. const j = api.jscodeshift;
  5. const root = j(fileInfo.source);
  6. root
  7. .find(j.ExportDefaultDeclaration)
  8. .find(j.ObjectExpression)
  9. .at(0)
  10. .find(j.Property, p => {
  11. return p.key.name === 'props'
  12. })
  13. .find(j.Property)
  14. .filter(p => {
  15. return p.parent.parent.node.key.name === 'props';
  16. })
  17. .find(j.ObjectExpression)
  18. .forEach(o => {
  19. const requiredFlag = o.node.properties.find(p => {
  20. return p.key.name === 'required'
  21. });
  22. if (requiredFlag) {
  23. if (requiredFlag.value.value === false) {
  24. requiredFlag.value = j.literal(true);
  25. }
  26. } else {
  27. o.node.properties.push(j.property(
  28. 'init',
  29. j.identifier('required'),
  30. j.literal(true)
  31. ));
  32. }
  33. });
  34. return root.toSource();
  35. });