(errSym, error)
| 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 |
no test coverage detected