generate-schema.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env node
  2. "use strict";
  3. if (require.main !== module) {
  4. module.exports = generateSchema;
  5. } else {
  6. const prettier = require("..");
  7. console.log(
  8. prettier.format(
  9. JSON.stringify(generateSchema(prettier.getSupportInfo().options)),
  10. { parser: "json" }
  11. )
  12. );
  13. }
  14. function generateSchema(options) {
  15. return {
  16. $schema: "http://json-schema.org/draft-04/schema#",
  17. title: "Schema for .prettierrc",
  18. type: "object",
  19. definitions: {
  20. optionsDefinition: {
  21. type: "object",
  22. properties: options.reduce(
  23. (props, option) =>
  24. Object.assign(props, { [option.name]: optionToSchema(option) }),
  25. {}
  26. )
  27. },
  28. overridesDefinition: {
  29. type: "object",
  30. properties: {
  31. overrides: {
  32. type: "array",
  33. description:
  34. "Provide a list of patterns to override prettier configuration.",
  35. items: {
  36. type: "object",
  37. required: ["files"],
  38. properties: {
  39. files: {
  40. description: "Include these files in this override.",
  41. oneOf: [
  42. { type: "string" },
  43. { type: "array", items: { type: "string" } }
  44. ]
  45. },
  46. excludeFiles: {
  47. description: "Exclude these files from this override.",
  48. oneOf: [
  49. { type: "string" },
  50. { type: "array", items: { type: "string" } }
  51. ]
  52. },
  53. options: {
  54. type: "object",
  55. description: "The options to apply for this override.",
  56. $ref: "#/definitions/optionsDefinition"
  57. }
  58. },
  59. additionalProperties: false
  60. }
  61. }
  62. }
  63. }
  64. },
  65. allOf: [
  66. { $ref: "#/definitions/optionsDefinition" },
  67. { $ref: "#/definitions/overridesDefinition" }
  68. ]
  69. };
  70. }
  71. function optionToSchema(option) {
  72. return Object.assign(
  73. {
  74. description: option.description,
  75. default: option.default
  76. },
  77. (option.array ? wrapWithArraySchema : identity)(
  78. option.type === "choice"
  79. ? { oneOf: option.choices.map(choiceToSchema) }
  80. : { type: optionTypeToSchemaType(option.type) }
  81. )
  82. );
  83. }
  84. function identity(x) {
  85. return x;
  86. }
  87. function wrapWithArraySchema(items) {
  88. return { type: "array", items };
  89. }
  90. function optionTypeToSchemaType(optionType) {
  91. switch (optionType) {
  92. case "int":
  93. return "integer";
  94. case "boolean":
  95. return optionType;
  96. case "choice":
  97. throw new Error(
  98. "Please use `oneOf` instead of `enum` for better description support."
  99. );
  100. case "path":
  101. return "string";
  102. default:
  103. throw new Error(`Unexpected optionType '${optionType}'`);
  104. }
  105. }
  106. function choiceToSchema(choice) {
  107. return { enum: [choice.value], description: choice.description };
  108. }