utils.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. exports.stringifyValidator = function stringifyValidator(
  2. validator,
  3. nodePrefix
  4. ) {
  5. if (validator === undefined) {
  6. return "any";
  7. }
  8. if (validator.each) {
  9. return `Array<${stringifyValidator(validator.each, nodePrefix)}>`;
  10. }
  11. if (validator.chainOf) {
  12. return stringifyValidator(validator.chainOf[1], nodePrefix);
  13. }
  14. if (validator.oneOf) {
  15. return validator.oneOf.map(JSON.stringify).join(" | ");
  16. }
  17. if (validator.oneOfNodeTypes) {
  18. return validator.oneOfNodeTypes.map(_ => nodePrefix + _).join(" | ");
  19. }
  20. if (validator.oneOfNodeOrValueTypes) {
  21. return validator.oneOfNodeOrValueTypes
  22. .map(_ => {
  23. return isValueType(_) ? _ : nodePrefix + _;
  24. })
  25. .join(" | ");
  26. }
  27. if (validator.type) {
  28. return validator.type;
  29. }
  30. return ["any"];
  31. };
  32. exports.toFunctionName = function toFunctionName(typeName) {
  33. const _ = typeName.replace(/^TS/, "ts").replace(/^JSX/, "jsx");
  34. return _.slice(0, 1).toLowerCase() + _.slice(1);
  35. };
  36. /**
  37. * Heuristic to decide whether or not the given type is a value type (eg. "null")
  38. * or a Node type (eg. "Expression").
  39. */
  40. function isValueType(type) {
  41. return type.charAt(0).toLowerCase() === type.charAt(0);
  42. }