MCPcopy
hub / github.com/processing/p5.js / handleMisspelling

Function handleMisspelling

src/core/friendly_errors/fes_core.js:378–465  ·  view source on GitHub ↗
(errSym, error)

Source from the content-addressed store, hash-verified

376 * @returns {Boolean} tell whether error was likely due to typo
377 */
378 const handleMisspelling = (errSym, error) => {
379 if (!misusedAtTopLevelCode) {
380 defineMisusedAtTopLevelCode();
381 }
382
383 const distanceMap = {};
384 let min = 999999;
385 // compute the levenshtein distance for the symbol against all known
386 // public p5 properties. Find the property with the minimum distance
387 misusedAtTopLevelCode.forEach(symbol => {
388 let dist = computeEditDistance(errSym, symbol.name);
389 if (distanceMap[dist]) distanceMap[dist].push(symbol);
390 else distanceMap[dist] = [symbol];
391
392 if (dist < min) min = dist;
393 });
394
395 // if the closest match has more "distance" than the max allowed threshold
396 if (min > Math.min(EDIT_DIST_THRESHOLD, errSym.length)) return false;
397
398 // Show a message only if the caught symbol and the matched property name
399 // differ in their name ( either letter difference or difference of case )
400 const matchedSymbols = distanceMap[min].filter(
401 symbol => symbol.name !== errSym
402 );
403 if (matchedSymbols.length !== 0) {
404 const parsed = p5._getErrorStackParser().parse(error);
405 let locationObj;
406 if (
407 parsed &&
408 parsed[0] &&
409 parsed[0].fileName &&
410 parsed[0].lineNumber &&
411 parsed[0].columnNumber
412 ) {
413 locationObj = {
414 location: `${parsed[0].fileName}:${parsed[0].lineNumber}:${
415 parsed[0].columnNumber
416 }`,
417 file: parsed[0].fileName.split('/').slice(-1),
418 line: parsed[0].lineNumber
419 };
420 }
421
422 let msg;
423 if (matchedSymbols.length === 1) {
424 // To be used when there is only one closest match. The count parameter
425 // allows i18n to pick between the keys "fes.misspelling" and
426 // "fes.misspelling_plural"
427 msg = translator('fes.misspelling', {
428 name: errSym,
429 actualName: matchedSymbols[0].name,
430 type: matchedSymbols[0].type,
431 location: locationObj ? translator('fes.location', locationObj) : '',
432 count: matchedSymbols.length
433 });
434 } else {
435 // To be used when there are multiple closest matches. Gives each

Callers 1

fesErrorMonitorFunction · 0.85

Calls 8

translatorFunction · 0.90
computeEditDistanceFunction · 0.85
mapToReferenceFunction · 0.85
sliceMethod · 0.80
splitMethod · 0.80
pushMethod · 0.45
filterMethod · 0.45
_friendlyErrorMethod · 0.45

Tested by

no test coverage detected