1 | const { MAX_SAFE_COMPONENT_LENGTH } = require('./constants')
|
---|
2 | const debug = require('./debug')
|
---|
3 | exports = module.exports = {}
|
---|
4 |
|
---|
5 | // The actual regexps go on exports.re
|
---|
6 | const re = exports.re = []
|
---|
7 | const src = exports.src = []
|
---|
8 | const t = exports.t = {}
|
---|
9 | let R = 0
|
---|
10 |
|
---|
11 | const createToken = (name, value, isGlobal) => {
|
---|
12 | const index = R++
|
---|
13 | debug(index, value)
|
---|
14 | t[name] = index
|
---|
15 | src[index] = value
|
---|
16 | re[index] = new RegExp(value, isGlobal ? 'g' : undefined)
|
---|
17 | }
|
---|
18 |
|
---|
19 | // The following Regular Expressions can be used for tokenizing,
|
---|
20 | // validating, and parsing SemVer version strings.
|
---|
21 |
|
---|
22 | // ## Numeric Identifier
|
---|
23 | // A single `0`, or a non-zero digit followed by zero or more digits.
|
---|
24 |
|
---|
25 | createToken('NUMERICIDENTIFIER', '0|[1-9]\\d*')
|
---|
26 | createToken('NUMERICIDENTIFIERLOOSE', '[0-9]+')
|
---|
27 |
|
---|
28 | // ## Non-numeric Identifier
|
---|
29 | // Zero or more digits, followed by a letter or hyphen, and then zero or
|
---|
30 | // more letters, digits, or hyphens.
|
---|
31 |
|
---|
32 | createToken('NONNUMERICIDENTIFIER', '\\d*[a-zA-Z-][a-zA-Z0-9-]*')
|
---|
33 |
|
---|
34 | // ## Main Version
|
---|
35 | // Three dot-separated numeric identifiers.
|
---|
36 |
|
---|
37 | createToken('MAINVERSION', `(${src[t.NUMERICIDENTIFIER]})\\.` +
|
---|
38 | `(${src[t.NUMERICIDENTIFIER]})\\.` +
|
---|
39 | `(${src[t.NUMERICIDENTIFIER]})`)
|
---|
40 |
|
---|
41 | createToken('MAINVERSIONLOOSE', `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
|
---|
42 | `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
|
---|
43 | `(${src[t.NUMERICIDENTIFIERLOOSE]})`)
|
---|
44 |
|
---|
45 | // ## Pre-release Version Identifier
|
---|
46 | // A numeric identifier, or a non-numeric identifier.
|
---|
47 |
|
---|
48 | createToken('PRERELEASEIDENTIFIER', `(?:${src[t.NUMERICIDENTIFIER]
|
---|
49 | }|${src[t.NONNUMERICIDENTIFIER]})`)
|
---|
50 |
|
---|
51 | createToken('PRERELEASEIDENTIFIERLOOSE', `(?:${src[t.NUMERICIDENTIFIERLOOSE]
|
---|
52 | }|${src[t.NONNUMERICIDENTIFIER]})`)
|
---|
53 |
|
---|
54 | // ## Pre-release Version
|
---|
55 | // Hyphen, followed by one or more dot-separated pre-release version
|
---|
56 | // identifiers.
|
---|
57 |
|
---|
58 | createToken('PRERELEASE', `(?:-(${src[t.PRERELEASEIDENTIFIER]
|
---|
59 | }(?:\\.${src[t.PRERELEASEIDENTIFIER]})*))`)
|
---|
60 |
|
---|
61 | createToken('PRERELEASELOOSE', `(?:-?(${src[t.PRERELEASEIDENTIFIERLOOSE]
|
---|
62 | }(?:\\.${src[t.PRERELEASEIDENTIFIERLOOSE]})*))`)
|
---|
63 |
|
---|
64 | // ## Build Metadata Identifier
|
---|
65 | // Any combination of digits, letters, or hyphens.
|
---|
66 |
|
---|
67 | createToken('BUILDIDENTIFIER', '[0-9A-Za-z-]+')
|
---|
68 |
|
---|
69 | // ## Build Metadata
|
---|
70 | // Plus sign, followed by one or more period-separated build metadata
|
---|
71 | // identifiers.
|
---|
72 |
|
---|
73 | createToken('BUILD', `(?:\\+(${src[t.BUILDIDENTIFIER]
|
---|
74 | }(?:\\.${src[t.BUILDIDENTIFIER]})*))`)
|
---|
75 |
|
---|
76 | // ## Full Version String
|
---|
77 | // A main version, followed optionally by a pre-release version and
|
---|
78 | // build metadata.
|
---|
79 |
|
---|
80 | // Note that the only major, minor, patch, and pre-release sections of
|
---|
81 | // the version string are capturing groups. The build metadata is not a
|
---|
82 | // capturing group, because it should not ever be used in version
|
---|
83 | // comparison.
|
---|
84 |
|
---|
85 | createToken('FULLPLAIN', `v?${src[t.MAINVERSION]
|
---|
86 | }${src[t.PRERELEASE]}?${
|
---|
87 | src[t.BUILD]}?`)
|
---|
88 |
|
---|
89 | createToken('FULL', `^${src[t.FULLPLAIN]}$`)
|
---|
90 |
|
---|
91 | // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
|
---|
92 | // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
|
---|
93 | // common in the npm registry.
|
---|
94 | createToken('LOOSEPLAIN', `[v=\\s]*${src[t.MAINVERSIONLOOSE]
|
---|
95 | }${src[t.PRERELEASELOOSE]}?${
|
---|
96 | src[t.BUILD]}?`)
|
---|
97 |
|
---|
98 | createToken('LOOSE', `^${src[t.LOOSEPLAIN]}$`)
|
---|
99 |
|
---|
100 | createToken('GTLT', '((?:<|>)?=?)')
|
---|
101 |
|
---|
102 | // Something like "2.*" or "1.2.x".
|
---|
103 | // Note that "x.x" is a valid xRange identifer, meaning "any version"
|
---|
104 | // Only the first item is strictly required.
|
---|
105 | createToken('XRANGEIDENTIFIERLOOSE', `${src[t.NUMERICIDENTIFIERLOOSE]}|x|X|\\*`)
|
---|
106 | createToken('XRANGEIDENTIFIER', `${src[t.NUMERICIDENTIFIER]}|x|X|\\*`)
|
---|
107 |
|
---|
108 | createToken('XRANGEPLAIN', `[v=\\s]*(${src[t.XRANGEIDENTIFIER]})` +
|
---|
109 | `(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
|
---|
110 | `(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
|
---|
111 | `(?:${src[t.PRERELEASE]})?${
|
---|
112 | src[t.BUILD]}?` +
|
---|
113 | `)?)?`)
|
---|
114 |
|
---|
115 | createToken('XRANGEPLAINLOOSE', `[v=\\s]*(${src[t.XRANGEIDENTIFIERLOOSE]})` +
|
---|
116 | `(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
|
---|
117 | `(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
|
---|
118 | `(?:${src[t.PRERELEASELOOSE]})?${
|
---|
119 | src[t.BUILD]}?` +
|
---|
120 | `)?)?`)
|
---|
121 |
|
---|
122 | createToken('XRANGE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAIN]}$`)
|
---|
123 | createToken('XRANGELOOSE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAINLOOSE]}$`)
|
---|
124 |
|
---|
125 | // Coercion.
|
---|
126 | // Extract anything that could conceivably be a part of a valid semver
|
---|
127 | createToken('COERCE', `${'(^|[^\\d])' +
|
---|
128 | '(\\d{1,'}${MAX_SAFE_COMPONENT_LENGTH}})` +
|
---|
129 | `(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
|
---|
130 | `(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
|
---|
131 | `(?:$|[^\\d])`)
|
---|
132 | createToken('COERCERTL', src[t.COERCE], true)
|
---|
133 |
|
---|
134 | // Tilde ranges.
|
---|
135 | // Meaning is "reasonably at or greater than"
|
---|
136 | createToken('LONETILDE', '(?:~>?)')
|
---|
137 |
|
---|
138 | createToken('TILDETRIM', `(\\s*)${src[t.LONETILDE]}\\s+`, true)
|
---|
139 | exports.tildeTrimReplace = '$1~'
|
---|
140 |
|
---|
141 | createToken('TILDE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAIN]}$`)
|
---|
142 | createToken('TILDELOOSE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAINLOOSE]}$`)
|
---|
143 |
|
---|
144 | // Caret ranges.
|
---|
145 | // Meaning is "at least and backwards compatible with"
|
---|
146 | createToken('LONECARET', '(?:\\^)')
|
---|
147 |
|
---|
148 | createToken('CARETTRIM', `(\\s*)${src[t.LONECARET]}\\s+`, true)
|
---|
149 | exports.caretTrimReplace = '$1^'
|
---|
150 |
|
---|
151 | createToken('CARET', `^${src[t.LONECARET]}${src[t.XRANGEPLAIN]}$`)
|
---|
152 | createToken('CARETLOOSE', `^${src[t.LONECARET]}${src[t.XRANGEPLAINLOOSE]}$`)
|
---|
153 |
|
---|
154 | // A simple gt/lt/eq thing, or just "" to indicate "any version"
|
---|
155 | createToken('COMPARATORLOOSE', `^${src[t.GTLT]}\\s*(${src[t.LOOSEPLAIN]})$|^$`)
|
---|
156 | createToken('COMPARATOR', `^${src[t.GTLT]}\\s*(${src[t.FULLPLAIN]})$|^$`)
|
---|
157 |
|
---|
158 | // An expression to strip any whitespace between the gtlt and the thing
|
---|
159 | // it modifies, so that `> 1.2.3` ==> `>1.2.3`
|
---|
160 | createToken('COMPARATORTRIM', `(\\s*)${src[t.GTLT]
|
---|
161 | }\\s*(${src[t.LOOSEPLAIN]}|${src[t.XRANGEPLAIN]})`, true)
|
---|
162 | exports.comparatorTrimReplace = '$1$2$3'
|
---|
163 |
|
---|
164 | // Something like `1.2.3 - 1.2.4`
|
---|
165 | // Note that these all use the loose form, because they'll be
|
---|
166 | // checked against either the strict or loose comparator form
|
---|
167 | // later.
|
---|
168 | createToken('HYPHENRANGE', `^\\s*(${src[t.XRANGEPLAIN]})` +
|
---|
169 | `\\s+-\\s+` +
|
---|
170 | `(${src[t.XRANGEPLAIN]})` +
|
---|
171 | `\\s*$`)
|
---|
172 |
|
---|
173 | createToken('HYPHENRANGELOOSE', `^\\s*(${src[t.XRANGEPLAINLOOSE]})` +
|
---|
174 | `\\s+-\\s+` +
|
---|
175 | `(${src[t.XRANGEPLAINLOOSE]})` +
|
---|
176 | `\\s*$`)
|
---|
177 |
|
---|
178 | // Star ranges basically just allow anything at all.
|
---|
179 | createToken('STAR', '(<|>)?=?\\s*\\*')
|
---|
180 | // >=0.0.0 is like a star
|
---|
181 | createToken('GTE0', '^\\s*>=\\s*0\.0\.0\\s*$')
|
---|
182 | createToken('GTE0PRE', '^\\s*>=\\s*0\.0\.0-0\\s*$')
|
---|