Fix spelling suggestions involving Unicode characters#4232
Merged
Conversation
jakebailey
approved these changes
Jun 6, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a regression test for issue #4223 and adjusts the compiler’s spelling-suggestion heuristic so “Did you mean …” suggestions behave correctly when the misspelled identifier contains non-ASCII characters.
Changes:
- Added a new compiler regression test covering Unicode identifiers that previously produced divergent
TS1435“Did you mean …” suggestions. - Updated
GetSpellingSuggestionheuristics to base length thresholds on Unicode-aware length for the input name.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
internal/core/core.go |
Adjusts spelling-suggestion length heuristics to use a Unicode-aware length for the target name. |
testdata/tests/cases/compiler/unicodeSpellingSuggestions.ts |
Adds a regression test for Unicode spelling suggestions (issue #4223). |
testdata/baselines/reference/compiler/unicodeSpellingSuggestions.errors.txt |
Captures expected diagnostic output for the new test. |
testdata/baselines/reference/compiler/unicodeSpellingSuggestions.types |
Captures expected type baseline output for the new test. |
testdata/baselines/reference/compiler/unicodeSpellingSuggestions.symbols |
Captures expected symbol baseline output for the new test. |
Comments suppressed due to low confidence (1)
internal/core/core.go:580
- GetSpellingSuggestion now computes
maximumLengthDifference/bestDistanceusing rune count forname, but the candidate-length filter still useslen(candidateName)(byte count). For candidates containing non-ASCII characters this mixes units and can incorrectly skip valid suggestions. It also redundantly convertscandidateNameto[]runefor Levenshtein.
runeName := []rune(name)
maximumLengthDifference := max(2, int(float64(len(runeName))*0.34))
bestDistance := math.Floor(float64(len(runeName))*0.4) + 0.9 // If the best result is worse than this, don't bother.
buffers := levenshteinBuffersPool.Get().(*levenshteinBuffers)
defer levenshteinBuffersPool.Put(buffers)
var bestCandidate T
hasBest := false
for candidate := range candidates {
candidateName := getName(candidate)
maxLen := max(len(candidateName), len(runeName))
minLen := min(len(candidateName), len(runeName))
if candidateName != "" && maxLen-minLen <= maximumLengthDifference {
if candidateName == name {
continue
}
// Only consider candidates less than 3 characters long when they differ by case.
// Otherwise, don't bother, since a user would usually notice differences of a 2-character name.
if len(candidateName) < 3 && !strings.EqualFold(candidateName, name) {
continue
}
distance := levenshteinWithMax(buffers, runeName, []rune(candidateName), bestDistance)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4223.