docs.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. const util = require("util");
  3. const utils = require("./utils");
  4. const types = require("../../packages/babel-types");
  5. const readme = [
  6. `# @babel/types
  7. > This module contains methods for building ASTs manually and for checking the types of AST nodes.
  8. ## Install
  9. \`\`\`sh
  10. npm install --save-dev @babel/types
  11. \`\`\`
  12. ## API`,
  13. ];
  14. const customTypes = {
  15. ClassMethod: {
  16. key: "if computed then `Expression` else `Identifier | Literal`",
  17. },
  18. Identifier: {
  19. name: "`string`",
  20. },
  21. MemberExpression: {
  22. property: "if computed then `Expression` else `Identifier`",
  23. },
  24. ObjectMethod: {
  25. key: "if computed then `Expression` else `Identifier | Literal`",
  26. },
  27. ObjectProperty: {
  28. key: "if computed then `Expression` else `Identifier | Literal`",
  29. },
  30. };
  31. Object.keys(types.BUILDER_KEYS)
  32. .sort()
  33. .forEach(function(key) {
  34. readme.push("### " + key[0].toLowerCase() + key.substr(1));
  35. readme.push("```javascript");
  36. readme.push(
  37. "t." +
  38. utils.toFunctionName(key) +
  39. "(" +
  40. types.BUILDER_KEYS[key].join(", ") +
  41. ")"
  42. );
  43. readme.push("```");
  44. readme.push("");
  45. readme.push(
  46. "See also `t.is" +
  47. key +
  48. "(node, opts)` and `t.assert" +
  49. key +
  50. "(node, opts)`."
  51. );
  52. readme.push("");
  53. if (types.ALIAS_KEYS[key] && types.ALIAS_KEYS[key].length) {
  54. readme.push(
  55. "Aliases: " +
  56. types.ALIAS_KEYS[key]
  57. .map(function(key) {
  58. return "`" + key + "`";
  59. })
  60. .join(", ")
  61. );
  62. readme.push("");
  63. }
  64. Object.keys(types.NODE_FIELDS[key])
  65. .sort(function(fieldA, fieldB) {
  66. const indexA = types.BUILDER_KEYS[key].indexOf(fieldA);
  67. const indexB = types.BUILDER_KEYS[key].indexOf(fieldB);
  68. if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
  69. if (indexA === -1) return 1;
  70. if (indexB === -1) return -1;
  71. return indexA - indexB;
  72. })
  73. .forEach(function(field) {
  74. const defaultValue = types.NODE_FIELDS[key][field].default;
  75. const fieldDescription = ["`" + field + "`"];
  76. const validator = types.NODE_FIELDS[key][field].validate;
  77. if (customTypes[key] && customTypes[key][field]) {
  78. fieldDescription.push(`: ${customTypes[key][field]}`);
  79. } else if (validator) {
  80. try {
  81. fieldDescription.push(
  82. ": `" + utils.stringifyValidator(validator, "") + "`"
  83. );
  84. } catch (ex) {
  85. if (ex.code === "UNEXPECTED_VALIDATOR_TYPE") {
  86. console.log(
  87. "Unrecognised validator type for " + key + "." + field
  88. );
  89. console.dir(ex.validator, { depth: 10, colors: true });
  90. }
  91. }
  92. }
  93. if (defaultValue !== null || types.NODE_FIELDS[key][field].optional) {
  94. fieldDescription.push(
  95. " (default: `" + util.inspect(defaultValue) + "`)"
  96. );
  97. } else {
  98. fieldDescription.push(" (required)");
  99. }
  100. readme.push(" - " + fieldDescription.join(""));
  101. });
  102. readme.push("");
  103. readme.push("---");
  104. readme.push("");
  105. });
  106. process.stdout.write(readme.join("\n"));