-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sm/levenshtein-missing-types (#1374)
* feat: did you mean when types aren't in Registry * refactor: nicer output formatting * test: skip a test * refactor: remove unused handler * refactor: complexity, simplified naming --------- Co-authored-by: Willie Ruemmele <[email protected]>
- Loading branch information
1 parent
7870179
commit a66f50d
Showing
9 changed files
with
452 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { Messages } from '@salesforce/core/messages'; | ||
import * as Levenshtein from 'fast-levenshtein'; | ||
import { MetadataRegistry } from './types'; | ||
|
||
Messages.importMessagesDirectory(__dirname); | ||
const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr'); | ||
|
||
/** "did you mean" for Metadata type names */ | ||
export const getTypeSuggestions = (registry: MetadataRegistry, typeName: string): string[] => { | ||
const scores = getScores( | ||
Object.values(registry.types).map((t) => t.name), | ||
typeName | ||
); | ||
|
||
const guesses = getLowestScores(scores).map((guess) => guess.registryKey); | ||
return [ | ||
...(guesses.length | ||
? [ | ||
`Did you mean one of the following types? [${guesses.join(',')}]`, | ||
'', // Add a blank line for better readability | ||
] | ||
: []), | ||
messages.getMessage('type_name_suggestions'), | ||
]; | ||
}; | ||
|
||
export const getSuffixGuesses = (suffixes: string[], input: string): string[] => { | ||
const scores = getScores(suffixes, input); | ||
return getLowestScores(scores).map((g) => g.registryKey); | ||
}; | ||
|
||
type LevenshteinScore = { | ||
registryKey: string; | ||
score: number; | ||
}; | ||
|
||
const getScores = (choices: string[], input: string): LevenshteinScore[] => | ||
choices.map((registryKey) => ({ | ||
registryKey, | ||
score: Levenshtein.get(input, registryKey, { useCollator: true }), | ||
})); | ||
|
||
/** Levenshtein uses positive integers for scores, find all scores that match the lowest score */ | ||
const getLowestScores = (scores: LevenshteinScore[]): LevenshteinScore[] => { | ||
const sortedScores = scores.sort(levenshteinSorter); | ||
const lowestScore = scores[0].score; | ||
return sortedScores.filter((score) => score.score === lowestScore); | ||
}; | ||
|
||
const levenshteinSorter = (a: LevenshteinScore, b: LevenshteinScore): number => a.score - b.score; |
Oops, something went wrong.