forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-schema.js
More file actions
128 lines (122 loc) · 3.67 KB
/
generate-schema.js
File metadata and controls
128 lines (122 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function generateSchemaData(options) {
return {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "https://json.schemastore.org/prettierrc.json",
definitions: {
optionsDefinition: {
type: "object",
properties: Object.fromEntries(
options
.sort(({ name: optionNameA }, { name: optionNameB }) =>
optionNameA.localeCompare(optionNameB),
)
.map((option) => [option.name, optionToSchema(option)]),
),
},
overridesDefinition: {
type: "object",
properties: {
overrides: {
type: "array",
description:
"Provide a list of patterns to override prettier configuration.",
items: {
type: "object",
required: ["files"],
properties: {
files: {
description: "Include these files in this override.",
oneOf: [
{ type: "string" },
{ type: "array", items: { type: "string" } },
],
},
excludeFiles: {
description: "Exclude these files from this override.",
oneOf: [
{ type: "string" },
{ type: "array", items: { type: "string" } },
],
},
options: {
$ref: "#/definitions/optionsDefinition",
type: "object",
description: "The options to apply for this override.",
},
},
additionalProperties: false,
},
},
},
},
},
oneOf: [
{
type: "object",
allOf: [
{ $ref: "#/definitions/optionsDefinition" },
{ $ref: "#/definitions/overridesDefinition" },
],
},
{
type: "string",
},
],
title: "Schema for .prettierrc",
};
}
function optionToSchema(option) {
let schema;
if (option.type === "choice") {
const choicesSchema = option.choices.map(choiceToSchema);
let key = "oneOf";
if (option.name === "parser") {
// To support custom parser
// ref: https://github.com/SchemaStore/schemastore/pull/1636
choicesSchema.push({ type: "string", description: "Custom parser" });
// We should use "anyOf" for "parser" option.
// ref: https://github.com/SchemaStore/schemastore/pull/1642
key = "anyOf";
}
schema = { [key]: choicesSchema };
} else {
schema = { type: optionTypeToSchemaType(option.type) };
}
if (option.array) {
schema = wrapWithArraySchema(schema);
}
return {
description: option.description,
default: option.default,
...schema,
};
}
function wrapWithArraySchema(items) {
return { type: "array", items };
}
function optionTypeToSchemaType(optionType) {
switch (optionType) {
case "int":
return "integer";
case "boolean":
return optionType;
case "choice":
throw new Error(
"Please use `oneOf` instead of `enum` for better description support.",
);
case "path":
return "string";
default:
throw new Error(`Unexpected optionType '${optionType}'`);
}
}
function choiceToSchema(choice) {
return { enum: [choice.value], description: choice.description };
}
async function generateSchema() {
const { format, getSupportInfo } = await import("../../src/index.js");
const supportInfo = await getSupportInfo();
const schema = generateSchemaData(supportInfo.options);
return format(JSON.stringify(schema, undefined, 2), { parser: "json" });
}
export { generateSchema, generateSchemaData };