-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
223 lines (205 loc) · 5.6 KB
/
index.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import browserslist from 'browserslist'
import pico from 'picocolors'
const LIMITED_BROWSERS_COUNT = 4
const COUNTRIES_MIN_COVERAGE = 80
// https://en.wikipedia.org/wiki/List_of_countries_by_number_of_Internet_users
const COUNTRIES_10M = {
CN: 'China',
IN: 'India',
US: 'United States',
ID: 'Indonesia',
BR: 'Brazil',
NG: 'Nigeria',
BD: 'Bangladesh',
RU: 'Russia',
PK: 'Pakistan',
JP: 'Japan',
MX: 'Mexico',
IR: 'Iran',
DE: 'Germany',
PH: 'Philippines',
TR: 'Turkey',
VN: 'Vietnam',
GB: 'United Kingdom',
FR: 'France',
EG: 'Egypt',
TH: 'Thailand',
IT: 'Italy',
KR: 'South Korea',
ES: 'Spain',
PL: 'Poland',
CA: 'Canada',
AR: 'Argentina',
ZA: 'South Africa',
UA: 'Ukraine',
CO: 'Colombia',
TZ: 'Tanzania',
SA: 'Saudi Arabia',
DZ: 'Algeria',
MY: 'Malaysia',
MA: 'Morocco',
TW: 'Taiwan',
AU: 'Australia',
VE: 'Venezuela',
ET: 'Ethiopia',
IQ: 'Iraq',
UZ: 'Uzbekistan',
MM: 'Myanmar',
NP: 'Nepal',
NL: 'Netherlands',
PE: 'Peru',
GH: 'Ghana',
CL: 'Chile',
KZ: 'Kazakhstan',
RO: 'Romania',
SD: 'Sudan',
GT: 'Guatemala',
CI: 'Ivory Coast',
UG: 'Uganda',
BE: 'Belgium'
}
const MIN_WORLD_USAGE = 0.3
function getTotalCoverage(data) {
let total = 0
for (let i in data) {
total += data[i] || 0
}
return total
}
function concat(array) {
return array.join(', ').replace(/, ([^,]+)$/, ', and $1')
}
const CHECKS = {
missedNotDead(ast) {
let hasLast = ast.some(query => query.type.startsWith('last_'))
let hasNotDead = ast.some(query => query.type === 'dead' && query.not)
if (hasLast && !hasNotDead) {
return {
message:
'The `not dead` query skipped when using `last N versions` query',
fixed: ast.map(query => query.query).join(', ') + ', not dead'
}
} else {
return false
}
},
limitedBrowsers(ast) {
let browsers = new Set(ast.map(query => query.browser))
let onlyBrowsersQueries = ast.every(query => 'browser' in query)
if (onlyBrowsersQueries && browsers.size < LIMITED_BROWSERS_COUNT) {
return {
message: 'Given config is narrowly limited for specific vendors',
fixed:
ast.map(query => query.query).join(', ') +
', last 2 versions, not dead'
}
} else {
return false
}
},
countryWasIgnored(ast, browsers) {
// The Node.js is not in the Can I Use db
let browsersWithStats = browsers.filter(i => !i.startsWith('node'))
if (!browsersWithStats.length) return false
let coverage
let countries = []
let tx = 0
for (let code in COUNTRIES_10M) {
coverage = browserslist.coverage(browsersWithStats, code)
tx = 100 / getTotalCoverage(browserslist.usage[code])
if (coverage * tx < COUNTRIES_MIN_COVERAGE) {
countries.push(COUNTRIES_10M[code])
}
}
if (countries.length > 0) {
let msg = 'Less than 80% coverage in '
let names = countries.map(i => '`' + i + '`')
if (names.length > 5) {
names = names.slice(0, 5).concat([`${countries.length - 5} more`])
}
return {
message: msg + concat(names) + ' regions',
fixed: [
`>${MIN_WORLD_USAGE}%`,
...ast
.filter(query => query.type !== 'popularity')
.map(query => query.query)
].join(', ')
}
} else {
return false
}
},
alreadyDead(ast) {
let hasNotDead = ast.some(query => query.type === 'dead' && query.not)
let hasDefaults = ast.some(query => query.type === 'defaults' && !query.not)
if (!hasNotDead && !hasDefaults) return false
let dead = browserslist('dead')
let duplicates = ast
.filter(query => query.type === 'browser_version' && query.not)
.map(query => {
let name = query.browser.toLowerCase()
let normalized = browserslist.aliases[name] || name
return {
double: `${normalized} ${query.version}`,
query: query.query
}
})
.filter(str => dead.includes(str.double))
if (duplicates.length > 0) {
let str =
concat(duplicates.map(i => '`not ' + i.double + '`')) + ' already in '
let fixedArray = ast.map(query => query.query)
duplicates.forEach(rule => {
let ruleInFixed = fixedArray.indexOf(rule.query)
if (ruleInFixed !== -1) {
fixedArray.splice(ruleInFixed, 1)
}
})
if (hasNotDead) {
return {
message: str + '`not dead`',
fixed: fixedArray.join(', ')
}
} else {
return {
message: str + '`defaults`',
fixed: fixedArray.join(', ')
}
}
} else {
return false
}
}
}
export function lint(queries, opts) {
let ast = browserslist.parse(queries, opts)
let browsers = browserslist(queries, opts)
let problems = []
for (let id in CHECKS) {
let result = CHECKS[id](ast, browsers)
if (result) {
let { message, fixed } = result
problems.push({ id, message, fixed })
}
}
return problems
}
export function formatReport(problems) {
if (!problems.length) return ''
let report = ''
let maxProblemIdWidth = problems.reduce((prev, problem) => {
return Math.max(prev, problem.id.length)
}, 0)
let offset
report += problems.reduce((str, problem) => {
offset = maxProblemIdWidth - problem.id.length + 3
offset = Array(offset).join(' ')
let message = problem.message.replace(/`[^`]+`/g, code => {
return pico.yellow(code.slice(1, -1))
})
return str + pico.gray(problem.id) + offset + message + '\n'
}, '')
report += '\n' + pico.red('✖ ') + problems.length + ' problems\n'
return report
}