Skip to content

[QUESTION] semver.intersects('>=16.0.0 <17.0.0', '^17.0.0-0', { includePrereleases: true }) should be false #345

Open
@ljharb

Description

What / Why

semver.intersects('>=16.0.0 <17.0.0', '^17.0.0-0') should be true, and is.

semver.intersects('>=16.0.0 <17.0.0', '^17.0.0-0', { includePrereleases: true }) should be false, and is not.

I made test cases:

diff --git a/test/classes/range.js b/test/classes/range.js
index e3867a9..bf77331 100644
--- a/test/classes/range.js
+++ b/test/classes/range.js
@@ -83,7 +83,7 @@ test('tostrings', (t) => {
 })
 
 test('ranges intersect', (t) => {
-  rangeIntersection.forEach(([r0, r1, expect]) => {
+  rangeIntersection.forEach(([r0, r1, expect, includePrerelease]) => {
     t.test(`${r0} <~> ${r1}`, t => {
       const range0 = new Range(r0)
       const range1 = new Range(r1)
@@ -92,6 +92,14 @@ test('ranges intersect', (t) => {
         `${r0} <~> ${r1} objects`)
       t.equal(range1.intersects(range0), expect,
         `${r1} <~> ${r0} objects`)
+
+      if (includePrerelease) {
+        t.equal(range0.intersects(range1, { includePrerelease: true }), expect,
+          `${r0} <~> ${r1} objects`)
+        t.equal(range1.intersects(range0, { includePrerelease: true }), expect,
+          `${r1} <~> ${r0} objects`)
+      }
+
       t.end()
     })
   })
diff --git a/test/fixtures/range-intersection.js b/test/fixtures/range-intersection.js
index e46c82a..956de82 100644
--- a/test/fixtures/range-intersection.js
+++ b/test/fixtures/range-intersection.js
@@ -53,5 +53,7 @@ module.exports = [
   ['1.3.0 || <1.0.0 >2.0.0', 'x', true],
   ['1.x', '1.3.0 || <1.0.0 >2.0.0', true],
   ['*', '*', true],
-  ['x', '', true]
+  ['x', '', true],
+  ['>=16.0.0 <17.0.0', '^17.0.0-0', false],
+  ['>=16.0.0 <17.0.0', '^17.0.0-0', true, true],
 ]
diff --git a/test/ranges/intersects.js b/test/ranges/intersects.js
index e93492b..1545420 100644
--- a/test/ranges/intersects.js
+++ b/test/ranges/intersects.js
@@ -28,7 +28,7 @@ test('intersect comparators', t => {
 })
 
 test('ranges intersect', (t) => {
-  rangeIntersection.forEach(([r0, r1, expect]) => {
+  rangeIntersection.forEach(([r0, r1, expect, includePrerelease]) => {
     t.test(`${r0} <~> ${r1}`, t => {
       const range0 = new Range(r0)
       const range1 = new Range(r1)
@@ -43,6 +43,20 @@ test('ranges intersect', (t) => {
         `${r0} <~> ${r1} objects loose`)
       t.equal(intersects(range1, range0, true), expect,
         `${r1} <~> ${r0} objects loose`)
+
+      if (includePrerelease) {
+        t.equal(intersects(r1, r0, { includePrerelease: true }), expect, `${r0} <~> ${r1}`)
+        t.equal(intersects(r0, r1, { includePrerelease: true }), expect, `${r1} <~> ${r0}`)
+        t.equal(intersects(r1, r0, { loose: true, includePrerelease: true }), expect, `${r0} <~> ${r1} loose`)
+        t.equal(intersects(r0, r1, { loose: true, includePrerelease: true }), expect, `${r1} <~> ${r0} loose`)
+        t.equal(intersects(range0, range1, { includePrerelease: true }), expect, `${r0} <~> ${r1} objects`)
+        t.equal(intersects(range1, range0, { includePrerelease: true }), expect, `${r1} <~> ${r0} objects`)
+        t.equal(intersects(range0, range1, { loose: true, includePrerelease: true }), expect,
+          `${r0} <~> ${r1} objects loose`)
+        t.equal(intersects(range1, range0, { loose: true, includePrerelease: true }), expect,
+          `${r1} <~> ${r0} objects loose`)
+      }
+
       t.end()
     })
   })

but i can't figure out how to fix it.

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions