3
0

generator.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //Import this script to inject the reflective propTypes in react
  2. //We need to ensure that we are using the same react module as the component we are using
  3. //For this to happen our script can't depend on react, and we must import react from the target node_modules folder
  4. var React = require("../../node_modules/react");
  5. var originalPropTypes = React.PropTypes;
  6. var newPropTypes = {}
  7. for(var propType in originalPropTypes){
  8. // Create a new reflective property each time the getter is called
  9. Object.defineProperty(newPropTypes, propType,
  10. {
  11. get: reflectiveGetter(propType)
  12. }
  13. );
  14. }
  15. React.PropTypes = newPropTypes
  16. //Enforce different scope for each property
  17. function reflectiveGetter(propType){
  18. return function(){
  19. return createReflectivePropType(propType);
  20. }
  21. }
  22. function createReflectivePropType(type){
  23. var fakePropType = null;
  24. //Used for propTypes with arguments
  25. fakePropType = function fakePropType(propTypes){
  26. createInnerPropertyTypes(fakePropType, propTypes)
  27. return fakePropType;
  28. }
  29. fakePropType.type = type;
  30. fakePropType.isRequired = function(){}
  31. fakePropType.isRequired.type = type;
  32. fakePropType.isRequired.required = true;
  33. return fakePropType;
  34. }
  35. function createInnerPropertyTypes(outer, propTypes){
  36. outer.inner = propTypes;
  37. outer.isRequired.inner = propTypes
  38. }