forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.js
More file actions
92 lines (76 loc) · 2.76 KB
/
support.js
File metadata and controls
92 lines (76 loc) · 2.76 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
import coreOptions from "./core-options.evaluate.js";
/**
* @typedef {import("./core-options.evaluate.js").OptionInfo} OptionInfo
* @typedef {{ name: string; pluginDefaults: Array<any> } & OptionInfo} NamedOptionInfo
*/
/**
* Strings in `plugins` are handled by a wrapped version
* of this function created by `withPlugins`. Don't pass them here directly.
* @param {object} param0
* @param {(string | object)[]=} param0.plugins Strings are resolved by `withPlugins`.
* @param {boolean=} param0.showDeprecated
* @return {{ languages: Array<any>, options: Array<NamedOptionInfo> }}
*/
function getSupportInfo({ plugins = [], showDeprecated = false } = {}) {
const languages = plugins.flatMap((plugin) => plugin.languages ?? []);
const options = [];
for (const option of normalizeOptionSettings(
Object.assign({}, ...plugins.map(({ options }) => options), coreOptions),
)) {
if (!showDeprecated && option.deprecated) {
continue;
}
if (Array.isArray(option.choices)) {
if (!showDeprecated) {
option.choices = option.choices.filter((choice) => !choice.deprecated);
}
if (option.name === "parser") {
option.choices = [
...option.choices,
...collectParsersFromLanguages(option.choices, languages, plugins),
];
}
}
option.pluginDefaults = Object.fromEntries(
plugins
.filter((plugin) => plugin.defaultOptions?.[option.name] !== undefined)
.map((plugin) => [plugin.name, plugin.defaultOptions[option.name]]),
);
options.push(option);
}
return { languages, options };
}
function* collectParsersFromLanguages(parserChoices, languages, plugins) {
const existingParsers = new Set(parserChoices.map((choice) => choice.value));
for (const language of languages) {
if (language.parsers) {
for (const parserName of language.parsers) {
if (!existingParsers.has(parserName)) {
existingParsers.add(parserName);
const plugin = plugins.find(
(plugin) =>
plugin.parsers && Object.hasOwn(plugin.parsers, parserName),
);
let description = language.name;
if (plugin?.name) {
description += ` (plugin: ${plugin.name})`;
}
yield { value: parserName, description };
}
}
}
}
}
function normalizeOptionSettings(settings) {
const options = [];
for (const [name, originalOption] of Object.entries(settings)) {
const option = { name, ...originalOption };
// This work this way because we used support `[{value: [], since: '0.0.0'}]`
if (Array.isArray(option.default)) {
option.default = option.default.at(-1).value;
}
options.push(option);
}
return options;
}
export { getSupportInfo, normalizeOptionSettings };