From 1d8f57e7b3206ea64cf5df7ce3bf8fcae2a5f9be Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 5 Jun 2017 11:17:44 -0700 Subject: [PATCH] Favour exact-match spelling suggestions Previously, the first match that was close enough was returned as the spelling suggestion. Now, if there is a candidate that differs only by case, it will always be the suggestion. --- src/compiler/checker.ts | 7 +++++- .../exactSpellingSuggestion.errors.txt | 16 ++++++++++++++ .../reference/exactSpellingSuggestion.js | 22 +++++++++++++++++++ .../cases/compiler/exactSpellingSuggestion.ts | 9 ++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/exactSpellingSuggestion.errors.txt create mode 100644 tests/baselines/reference/exactSpellingSuggestion.js create mode 100644 tests/cases/compiler/exactSpellingSuggestion.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1b3084f9cbd..1cc91550dc0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14522,6 +14522,7 @@ namespace ts { const maximumLengthDifference = Math.min(3, name.length * 0.34); let bestDistance = Number.MAX_VALUE; let bestCandidate = undefined; + let justCheckExactMatches = false; if (name.length > 30) { return undefined; } @@ -14534,6 +14535,9 @@ namespace ts { if (candidateName === name) { return candidate; } + if (justCheckExactMatches) { + continue; + } if (candidateName.length < 3 || name.length < 3 || candidateName === "eval" || @@ -14549,7 +14553,8 @@ namespace ts { continue; } if (distance < 3) { - return candidate; + justCheckExactMatches = true; + bestCandidate = candidate; } else if (distance < bestDistance) { bestDistance = distance; diff --git a/tests/baselines/reference/exactSpellingSuggestion.errors.txt b/tests/baselines/reference/exactSpellingSuggestion.errors.txt new file mode 100644 index 00000000000..dbe611590d4 --- /dev/null +++ b/tests/baselines/reference/exactSpellingSuggestion.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/exactSpellingSuggestion.ts(9,4): error TS2551: Property 'bit_2' does not exist on type 'typeof U8'. Did you mean 'BIT_2'? + + +==== tests/cases/compiler/exactSpellingSuggestion.ts (1 errors) ==== + // Fixes #16245 -- always suggest the exact match, even when + // other options are very close + enum U8 { + BIT_0 = 1 << 0, + BIT_1 = 1 << 1, + BIT_2 = 1 << 2 + } + + U8.bit_2 + ~~~~~ +!!! error TS2551: Property 'bit_2' does not exist on type 'typeof U8'. Did you mean 'BIT_2'? + \ No newline at end of file diff --git a/tests/baselines/reference/exactSpellingSuggestion.js b/tests/baselines/reference/exactSpellingSuggestion.js new file mode 100644 index 00000000000..7b71065061d --- /dev/null +++ b/tests/baselines/reference/exactSpellingSuggestion.js @@ -0,0 +1,22 @@ +//// [exactSpellingSuggestion.ts] +// Fixes #16245 -- always suggest the exact match, even when +// other options are very close +enum U8 { + BIT_0 = 1 << 0, + BIT_1 = 1 << 1, + BIT_2 = 1 << 2 +} + +U8.bit_2 + + +//// [exactSpellingSuggestion.js] +// Fixes #16245 -- always suggest the exact match, even when +// other options are very close +var U8; +(function (U8) { + U8[U8["BIT_0"] = 1] = "BIT_0"; + U8[U8["BIT_1"] = 2] = "BIT_1"; + U8[U8["BIT_2"] = 4] = "BIT_2"; +})(U8 || (U8 = {})); +U8.bit_2; diff --git a/tests/cases/compiler/exactSpellingSuggestion.ts b/tests/cases/compiler/exactSpellingSuggestion.ts new file mode 100644 index 00000000000..99aec9a0199 --- /dev/null +++ b/tests/cases/compiler/exactSpellingSuggestion.ts @@ -0,0 +1,9 @@ +// Fixes #16245 -- always suggest the exact match, even when +// other options are very close +enum U8 { + BIT_0 = 1 << 0, + BIT_1 = 1 << 1, + BIT_2 = 1 << 2 +} + +U8.bit_2