range.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // hoisted class for cyclic dependency
  2. class Range {
  3. constructor (range, options) {
  4. if (!options || typeof options !== 'object') {
  5. options = {
  6. loose: !!options,
  7. includePrerelease: false
  8. }
  9. }
  10. if (range instanceof Range) {
  11. if (
  12. range.loose === !!options.loose &&
  13. range.includePrerelease === !!options.includePrerelease
  14. ) {
  15. return range
  16. } else {
  17. return new Range(range.raw, options)
  18. }
  19. }
  20. if (range instanceof Comparator) {
  21. // just put it in the set and return
  22. this.raw = range.value
  23. this.set = [[range]]
  24. this.format()
  25. return this
  26. }
  27. this.options = options
  28. this.loose = !!options.loose
  29. this.includePrerelease = !!options.includePrerelease
  30. // First, split based on boolean or ||
  31. this.raw = range
  32. this.set = range
  33. .split(/\s*\|\|\s*/)
  34. // map the range to a 2d array of comparators
  35. .map(range => this.parseRange(range.trim()))
  36. // throw out any comparator lists that are empty
  37. // this generally means that it was not a valid range, which is allowed
  38. // in loose mode, but will still throw if the WHOLE range is invalid.
  39. .filter(c => c.length)
  40. if (!this.set.length) {
  41. throw new TypeError(`Invalid SemVer Range: ${range}`)
  42. }
  43. this.format()
  44. }
  45. format () {
  46. this.range = this.set
  47. .map((comps) => {
  48. return comps.join(' ').trim()
  49. })
  50. .join('||')
  51. .trim()
  52. return this.range
  53. }
  54. toString () {
  55. return this.range
  56. }
  57. parseRange (range) {
  58. const loose = this.options.loose
  59. range = range.trim()
  60. // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
  61. const hr = loose ? re[t.HYPHENRANGELOOSE] : re[t.HYPHENRANGE]
  62. range = range.replace(hr, hyphenReplace)
  63. debug('hyphen replace', range)
  64. // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
  65. range = range.replace(re[t.COMPARATORTRIM], comparatorTrimReplace)
  66. debug('comparator trim', range, re[t.COMPARATORTRIM])
  67. // `~ 1.2.3` => `~1.2.3`
  68. range = range.replace(re[t.TILDETRIM], tildeTrimReplace)
  69. // `^ 1.2.3` => `^1.2.3`
  70. range = range.replace(re[t.CARETTRIM], caretTrimReplace)
  71. // normalize spaces
  72. range = range.split(/\s+/).join(' ')
  73. // At this point, the range is completely trimmed and
  74. // ready to be split into comparators.
  75. const compRe = loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
  76. return range
  77. .split(' ')
  78. .map(comp => parseComparator(comp, this.options))
  79. .join(' ')
  80. .split(/\s+/)
  81. // in loose mode, throw out any that are not valid comparators
  82. .filter(this.options.loose ? comp => !!comp.match(compRe) : () => true)
  83. .map(comp => new Comparator(comp, this.options))
  84. }
  85. intersects (range, options) {
  86. if (!(range instanceof Range)) {
  87. throw new TypeError('a Range is required')
  88. }
  89. return this.set.some((thisComparators) => {
  90. return (
  91. isSatisfiable(thisComparators, options) &&
  92. range.set.some((rangeComparators) => {
  93. return (
  94. isSatisfiable(rangeComparators, options) &&
  95. thisComparators.every((thisComparator) => {
  96. return rangeComparators.every((rangeComparator) => {
  97. return thisComparator.intersects(rangeComparator, options)
  98. })
  99. })
  100. )
  101. })
  102. )
  103. })
  104. }
  105. // if ANY of the sets match ALL of its comparators, then pass
  106. test (version) {
  107. if (!version) {
  108. return false
  109. }
  110. if (typeof version === 'string') {
  111. try {
  112. version = new SemVer(version, this.options)
  113. } catch (er) {
  114. return false
  115. }
  116. }
  117. for (let i = 0; i < this.set.length; i++) {
  118. if (testSet(this.set[i], version, this.options)) {
  119. return true
  120. }
  121. }
  122. return false
  123. }
  124. }
  125. module.exports = Range
  126. const Comparator = require('./comparator')
  127. const debug = require('../internal/debug')
  128. const SemVer = require('./semver')
  129. const {
  130. re,
  131. t,
  132. comparatorTrimReplace,
  133. tildeTrimReplace,
  134. caretTrimReplace
  135. } = require('../internal/re')
  136. // take a set of comparators and determine whether there
  137. // exists a version which can satisfy it
  138. const isSatisfiable = (comparators, options) => {
  139. let result = true
  140. const remainingComparators = comparators.slice()
  141. let testComparator = remainingComparators.pop()
  142. while (result && remainingComparators.length) {
  143. result = remainingComparators.every((otherComparator) => {
  144. return testComparator.intersects(otherComparator, options)
  145. })
  146. testComparator = remainingComparators.pop()
  147. }
  148. return result
  149. }
  150. // comprised of xranges, tildes, stars, and gtlt's at this point.
  151. // already replaced the hyphen ranges
  152. // turn into a set of JUST comparators.
  153. const parseComparator = (comp, options) => {
  154. debug('comp', comp, options)
  155. comp = replaceCarets(comp, options)
  156. debug('caret', comp)
  157. comp = replaceTildes(comp, options)
  158. debug('tildes', comp)
  159. comp = replaceXRanges(comp, options)
  160. debug('xrange', comp)
  161. comp = replaceStars(comp, options)
  162. debug('stars', comp)
  163. return comp
  164. }
  165. const isX = id => !id || id.toLowerCase() === 'x' || id === '*'
  166. // ~, ~> --> * (any, kinda silly)
  167. // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
  168. // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
  169. // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
  170. // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
  171. // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
  172. const replaceTildes = (comp, options) =>
  173. comp.trim().split(/\s+/).map((comp) => {
  174. return replaceTilde(comp, options)
  175. }).join(' ')
  176. const replaceTilde = (comp, options) => {
  177. const r = options.loose ? re[t.TILDELOOSE] : re[t.TILDE]
  178. return comp.replace(r, (_, M, m, p, pr) => {
  179. debug('tilde', comp, _, M, m, p, pr)
  180. let ret
  181. if (isX(M)) {
  182. ret = ''
  183. } else if (isX(m)) {
  184. ret = `>=${M}.0.0 <${+M + 1}.0.0`
  185. } else if (isX(p)) {
  186. // ~1.2 == >=1.2.0 <1.3.0
  187. ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0`
  188. } else if (pr) {
  189. debug('replaceTilde pr', pr)
  190. ret = `>=${M}.${m}.${p}-${pr
  191. } <${M}.${+m + 1}.0`
  192. } else {
  193. // ~1.2.3 == >=1.2.3 <1.3.0
  194. ret = `>=${M}.${m}.${p
  195. } <${M}.${+m + 1}.0`
  196. }
  197. debug('tilde return', ret)
  198. return ret
  199. })
  200. }
  201. // ^ --> * (any, kinda silly)
  202. // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
  203. // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
  204. // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
  205. // ^1.2.3 --> >=1.2.3 <2.0.0
  206. // ^1.2.0 --> >=1.2.0 <2.0.0
  207. const replaceCarets = (comp, options) =>
  208. comp.trim().split(/\s+/).map((comp) => {
  209. return replaceCaret(comp, options)
  210. }).join(' ')
  211. const replaceCaret = (comp, options) => {
  212. debug('caret', comp, options)
  213. const r = options.loose ? re[t.CARETLOOSE] : re[t.CARET]
  214. return comp.replace(r, (_, M, m, p, pr) => {
  215. debug('caret', comp, _, M, m, p, pr)
  216. let ret
  217. if (isX(M)) {
  218. ret = ''
  219. } else if (isX(m)) {
  220. ret = `>=${M}.0.0 <${+M + 1}.0.0`
  221. } else if (isX(p)) {
  222. if (M === '0') {
  223. ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0`
  224. } else {
  225. ret = `>=${M}.${m}.0 <${+M + 1}.0.0`
  226. }
  227. } else if (pr) {
  228. debug('replaceCaret pr', pr)
  229. if (M === '0') {
  230. if (m === '0') {
  231. ret = `>=${M}.${m}.${p}-${pr
  232. } <${M}.${m}.${+p + 1}`
  233. } else {
  234. ret = `>=${M}.${m}.${p}-${pr
  235. } <${M}.${+m + 1}.0`
  236. }
  237. } else {
  238. ret = `>=${M}.${m}.${p}-${pr
  239. } <${+M + 1}.0.0`
  240. }
  241. } else {
  242. debug('no pr')
  243. if (M === '0') {
  244. if (m === '0') {
  245. ret = `>=${M}.${m}.${p
  246. } <${M}.${m}.${+p + 1}`
  247. } else {
  248. ret = `>=${M}.${m}.${p
  249. } <${M}.${+m + 1}.0`
  250. }
  251. } else {
  252. ret = `>=${M}.${m}.${p
  253. } <${+M + 1}.0.0`
  254. }
  255. }
  256. debug('caret return', ret)
  257. return ret
  258. })
  259. }
  260. const replaceXRanges = (comp, options) => {
  261. debug('replaceXRanges', comp, options)
  262. return comp.split(/\s+/).map((comp) => {
  263. return replaceXRange(comp, options)
  264. }).join(' ')
  265. }
  266. const replaceXRange = (comp, options) => {
  267. comp = comp.trim()
  268. const r = options.loose ? re[t.XRANGELOOSE] : re[t.XRANGE]
  269. return comp.replace(r, (ret, gtlt, M, m, p, pr) => {
  270. debug('xRange', comp, ret, gtlt, M, m, p, pr)
  271. const xM = isX(M)
  272. const xm = xM || isX(m)
  273. const xp = xm || isX(p)
  274. const anyX = xp
  275. if (gtlt === '=' && anyX) {
  276. gtlt = ''
  277. }
  278. // if we're including prereleases in the match, then we need
  279. // to fix this to -0, the lowest possible prerelease value
  280. pr = options.includePrerelease ? '-0' : ''
  281. if (xM) {
  282. if (gtlt === '>' || gtlt === '<') {
  283. // nothing is allowed
  284. ret = '<0.0.0-0'
  285. } else {
  286. // nothing is forbidden
  287. ret = '*'
  288. }
  289. } else if (gtlt && anyX) {
  290. // we know patch is an x, because we have any x at all.
  291. // replace X with 0
  292. if (xm) {
  293. m = 0
  294. }
  295. p = 0
  296. if (gtlt === '>') {
  297. // >1 => >=2.0.0
  298. // >1.2 => >=1.3.0
  299. gtlt = '>='
  300. if (xm) {
  301. M = +M + 1
  302. m = 0
  303. p = 0
  304. } else {
  305. m = +m + 1
  306. p = 0
  307. }
  308. } else if (gtlt === '<=') {
  309. // <=0.7.x is actually <0.8.0, since any 0.7.x should
  310. // pass. Similarly, <=7.x is actually <8.0.0, etc.
  311. gtlt = '<'
  312. if (xm) {
  313. M = +M + 1
  314. } else {
  315. m = +m + 1
  316. }
  317. }
  318. ret = `${gtlt + M}.${m}.${p}${pr}`
  319. } else if (xm) {
  320. ret = `>=${M}.0.0${pr} <${+M + 1}.0.0${pr}`
  321. } else if (xp) {
  322. ret = `>=${M}.${m}.0${pr
  323. } <${M}.${+m + 1}.0${pr}`
  324. }
  325. debug('xRange return', ret)
  326. return ret
  327. })
  328. }
  329. // Because * is AND-ed with everything else in the comparator,
  330. // and '' means "any version", just remove the *s entirely.
  331. const replaceStars = (comp, options) => {
  332. debug('replaceStars', comp, options)
  333. // Looseness is ignored here. star is always as loose as it gets!
  334. return comp.trim().replace(re[t.STAR], '')
  335. }
  336. // This function is passed to string.replace(re[t.HYPHENRANGE])
  337. // M, m, patch, prerelease, build
  338. // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
  339. // 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
  340. // 1.2 - 3.4 => >=1.2.0 <3.5.0
  341. const hyphenReplace = ($0,
  342. from, fM, fm, fp, fpr, fb,
  343. to, tM, tm, tp, tpr, tb) => {
  344. if (isX(fM)) {
  345. from = ''
  346. } else if (isX(fm)) {
  347. from = `>=${fM}.0.0`
  348. } else if (isX(fp)) {
  349. from = `>=${fM}.${fm}.0`
  350. } else {
  351. from = `>=${from}`
  352. }
  353. if (isX(tM)) {
  354. to = ''
  355. } else if (isX(tm)) {
  356. to = `<${+tM + 1}.0.0`
  357. } else if (isX(tp)) {
  358. to = `<${tM}.${+tm + 1}.0`
  359. } else if (tpr) {
  360. to = `<=${tM}.${tm}.${tp}-${tpr}`
  361. } else {
  362. to = `<=${to}`
  363. }
  364. return (`${from} ${to}`).trim()
  365. }
  366. const testSet = (set, version, options) => {
  367. for (let i = 0; i < set.length; i++) {
  368. if (!set[i].test(version)) {
  369. return false
  370. }
  371. }
  372. if (version.prerelease.length && !options.includePrerelease) {
  373. // Find the set of versions that are allowed to have prereleases
  374. // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
  375. // That should allow `1.2.3-pr.2` to pass.
  376. // However, `1.2.4-alpha.notready` should NOT be allowed,
  377. // even though it's within the range set by the comparators.
  378. for (let i = 0; i < set.length; i++) {
  379. debug(set[i].semver)
  380. if (set[i].semver === Comparator.ANY) {
  381. continue
  382. }
  383. if (set[i].semver.prerelease.length > 0) {
  384. const allowed = set[i].semver
  385. if (allowed.major === version.major &&
  386. allowed.minor === version.minor &&
  387. allowed.patch === version.patch) {
  388. return true
  389. }
  390. }
  391. }
  392. // Version has a -pre, but it's not one of the ones we like.
  393. return false
  394. }
  395. return true
  396. }