forked from prettier/prettier
/
support.js
155 lines (140 loc) · 4.1 KB
/
support.js
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
"use strict";
const semver = require("semver");
const arrayify = require("../utils/arrayify");
const currentVersion = require("../../package.json").version;
const coreOptions = require("./core-options").options;
function getSupportInfo(version, opts) {
opts = Object.assign(
{
plugins: [],
showUnreleased: false,
showDeprecated: false,
showInternal: false
},
opts
);
if (!version) {
// pre-release version is smaller than the normal version in semver,
// we need to treat it as the normal one so as to test new features.
version = currentVersion.split("-", 1)[0];
}
const plugins = opts.plugins;
const options = arrayify(
Object.assign(
plugins.reduce(
(currentOptions, plugin) =>
Object.assign(currentOptions, plugin.options),
{}
),
coreOptions
),
"name"
)
.sort((a, b) => (a.name === b.name ? 0 : a.name < b.name ? -1 : 1))
.filter(filterSince)
.filter(filterDeprecated)
.map(mapDeprecated)
.map(mapInternal)
.map(option => {
const newOption = Object.assign({}, option);
if (Array.isArray(newOption.default)) {
newOption.default =
newOption.default.length === 1
? newOption.default[0].value
: newOption.default
.filter(filterSince)
.sort((info1, info2) =>
semver.compare(info2.since, info1.since)
)[0].value;
}
if (Array.isArray(newOption.choices)) {
newOption.choices = newOption.choices
.filter(filterSince)
.filter(filterDeprecated)
.map(mapDeprecated);
}
return newOption;
})
.map(option => {
const filteredPlugins = plugins.filter(
plugin => plugin.defaultOptions && plugin.defaultOptions[option.name]
);
const pluginDefaults = filteredPlugins.reduce((reduced, plugin) => {
reduced[plugin.name] = plugin.defaultOptions[option.name];
return reduced;
}, {});
return Object.assign(option, { pluginDefaults });
});
const usePostCssParser = semver.lt(version, "1.7.1");
const useBabylonParser = semver.lt(version, "1.16.0");
const languages = plugins
.reduce((all, plugin) => all.concat(plugin.languages || []), [])
.filter(filterSince)
.map(language => {
// Prevent breaking changes
if (language.name === "Markdown") {
return Object.assign({}, language, {
parsers: ["markdown"]
});
}
if (language.name === "TypeScript") {
return Object.assign({}, language, {
parsers: ["typescript"]
});
}
// "babylon" was renamed to "babel" in 1.16.0
if (useBabylonParser && language.parsers.indexOf("babel") !== -1) {
return Object.assign({}, language, {
parsers: language.parsers.map(parser =>
parser === "babel" ? "babylon" : parser
)
});
}
if (
usePostCssParser &&
(language.name === "CSS" || language.group === "CSS")
) {
return Object.assign({}, language, {
parsers: ["postcss"]
});
}
return language;
});
return { languages, options };
function filterSince(object) {
return (
opts.showUnreleased ||
!("since" in object) ||
(object.since && semver.gte(version, object.since))
);
}
function filterDeprecated(object) {
return (
opts.showDeprecated ||
!("deprecated" in object) ||
(object.deprecated && semver.lt(version, object.deprecated))
);
}
function mapDeprecated(object) {
if (!object.deprecated || opts.showDeprecated) {
return object;
}
const newObject = Object.assign({}, object);
delete newObject.deprecated;
delete newObject.redirect;
return newObject;
}
function mapInternal(object) {
if (opts.showInternal) {
return object;
}
const newObject = Object.assign({}, object);
delete newObject.cliName;
delete newObject.cliCategory;
delete newObject.cliDescription;
return newObject;
}
}
module.exports = {
getSupportInfo
};