forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.js
More file actions
197 lines (168 loc) · 5.64 KB
/
Copy pathusage.js
File metadata and controls
197 lines (168 loc) · 5.64 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import camelCase from "camelcase";
import { categoryOrder, usageSummary } from "./constants.evaluate.js";
import { formatOptionsHiddenDefaults } from "./prettier-internal.js";
import { groupBy } from "./utils.js";
const OPTION_USAGE_THRESHOLD = 25;
const CHOICE_USAGE_MARGIN = 3;
const CHOICE_USAGE_INDENTATION = 2;
function indent(str, spaces) {
return str.replaceAll(/^/gmu, " ".repeat(spaces));
}
function createDefaultValueDisplay(value) {
return Array.isArray(value)
? `[${value.map(createDefaultValueDisplay).join(", ")}]`
: value;
}
function getOptionDefaultValue(context, optionName) {
// --no-option
const option = context.detailedOptions.find(
({ name }) => name === optionName,
);
if (option?.default !== undefined) {
return option.default;
}
const optionCamelName = camelCase(optionName);
return (
formatOptionsHiddenDefaults[optionCamelName] ??
context.supportOptions.find(
(option) => !option.deprecated && option.name === optionCamelName,
)?.default
);
}
function createOptionUsageHeader(option) {
const name = `--${option.name}`;
const alias = option.alias ? `-${option.alias},` : null;
const type = createOptionUsageType(option);
return [alias, name, type].filter(Boolean).join(" ");
}
function createOptionUsageRow(header, content, threshold) {
const separator =
header.length >= threshold
? `\n${" ".repeat(threshold)}`
: " ".repeat(threshold - header.length);
const description = content.replaceAll("\n", `\n${" ".repeat(threshold)}`);
return `${header}${separator}${description}`;
}
function createOptionUsageType(option) {
switch (option.type) {
case "boolean":
return null;
case "choice":
return `<${option.choices
.filter((choice) => !choice.deprecated)
.map((choice) => choice.value)
.join("|")}>`;
default:
return `<${option.type}>`;
}
}
function createChoiceUsages(choices, margin, indentation) {
const activeChoices = choices.filter((choice) => !choice.deprecated);
const threshold =
Math.max(0, ...activeChoices.map((choice) => choice.value.length)) + margin;
return activeChoices.map((choice) =>
indent(
createOptionUsageRow(choice.value, choice.description, threshold),
indentation,
),
);
}
function createOptionUsage(context, option, threshold) {
const header = createOptionUsageHeader(option);
const optionDefaultValue = getOptionDefaultValue(context, option.name);
return createOptionUsageRow(
header,
`${option.description}${
optionDefaultValue === undefined
? ""
: `\nDefaults to ${createDefaultValueDisplay(optionDefaultValue)}.`
}`,
threshold,
);
}
function getOptionsWithOpposites(options) {
// Add --no-foo after --foo.
const optionsWithOpposites = options.map((option) => [
option.description ? option : null,
option.oppositeDescription
? {
...option,
name: `no-${option.name}`,
type: "boolean",
description: option.oppositeDescription,
}
: null,
]);
return optionsWithOpposites.flat().filter(Boolean);
}
function createUsage(context) {
const sortedOptions = context.detailedOptions.sort((optionA, optionB) =>
optionA.name.localeCompare(optionB.name),
);
const options = getOptionsWithOpposites(sortedOptions).filter(
// remove unnecessary option (e.g. `semi`, `color`, etc.), which is only used for --help <flag>
(option) =>
!(
option.type === "boolean" &&
option.oppositeDescription &&
!option.name.startsWith("no-")
),
);
const groupedOptions = groupBy(options, (option) => option.category);
const firstCategories = categoryOrder.slice(0, -1);
const lastCategories = categoryOrder.slice(-1);
const restCategories = Object.keys(groupedOptions).filter(
(category) => !categoryOrder.includes(category),
);
const allCategories = [
...firstCategories,
...restCategories,
...lastCategories,
];
const optionsUsage = allCategories.map((category) => {
const categoryOptions = groupedOptions[category]
.map((option) =>
createOptionUsage(context, option, OPTION_USAGE_THRESHOLD),
)
.join("\n");
return `${category} options:\n\n${indent(categoryOptions, 2)}`;
});
return [usageSummary, ...optionsUsage, ""].join("\n\n");
}
function createPluginDefaults(pluginDefaults) {
if (!pluginDefaults || Object.keys(pluginDefaults).length === 0) {
return "";
}
const defaults = Object.entries(pluginDefaults)
.sort(([pluginNameA], [pluginNameB]) =>
pluginNameA.localeCompare(pluginNameB),
)
.map(
([plugin, value]) => `* ${plugin}: ${createDefaultValueDisplay(value)}`,
)
.join("\n");
return `\nPlugin defaults:\n${defaults}`;
}
function createDetailedUsage(context, flag) {
const option = getOptionsWithOpposites(context.detailedOptions).find(
(option) => option.name === flag || option.alias === flag,
);
const header = createOptionUsageHeader(option);
const description = `\n\n${indent(option.description, 2)}`;
const choices =
option.type !== "choice"
? ""
: `\n\nValid options:\n\n${createChoiceUsages(
option.choices,
CHOICE_USAGE_MARGIN,
CHOICE_USAGE_INDENTATION,
).join("\n")}`;
const optionDefaultValue = getOptionDefaultValue(context, option.name);
const defaults =
optionDefaultValue !== undefined
? `\n\nDefault: ${createDefaultValueDisplay(optionDefaultValue)}`
: "";
const pluginDefaults = createPluginDefaults(option.pluginDefaults);
return `${header}${description}${choices}${defaults}${pluginDefaults}`;
}
export { createDetailedUsage, createUsage };