Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace dequal w/ stable-hash #99

Merged
merged 3 commits into from
Jul 1, 2024

Conversation

SukkaW
Copy link
Collaborator

@SukkaW SukkaW commented Jun 30, 2024

Continues from #85.

Unlike dequal, stable-hash uses shallow comparison and referential equality only (so mutable Object, Set, and Map won't have the same hash after mutation). And stable-hash outputs meaningful hash instead of version.

cc @kosmotema

Copy link

changeset-bot bot commented Jun 30, 2024

⚠️ No Changeset found

Latest commit: fa96904

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

codesandbox-ci bot commented Jun 30, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

codecov bot commented Jun 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.23%. Comparing base (8b53954) to head (558378e).
Report is 7 commits behind head on master.

Current head 558378e differs from pull request most recent head fa96904

Please upload reports for the commit fa96904 to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #99   +/-   ##
=======================================
  Coverage   96.22%   96.23%           
=======================================
  Files          91       91           
  Lines        4399     4410   +11     
  Branches     1497     1499    +2     
=======================================
+ Hits         4233     4244   +11     
  Misses        160      160           
  Partials        6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kosmotema
Copy link

kosmotema commented Jul 1, 2024

so mutable Object, Set, and Map won't have the same hash after mutation

Hm, it seems, that they will. Here is a small example:

import assert from "node:assert";
import stableHash from "stable-hash";

const foo = {};
const hash = stableHash(foo);
foo.bar = "baz";

assert.notStrictEqual(hash, stableHash(foo));

And stable-hash outputs meaningful hash instead of version

But do we really need a meaningful hash? Besides, this hash is not a real hash (since it's variable length, so it's more like a stringifier) and on some of our tests the full context hash can be up to 400 characters long. However, changing just one character would be enough to invalidate the previous ExportMap.


If you compare all the approaches, you get the following:

Point JSON.stringify + hashify dequal + version stable-hash
Supports mutable objects
Stability 🤔 (not always) ✅ (using deep equality) ✅ (using stable algorithm)
Hash length fixed (64 chars) varying (up to 16 chars) varying (not limited by default)
Performance optimizations referential equality before deep compare WeakMap for previously generated hashes
Simple implementation ✅ (we can just use stableHash instead of getOptionsVersion)

So we have three options:

  • leave it "as is"
  • use stable-hash and possibly have long hash strings, and replace getOptionsVersion with it (and also remove optionsVersionsCache completely)
  • use stable-hash and then calculating real fixed-length hash (but this will be similar to the current approach)

@SukkaW SukkaW merged commit 0f07c36 into un-ts:master Jul 1, 2024
15 checks passed
@SukkaW SukkaW deleted the fix-language-option-child-context branch July 1, 2024 13:33
robtaylor pushed a commit to robtaylor/eslint-plugin-import-x that referenced this pull request Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants