suggestion.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. // @see: https://github.com/microsoft/TypeScript/blob/master/src/compiler/checker.ts
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.getSpellingSuggestion = void 0;
  5. /**
  6. * Given a name and a list of names that are not equal to the name, return a
  7. * spelling suggestion if there is one that is close enough. Names less than
  8. * length 3 only check for case-insensitive equality, not Levenshtein distance.
  9. *
  10. * - If there is a candidate that's the same except for case, return that.
  11. * - If there is a candidate that's within one edit of the name, return that.
  12. * - Otherwise, return the candidate with the smallest Levenshtein distance,
  13. * except for candidates:
  14. * * With no name
  15. * * Whose length differs from the target name by more than 0.34 of the
  16. * length of the name.
  17. * * Whose levenshtein distance is more than 0.4 of the length of the
  18. * name (0.4 allows 1 substitution/transposition for every 5 characters,
  19. * and 1 insertion/deletion at 3 characters)
  20. */
  21. function getSpellingSuggestion(name, candidates, getName) {
  22. const maximumLengthDifference = Math.min(2, Math.floor(name.length * 0.34));
  23. // If the best result isn't better than this, don't bother.
  24. let bestDistance = Math.floor(name.length * 0.4) + 1;
  25. let bestCandidate;
  26. let justCheckExactMatches = false;
  27. const nameLowerCase = name.toLowerCase();
  28. // eslint-disable-next-line
  29. for (const candidate of candidates) {
  30. const candidateName = getName(candidate);
  31. if (candidateName !== undefined &&
  32. Math.abs(candidateName.length - nameLowerCase.length) <=
  33. maximumLengthDifference) {
  34. const candidateNameLowerCase = candidateName.toLowerCase();
  35. if (candidateNameLowerCase === nameLowerCase) {
  36. if (candidateName === name) {
  37. continue;
  38. }
  39. return candidate;
  40. }
  41. if (justCheckExactMatches) {
  42. continue;
  43. }
  44. if (candidateName.length < 3) {
  45. // Don't bother, user would have noticed a
  46. // 2-character name having an extra character.
  47. continue;
  48. }
  49. // Only care about a result better than the best so far.
  50. const distance = levenshteinWithMax(nameLowerCase, candidateNameLowerCase, bestDistance - 1);
  51. if (distance === undefined) {
  52. continue;
  53. }
  54. if (distance < 3) {
  55. justCheckExactMatches = true;
  56. bestCandidate = candidate;
  57. }
  58. else {
  59. // Debug.assert(distance < bestDistance)
  60. bestDistance = distance;
  61. bestCandidate = candidate;
  62. }
  63. }
  64. }
  65. return bestCandidate;
  66. }
  67. exports.getSpellingSuggestion = getSpellingSuggestion;
  68. function levenshteinWithMax(s1, s2, max) {
  69. let previous = new Array(s2.length + 1); // eslint-disable-line
  70. let current = new Array(s2.length + 1); // eslint-disable-line
  71. /** Represents any value > max. We don't care about the particular value. */
  72. const big = max + 1;
  73. for (let i = 0; i <= s2.length; i += 1) {
  74. previous[i] = i;
  75. }
  76. for (let i = 1; i <= s1.length; i += 1) {
  77. const c1 = s1.charCodeAt(i - 1);
  78. const minJ = i > max ? i - max : 1;
  79. const maxJ = s2.length > max + i ? max + i : s2.length;
  80. current[0] = i;
  81. /** Smallest value of the matrix in the ith column. */
  82. let colMin = i;
  83. for (let j = 1; j < minJ; j += 1) {
  84. current[j] = big;
  85. }
  86. for (let j = minJ; j <= maxJ; j += 1) {
  87. const dist = c1 === s2.charCodeAt(j - 1)
  88. ? previous[j - 1]
  89. : Math.min(
  90. /* delete */ previous[j] + 1,
  91. /* insert */ current[j - 1] + 1,
  92. /* substitute */ previous[j - 1] + 2);
  93. current[j] = dist;
  94. colMin = Math.min(colMin, dist);
  95. }
  96. for (let j = maxJ + 1; j <= s2.length; j += 1) {
  97. current[j] = big;
  98. }
  99. if (colMin > max) {
  100. // Give up -- everything in this column is > max
  101. // and it can't get better in future columns.
  102. return undefined;
  103. }
  104. const temp = previous;
  105. previous = current;
  106. current = temp;
  107. }
  108. const res = previous[s2.length];
  109. return res > max ? undefined : res;
  110. }
  111. //# sourceMappingURL=suggestion.js.map