Repository: atlassian/changesets
Pull request #454 (2020-09-11 07:25:55)

Remove packagesByNameWithVersionsFromBeforePreModeIfInPreMode and releasesFromUnfilteredChangesets
cc @mitchellhamilton 😱 

~it's super weird because I would totally expect for tests added here to fail: https://github.com/atlassian/changesets/pull/263 , need to investigate why they pass~ they pass because we've removed both of those variables and it's able to compute good new version based on the given information

changeset-bot[bot]: ###  🦋  Changeset is good to go

Latest commit: abdac73acdc089505f272f1c0921c1e12e5f4bb2

**We got this.**

<details><summary>This PR includes changesets to release 2 packages</summary>

  | Name                              | Type  |
| --------------------------------- | ----- |
| @changesets/cli                   | Patch |
| @changesets/assemble-release-plan | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/atlassian/changesets/new/test-cleanup?filename=.changeset/pink-rats-accept.md&value=---%0A%22%40changesets%2Fassemble-release-plan%22%3A%20patch%0A%22%40changesets%2Fcli%22%3A%20patch%0A---%0A%0A%5Bdo%20not%20merge%5D%20Just%20a%20test%20cleanup%0A)


Andarist: This simplifies the code quite a bit and makes it easier to understand overall. There is only a small behavior change for `@changesets/assemble-release-plan` which after discussing with @mitchellhamilton is acceptable if we make this a major bump.

Patch:
diff --git a/.changeset/gorgeous-weeks-begin.md b/.changeset/gorgeous-weeks-begin.md
new file mode 100644
index 0000000..430e7e5
--- /dev/null
+++ b/.changeset/gorgeous-weeks-begin.md
@@ -0,0 +1,5 @@
+---
+"@changesets/assemble-release-plan": major
+---
+
+Returned releases in pre mode will have type of the highest bump type within the current release now, instead of having the highest bump type from among all changesets within this pre mode. So if you release a new prerelease including a major bump and later release the same package with a minor bump the assembled release will be of type minor - the final computed version will, of course, still be the next one for a major bump.
diff --git a/packages/assemble-release-plan/src/index.ts b/packages/assemble-release-plan/src/index.ts
index efb23f7..a726461 100644
--- a/packages/assemble-release-plan/src/index.ts
+++ b/packages/assemble-release-plan/src/index.ts
@@ -92,15 +92,10 @@ function assembleReleasePlan(
     snapshotSuffix = getSnapshotSuffix(snapshot);
   }
 
-  let packagesByNameWithVersionsFromBeforePreModeIfInPreMode = new Map(
-    packages.packages.map(x => [x.packageJson.name, x])
-  );
   let packagesByName = new Map(
-    packagesByNameWithVersionsFromBeforePreModeIfInPreMode
+    packages.packages.map(x => [x.packageJson.name, x])
   );
 
-  let unfilteredChangesets = changesets;
-
   let preVersions = new Map();
   if (updatedPreState !== undefined) {
     for (let pkg of packages.packages) {
@@ -129,11 +124,7 @@ function assembleReleasePlan(
       let highestPreVersion = 0;
       for (let linkedPackage of linkedGroup) {
         highestPreVersion = Math.max(
-          getPreVersion(
-            packagesByNameWithVersionsFromBeforePreModeIfInPreMode.get(
-              linkedPackage
-            )!.packageJson.version
-          ),
+          getPreVersion(packagesByName.get(linkedPackage)!.packageJson.version),
           highestPreVersion
         );
       }
@@ -141,29 +132,12 @@ function assembleReleasePlan(
         preVersions.set(linkedPackage, highestPreVersion);
       }
     }
-
-    for (let pkg of packages.packages) {
-      packagesByNameWithVersionsFromBeforePreModeIfInPreMode.set(
-        pkg.packageJson.name,
-        {
-          ...pkg,
-          packageJson: {
-            ...pkg.packageJson,
-            version: updatedPreState.initialVersions[pkg.packageJson.name]
-          }
-        }
-      );
-    }
   }
 
   // releases is, at this point a list of all packages we are going to releases,
   // flattened down to one release per package, having a reference back to their
   // changesets, and with a calculated new versions
-  let releases = flattenReleases(
-    changesets,
-    packagesByNameWithVersionsFromBeforePreModeIfInPreMode,
-    config.ignore
-  );
+  let releases = flattenReleases(changesets, packagesByName, config.ignore);
 
   let preInfo: PreInfo | undefined =
     updatedPreState === undefined
@@ -180,7 +154,7 @@ function assembleReleasePlan(
     // The map passed in to determineDependents will be mutated
     let dependentAdded = determineDependents({
       releases,
-      packagesByName: packagesByNameWithVersionsFromBeforePreModeIfInPreMode,
+      packagesByName,
       dependencyGraph,
       preInfo,
       ignoredPackages: config.ignore,
@@ -212,35 +186,6 @@ function assembleReleasePlan(
           }
         }
       }
-    } else {
-      // for every release in pre mode, we want versions to be bumped to the highest bump type
-      // across all the changesets even if the package doesn't have a changeset that releases
-      // to the highest bump type in a given release in pre mode and importantly
-      // we don't want to add any new releases, we only want to update ones that will already happen
-      // because if they're not being released, the version will already have been bumped with the highest bump type
-      let releasesFromUnfilteredChangesets = flattenReleases(
-        unfilteredChangesets,
-        packagesByNameWithVersionsFromBeforePreModeIfInPreMode,
-        config.ignore
-      );
-
-      releases.forEach((value, key) => {
-        let releaseFromUnfilteredChangesets = releasesFromUnfilteredChangesets.get(
-          key
-        );
-        if (releaseFromUnfilteredChangesets === undefined) {
-          return;
-        }
-
-        releases.set(key, {
-          ...value,
-          // note that we're only setting the type, not the changesets which could be different(the name and oldVersion would be the same so they don't matter)
-          // because the changesets on a given release refer to why a given package is being released
-          // NOT why it's being released with a given bump type
-          // (the bump type could change because of this, linked or peer dependencies)
-          type: releaseFromUnfilteredChangesets.type
-        });
-      });
     }
   }
 


Test patch:
diff --git a/packages/assemble-release-plan/src/index.test.ts b/packages/assemble-release-plan/src/index.test.ts
index 8ba3bda..a06a2c4 100644
--- a/packages/assemble-release-plan/src/index.test.ts
+++ b/packages/assemble-release-plan/src/index.test.ts
@@ -504,8 +504,8 @@ Mixed changesets that contain both ignored and not ignored packages are not allo
 `);
   });
 
-  describe("pre mode exit", () => {
-    it("should not generate a release for package that has no changesets and is not a dependent of any packages being released", () => {
+  describe("pre mode", () => {
+    it("should not generate a release for package that has no changesets and is not a dependent of any packages being released when exiting pre mode", () => {
       const { releases } = assembleReleasePlan(
         setup.changesets,
         setup.packages,
@@ -524,6 +524,53 @@ Mixed changesets that contain both ignored and not ignored packages are not allo
       expect(releases[0].name).toEqual("pkg-a");
       expect(releases[0].newVersion).toEqual("1.0.1");
     });
+
+    it("should return a release with the highest bump type within the current release despite of having a higher release among previous prereleases", () => {
+      // previous release
+      setup.addChangeset({
+        id: "major-bumping-one",
+        releases: [
+          {
+            name: "pkg-a",
+            type: "major"
+          }
+        ]
+      });
+      setup.updatePackage("pkg-a", "2.0.0-next.0");
+
+      // current release
+      setup.addChangeset({
+        id: "minor-bumping-one",
+        releases: [
+          {
+            name: "pkg-a",
+            type: "minor"
+          }
+        ]
+      });
+      const { releases } = assembleReleasePlan(
+        setup.changesets,
+        setup.packages,
+        {
+          ...defaultConfig
+        },
+        {
+          changesets: ["major-bumping-one"],
+          tag: "next",
+          initialVersions: {
+            "pkg-a": "1.0.0",
+            "pkg-b": "1.0.0",
+            "pkg-c": "1.0.0"
+          },
+          mode: "pre"
+        }
+      );
+
+      expect(releases.length).toEqual(1);
+      expect(releases[0].name).toEqual("pkg-a");
+      expect(releases[0].newVersion).toEqual("2.0.0-next.1");
+      expect(releases[0].type).toEqual("minor");
+    });
   });
 });