forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliquid-versioning.js
312 lines (279 loc) · 10.6 KB
/
liquid-versioning.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import semver from 'semver'
import { TokenKind } from 'liquidjs'
import { addError } from 'markdownlint-rule-helpers'
import { getRange, addFixErrorDetail } from '../helpers/utils.js'
import { allVersions, allVersionShortnames } from '#src/versions/lib/all-versions.js'
import {
supported,
next,
nextNext,
deprecated,
} from '#src/versions/lib/enterprise-server-releases.js'
import allowedVersionOperators from '#src/content-render/liquid/ifversion-supported-operators.js'
import { getDeepDataByLanguage } from '#src/data-directory/lib/get-data.js'
import getApplicableVersions from '#src/versions/lib/get-applicable-versions.js'
import { getLiquidTokens, getPositionData } from '../helpers/liquid-utils.js'
const allShortnames = Object.keys(allVersionShortnames)
const getAllPossibleVersionNames = memoize(() => {
// This function might appear "slow" but it's wrapped in a memoizer
// so it's only every executed once for all files that the
// Liquid linting rule functions on.
// The third argument passed to getDeepDataByLanguage() is only
// there for the sake of being able to write a unit test on these
// lint functions.
return new Set([...Object.keys(getAllFeatures()), ...allShortnames])
})
const getAllFeatures = memoize(() => getDeepDataByLanguage('features', 'en', process.env.ROOT))
const allVersionNames = Object.keys(allVersions)
function isAllVersions(versions) {
if (versions.length === allVersionNames.length) {
return versions.every((version) => allVersionNames.includes(version))
}
return false
}
function memoize(func) {
let cached = null
return () => {
if (!cached) {
cached = func()
}
return cached
}
}
export const liquidIfTags = {
names: ['GHD019', 'liquid-if-tags'],
description:
'Liquid `ifversion` tags should be used instead of `if` tags when the argument is a valid version',
tags: ['liquid', 'versioning'],
function: (params, onError) => {
const content = params.lines.join('\n')
const tokens = getLiquidTokens(content).filter(
(token) =>
token.kind === TokenKind.Tag &&
token.name === 'if' &&
token.args.split(/\s+/).some((arg) => getAllPossibleVersionNames().has(arg)),
)
for (const token of tokens) {
const args = token.args
const { lineNumber } = getPositionData(token, params.lines)
addFixErrorDetail(
onError,
lineNumber,
token.content.replace('if', 'ifversion'),
token.content,
getRange(token.content, args),
null, // No fix possible
)
}
},
}
export const liquidIfVersionTags = {
names: ['GHD020', 'liquid-ifversion-tags'],
description: 'Liquid `ifversion` tags should contain valid version names as arguments',
tags: ['liquid', 'versioning'],
function: (params, onError) => {
const content = params.lines.join('\n')
const tokens = getLiquidTokens(content)
.filter((token) => token.kind === TokenKind.Tag)
.filter((token) => token.name === 'ifversion' || token.name === 'elsif')
for (const token of tokens) {
const args = token.args
const ifVersionErrors = validateIfversionConditionals(args, getAllPossibleVersionNames())
if (ifVersionErrors.length === 0) continue
const { lineNumber } = getPositionData(token, params.lines)
if (ifVersionErrors.length) {
addError(
onError,
lineNumber,
ifVersionErrors.join('. '),
token.content,
null, // getRange(token.content, args),
null, // No fix possible
)
}
}
},
}
export const liquidIfVersionVersions = {
names: ['GHD022', 'liquid-ifversion-versions'],
description: 'Liquid `ifversion` (and `elsif`) should not always be true',
tags: ['liquid', 'versioning'],
function: (params, onError) => {
const content = params.lines.join('\n')
const tokens = getLiquidTokens(content)
.filter((token) => token.kind === TokenKind.Tag)
.filter((token) => token.name === 'ifversion' || token.name === 'elsif')
const { name } = params
for (const token of tokens) {
const args = token.args
const { lineNumber } = getPositionData(token, params.lines)
try {
const errors = validateIfversionConditionalsVersions(args, getAllFeatures())
if (errors.length === 0) continue
if (errors.length) {
addError(
onError,
lineNumber,
errors.join('. '),
token.content,
null, // getRange(token.content, args),
null, // No fix possible
)
}
} catch (error) {
console.error(
`Name that caused the error: ${name}, Token args: '${args}', Line number: ${lineNumber}`,
)
throw error
}
}
},
}
function validateIfversionConditionals(cond, possibleVersionNames) {
const validateVersion = (version) => possibleVersionNames.has(version)
const errors = []
// Where `cond` is an array of strings, where each string may have one of the following space-separated formats:
// * Length 1: `<version>` (example: `fpt`)
// * Length 2: `not <version>` (example: `not ghae`)
// * Length 3: `<version> <operator> <release>` (example: `ghes > 3.0`)
//
// Note that Lengths 1 and 2 may be used with feature-based versioning, but NOT Length 3.
const condParts = cond.split(/ (or|and) /).filter((part) => !(part === 'or' || part === 'and'))
condParts.forEach((str) => {
const strParts = str.split(' ')
// if length = 1, this should be a valid short version or feature version name.
if (strParts.length === 1) {
const version = strParts[0]
const isValidVersion = validateVersion(version)
if (!isValidVersion) {
errors.push(`"${version}" is not a valid short version or feature version name`)
}
}
// if length = 2, this should be 'not' followed by a valid short version name.
if (strParts.length === 2) {
const [notKeyword, version] = strParts
const isValidVersion = validateVersion(version)
const isValid = notKeyword === 'not' && isValidVersion
if (!isValid) {
errors.push(`"${cond}" is not a valid conditional`)
}
}
// if length = 3, this should be a range in the format: ghes > 3.0
// where the first item is `ghes` (currently the only version with numbered releases),
// the second item is a supported operator, and the third is a supported GHES release.
if (strParts.length === 3) {
const [version, operator, release] = strParts
const hasSemanticVersioning = Object.values(allVersions).some(
(v) => (v.hasNumberedReleases || v.internalLatestRelease) && v.shortName === version,
)
if (!hasSemanticVersioning) {
errors.push(
`Found "${version}" inside "${cond}" with a "${operator}" operator, but "${version}" does not support semantic comparisons"`,
)
}
if (!allowedVersionOperators.includes(operator)) {
errors.push(
`Found a "${operator}" operator inside "${cond}", but "${operator}" is not supported`,
)
}
// Check that the versions in conditionals are supported
// versions of GHES or the first deprecated version. Allowing
// the first deprecated version to exist in code ensures
// allows us to deprecate the version before removing
// the old liquid content.
if (
!(
supported.includes(release) ||
release === next ||
release === nextNext ||
deprecated[0] === release
)
) {
errors.push(
`Found ${release} inside "${cond}", but ${release} is not a supported GHES release`,
)
}
}
})
return errors
}
// The reason this function is exported is because it's sufficiently
// complex that it needs to be tested in isolation.
export function validateIfversionConditionalsVersions(cond, allFeatures) {
// Suppose the cond is `ghes >3.1 or some-cool-feature` we need to open
// that `some-cool-feature` and if that has `{ghes:'>3.0', ghec:'*', fpt:'*'}`
// then *combined* versions will be `{ghes:'>3.0', ghec:'*', fpt:'*'}`.
// If the conditions use `and` then we bail because it's too complex to handle.
// Note, don't use \b (word boundary) regex because it would match `foo-and-bar`.
if (/\sand\s/.test(cond)) {
return []
}
const errors = []
const versions = {}
let hasFutureLessThan = false
for (const part of cond.split(/\sor\s/)) {
// For example `fpt or not ghec` or `not ghes or ghec or not fpt`
if (/(^|\s)not(\s|$)/.test(part)) {
// Bail because it's too complex to handle.
return []
}
for (const [ver, value] of Object.entries(getVersionsObject(part.trim(), allFeatures))) {
// If the version value is something like `<=3.0` and the versioning is set
// to `<3.19` then it means the version can *potentially* match a version
// that doesn't exist yet, but will, in the future.
if (/<=?[\d.]+/.test(value)) {
hasFutureLessThan = true
}
if (ver in versions) {
versions[ver] = lowestVersion(value, versions[ver])
} else {
versions[ver] = value
}
}
}
const applicableVersions = []
try {
applicableVersions.push(...getApplicableVersions(versions))
} catch (error) {
console.warn(`Condition '${cond}' throws an error when trying to get applicable versions`)
}
if (isAllVersions(applicableVersions) && !hasFutureLessThan) {
errors.push(
`The Liquid ifversion condition '${cond}' includes all possible versions and will always be true`,
)
}
return errors
}
function getVersionsObject(part, allFeatures) {
const versions = {}
if (part in allFeatures) {
for (const [shortName, version] of Object.entries(allFeatures[part].versions)) {
const versionOperator =
version in allFeatures ? getVersionsObject(version, allFeatures) : version
if (shortName in versions) {
versions[shortName] = lowestVersion(versionOperator, versions[shortName])
} else {
versions[shortName] = versionOperator
}
}
} else if (allShortnames.includes(part)) {
versions[part] = '*'
} else if (allShortnames.some((v) => part.startsWith(v))) {
const shortNamed = allShortnames.find((v) => part.startsWith(v))
const rest = part.replace(shortNamed, '').trim()
versions[shortNamed] = rest
} else {
throw new Error(`The version '${part}' is neither a short version name or a feature name`)
}
return versions
}
function lowestVersion(version1, version2) {
if (version1 === '*' || version2 === '*') {
return '*'
}
if (semver.lt(semver.minVersion(version1), semver.minVersion(version2))) {
return version1
} else {
return version2
}
}