Skip to content

Use Levenshtein distance to display similar artifact names if given name is not found#2052

Merged
zoeyTM merged 14 commits intomasterfrom
artifact-distance
Nov 19, 2021
Merged

Use Levenshtein distance to display similar artifact names if given name is not found#2052
zoeyTM merged 14 commits intomasterfrom
artifact-distance

Conversation

@zoeyTM
Copy link
Copy Markdown
Contributor

@zoeyTM zoeyTM commented Nov 9, 2021

Core feature goal: Implement an appropriate edit-distance algorithm (Levenshtein) to suggest similar artifacts if a requested artifact is not found (i.e. if a user made a typo in the name)

New functions:

Public:

  • formArtifactPathFromFullyQualifiedName

Private:

  • _trueCasePath
  • _trueCasePathSync
  • _getAllContractNamesFromFiles
  • _getAllFullyQualifiedNamesSync
  • _handleWrongArtifactForFullyQualifiedName
  • _handleWrongArtifactForContractName
  • _handleMultipleSimilarContractNames
  • _getSimilarContractNames
  • findDistance (Levenshtein logic)

Added Error 703: ERRORS.ARTIFACTS.TYPO_SUGGESTION
Example:

HardhatError: HH703: Artifact for contract "Lob" not found, but we found some that were similar:

Lab
Lib.sol:Lib
Lib2.sol:Lib

Please replace Lob for the correct contract name wherever you are trying to read its artifact.

Some existing functions were edited such as moving the true-case-path logic from readArtifact and readArtifactSync into _getArtifactPathFromFullyQualifiedName (now called _getValidArtifactPathFromFullyQualifiedName due to needing to keep the old function for TypeChain).

The reasoning behind moving this logic is that it was only needed when the user passed in a fully qualified name, so its placement in the top-level public function was misleading. Moving it fully into the dedicated "fully qualified name" code path is clearer and doesn't make additional, unnecessary file system calls when a contract name is given instead of an FQN.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Nov 9, 2021

🦋 Changeset detected

Latest commit: b62b5e3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hardhat Patch

Not sure what this means? Click here to learn what changesets are.

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

@zoeyTM zoeyTM changed the title core code for async path finished Use Levenshtein distance to display similar artifact names if given name is not found Nov 9, 2021
@zoeyTM zoeyTM marked this pull request as ready for review November 11, 2021 22:37
@zoeyTM zoeyTM requested a review from fvictorio November 11, 2021 22:38
@zoeyTM zoeyTM requested a review from fvictorio November 16, 2021 23:33
@zoeyTM zoeyTM merged commit 73d7607 into master Nov 19, 2021
@zoeyTM zoeyTM deleted the artifact-distance branch November 19, 2021 15:36
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants