UNPKG

p5

Version:

[![npm version](https://badge.fury.io/js/p5.svg)](https://www.npmjs.com/package/p5)

380 lines (379 loc) • 17.4 kB
import { t as FES } from "../fes-Dwprq5RO.js"; import { entryPoints, errorTable } from "./browser_errors.js"; import { errorStackParser, getFriendlyStack, printFriendlyStack, processStack } from "./stacktrace.js"; //#region src/friendly_errors/fes_core.js /** * @for p5 * @requires core * * This is the main file for the Friendly Error System (FES), containing * the core as well as miscellaneous functionality of the FES. Here is a * brief outline of the functions called in this system. * * The FES may be invoked by a call to either * (1) _validateParameters, (2) _friendlyFileLoadError, (3) _friendlyError, * (4) helpForMisusedAtTopLevelCode, or (5) _fesErrorMonitor. * * _validateParameters is located in validate_params.js along with other code * used for parameter validation. * _friendlyFileLoadError is located in file_errors.js along with other code * used for dealing with file load errors. * Apart from this, there's also a file stacktrace.js, which contains the code * to parse the error stack, borrowed from: * https://github.com/stacktracejs/stacktrace.js * * For more detailed information on the FES functions, including the call * sequence of each function, please look at the FES Reference + Dev Notes: * https://github.com/processing/p5.js/blob/main/contributor_docs/fes_reference_dev_notes.md */ function fesCore(p5, fn, lifecycles) { let misusedAtTopLevelCode = null; /** * A helper function for populating misusedAtTopLevel list. * * @method defineMisusedAtTopLevelCode * @private */ let defineMisusedAtTopLevelCode = () => { const uniqueNamesFound = {}; const getSymbols = (obj) => Object.getOwnPropertyNames(obj).filter((name) => { if (name[0] === "_") return false; if (name in uniqueNamesFound) return false; uniqueNamesFound[name] = true; return true; }).map((name) => { let type; if (typeof obj[name] === "function") type = "function"; else if (name === name.toUpperCase()) type = "constant"; else type = "variable"; return { name, type }; }); misusedAtTopLevelCode = getSymbols(fn); misusedAtTopLevelCode.sort((a, b) => b.name.length - a.name.length); }; const EDIT_DIST_THRESHOLD = 2; /** * Takes a message and a p5 function func, and adds a link pointing to * the reference documentation of func at the end of the message * * @method mapToReference * @private * @param {String} message the words to be said * @param {String} [func] the name of function * * @returns {String} */ const mapToReference = (message, func) => { let msgWithReference = ""; if (func == null || func.substring(0, 4) === "load") msgWithReference = message; else { const methodParts = func.split("."); const referenceSection = methodParts.length > 1 ? `${methodParts[0]}.${methodParts[1]}` : "p5"; const funcName = methodParts.length === 1 ? func : methodParts.slice(2).join("/"); if (funcName.startsWith("p5.")) msgWithReference = FES.log`${message} (https://p5js.org/reference/${referenceSection}.${funcName})`; else msgWithReference = FES.log`${message} (https://p5js.org/reference/${referenceSection}/${funcName})`; } return msgWithReference; }; /** * Prints out a fancy, colorful message to the console log * Attaches Friendly Errors prefix [fes.pre] to the message. * * @method _report * @private * @param {String} message Message to be printed * @param {String} [func] Name of function * * @return console logs */ p5._report = (message, func) => { message = mapToReference(message, func); FES.log`${message}`(); }; /** * This is a generic method that can be called from anywhere in the p5 * library to alert users to a common error. * * @method _friendlyError * @private * @param {String} message Message to be printed * @param {String} [func] Name of the function linked to error */ p5._friendlyError = function(message, func) { if (p5.disableFriendlyErrors) return; p5._report(message, func); }; /** * Whether or not p5.js is running in an environment where `preload` will be * run before `setup`. * * This will return false for default builds >= 2.0, but backwards compatibility * addons may set this to true. * * @private */ p5.isPreloadSupported = function() { return false; }; /** * Checks capitalization for user defined functions. * * Generates and prints a friendly error message using key: * "fes.checkUserDefinedFns". * * @method checkForUserDefinedFunctions * @private * @param {*} context Current default context. Set to window in * "global mode" and to a p5 instance in "instance mode" */ const checkForUserDefinedFunctions = (context) => { if (p5.disableFriendlyErrors) return; context = context instanceof p5 ? context : window; const fnNames = entryPoints; if (context.preload && !p5.isPreloadSupported()) FES.log`The preload() function has been removed in p5.js 2.0. Please load assets in setup() using async / await keywords or callbacks instead. See https://github.com/processing/p5.js-compatibility for more information about 2.0 and compatibility, or https://dev.to/limzykenneth/asynchronous-p5js-20-458f for more information about promises and async/await.`(); const fxns = {}; fnNames.forEach((symbol) => { fxns[symbol.toLowerCase()] = symbol; }); for (const prop of Object.keys(context)) { const lowercase = prop.toLowerCase(); if (fxns.hasOwnProperty(lowercase) && !context[fxns[lowercase]] && typeof context[prop] === "function") FES.log`It seems that you may have accidentally written ${prop} instead of ${fxns[lowercase]}. Please correct it if it's not intentional.`({ reference: fxns[lowercase] }); } }; /** * Compares the symbol caught in the ReferenceError to everything in * misusedAtTopLevel ( all public p5 properties ). * * Generates and prints a friendly error message using key: "fes.misspelling". * * @method handleMisspelling * @private * @param {String} errSym Symbol to whose spelling to check * @param {Error} error ReferenceError object * * @returns {Boolean} tell whether error was likely due to typo */ const handleMisspelling = (errSym, error) => { const distanceMap = {}; let min = 999999; misusedAtTopLevelCode.forEach((symbol) => { let dist = computeEditDistance(errSym, symbol.name); if (distanceMap[dist]) distanceMap[dist].push(symbol); else distanceMap[dist] = [symbol]; if (dist < min) min = dist; }); if (min > Math.min(EDIT_DIST_THRESHOLD, errSym.length)) return false; const matchedSymbols = distanceMap[min].filter((symbol) => symbol.name !== errSym); if (matchedSymbols.length !== 0) { const parsed = errorStackParser.parse(error); let locationObj; if (parsed && parsed[0] && parsed[0].fileName && parsed[0].lineNumber && parsed[0].columnNumber) locationObj = { location: `${parsed[0].fileName}:${parsed[0].lineNumber}:${parsed[0].columnNumber}`, file: parsed[0].fileName.split("/").slice(-1), line: parsed[0].lineNumber }; let msg; if (matchedSymbols.length === 1) msg = FES.log`${locationObj ? FES.log`[${locationObj.file}, line ${locationObj.line}]` : ""} It seems that you may have accidentally written "${errSym}" instead of "${matchedSymbols[0].name}". Please correct it to ${matchedSymbols[0].name} if you wish to use the ${matchedSymbols[0].type} from p5.js.`; else { const suggestions = matchedSymbols.map((symbol) => { const message = "▶️ " + symbol.name + (symbol.type === "function" ? "()" : ""); return mapToReference(message, symbol.name); }).join("\n"); msg = FES.log`${locationObj ? FES.log`[${locationObj.file}, line ${locationObj.line}]` : ""} It seems that you may have accidentally written "${errSym}".\nYou may have meant one of the following: \n${suggestions}`; } msg({ reference: matchedSymbols.length === 1 ? matchedSymbols[0].name : void 0 }); return true; } return false; }; /** * Detects browser level error event for p5 constants/functions used outside * of setup() and draw(). * * Generates and prints a friendly error message using key: * "fes.misusedTopLevel". * * @method helpForMisusedAtTopLevelCode * @private * @param {Event} e Error event * @param {Boolean} log false * * @returns {Boolean} true */ const helpForMisusedAtTopLevelCode = (e) => { return misusedAtTopLevelCode.some((symbol) => { if (e.message && e.message.match(`\\W?${symbol.name}\\W`) !== null) { const FAQ_URL = "https://github.com/processing/p5.js/wiki/p5.js-overview#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup"; const symbolName = symbol.type === "function" ? `${symbol.name}()` : symbol.name; FES.log`Did you just try to use p5.js's ${symbolName} ${symbol.type}? If so, you may want to move it into your sketch's setup() function.\n\nFor more details, see: ${FAQ_URL}`(); return true; } }); }; /** * Handles "global" errors that the browser catches. * * Called when an error event happens and detects the type of error. * * Generates and prints a friendly error message using key: * "fes.globalErrors.syntax.[*]", "fes.globalErrors.reference.[*]", * "fes.globalErrors.type.[*]". * * @method fesErrorMonitor * @private * @param {*} e Event object to extract error details from */ const fesErrorMonitor = (e) => { if (p5.disableFriendlyErrors) return; if (!misusedAtTopLevelCode) defineMisusedAtTopLevelCode(); let error; if (e instanceof Error) error = e; else if (e instanceof ErrorEvent) error = e.error; else if (e instanceof PromiseRejectionEvent) { error = e.reason; if (!(error instanceof Error)) return; } if (!error) return; let stacktrace = errorStackParser.parse(error); let [isInternal, friendlyStack] = processStack(error, stacktrace); if (isInternal) return; const errList = errorTable[error.name]; if (!errList) return; let matchedError; for (const obj of errList) { let string = obj.msg; string = string.replace(/* @__PURE__ */ new RegExp("{{}}", "g"), "([a-zA-Z0-9_]+)"); string = string.replace(/* @__PURE__ */ new RegExp("{{.}}", "g"), "(.+)"); string = string.replace(/* @__PURE__ */ new RegExp("{}", "g"), "(?:[a-zA-Z0-9_]+)"); let matched = error.message.match(string); if (matched) { matchedError = Object.assign({}, obj); matchedError.match = matched; break; } } if (!matchedError) return; let locationStr = ""; if (stacktrace && stacktrace[0].fileName && stacktrace[0].lineNumber && stacktrace[0].columnNumber) locationStr = getFriendlyStack(stacktrace, true); switch (error.name) { case "SyntaxError": switch (matchedError.type) { case "INVALIDTOKEN": FES.log`Syntax Error - Found a symbol that JavaScript doesn't recognize or didn't expect at its place.\n\n+ More info: ${"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Errors/Illegal_character#What_went_wrong"}`(); break; case "UNEXPECTEDTOKEN": FES.log`Syntax Error - Symbol present at a place that wasn't expected.\nUsually this is due to a typo. Check the line number in the error for anything missing/extra.\n\n+ More info: ${"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Errors/Unexpected_token#What_went_wrong"}`(); break; case "REDECLAREDVARIABLE": { let errSym = matchedError.match[1]; FES.log`Syntax Error - "${errSym}" is being redeclared. JavaScript doesn't allow declaring a variable more than once. Check the line number in error for redeclaration of the variable.\n\n+ More info: ${"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Redeclared_parameter#what_went_wrong"}`(); break; } case "MISSINGINITIALIZER": FES.log`Syntax Error - A const variable is declared but not initialized. In JavaScript, an initializer for a const is required. A value must be specified in the same statement in which the variable is declared. Check the line number in the error and assign the const variable a value.\n\n+ More info: ${"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_initializer_in_const#what_went_wrong"}`(); break; case "BADRETURNORYIELD": FES.log`Syntax Error - return lies outside of a function. Make sure you’re not missing any brackets, so that return lies inside a function.\n\n+ More info: ${"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_return_or_yield#what_went_wrong"}`(); } break; case "ReferenceError": switch (matchedError.type) { case "NOTDEFINED": { let errSym = matchedError.match[1]; if (errSym && handleMisspelling(errSym, error) || helpForMisusedAtTopLevelCode(e)) break; FES.log`${locationStr} "${errSym}" is not defined in the current scope. If you have defined it in your code, you should check its scope, spelling, and letter-casing (JavaScript is case-sensitive).\n\n+ More info: ${"https://p5js.org/examples/data-variable-scope.html"}`(); if (friendlyStack) { const msg = getFriendlyStack(friendlyStack); if (msg) FES.log`${msg}`({ prefix: false }); } break; } case "CANNOTACCESS": { let errSym = matchedError.match[1]; FES.log`${locationStr} "${errSym} is used before declaration. Make sure you have declared the variable before using it.\n\n+ More info: ${"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init#what_went_wrong"}`(); if (friendlyStack) printFriendlyStack(friendlyStack); break; } } break; case "TypeError": switch (matchedError.type) { case "NOTFUNC": { let splitSym = matchedError.match[1].split("."); let url = "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Errors/Not_a_function#What_went_wrong"; let translationObj = { symbol: splitSym[splitSym.length - 1], obj: splitSym.slice(0, splitSym.length - 1).join(".") }; if (splitSym.length > 1) FES.log`${locationStr} "${translationObj.symbol}" could not be called as a function.\nVerify whether "${translationObj.obj}" has "${translationObj.symbol}" in it and check the spelling, letter-casing (JavaScript is case-sensitive) and its type.\n\n+ More info: ${url}`(); else FES.log`${locationStr} "${translationObj.symbol}" could not be called as a function.\nCheck the spelling, letter-casing (JavaScript is case-sensitive) and its type.\n\n+ More info: ${url}`(); if (friendlyStack) printFriendlyStack(friendlyStack); break; } case "READNULL": FES.log`${locationStr} The property of null can't be read. In javascript the value null indicates that an object has no value.\n\n+ More info: ${"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_property#what_went_wrong"}`(); if (friendlyStack) printFriendlyStack(friendlyStack); break; case "READUDEFINED": FES.log`${locationStr} Cannot read property of undefined. Check the line number in error and make sure the variable which is being operated is not undefined.\n\n + More info: ${"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_property#what_went_wrong"}`(); if (friendlyStack) printFriendlyStack(friendlyStack); break; case "CONSTASSIGN": FES.log`${locationStr} A const variable is being re-assigned. In javascript, re-assigning a value to a constant is not allowed. If you want to re-assign new values to a variable, make sure it is declared as var or let.\n\n+ More info: ${"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_const_assignment#what_went_wrong"}`(); if (friendlyStack) printFriendlyStack(friendlyStack); } } }; p5._checkForUserDefinedFunctions = checkForUserDefinedFunctions; p5._fesLogCache = {}; lifecycles.presetup = function() { checkForUserDefinedFunctions(); window.addEventListener("error", fesErrorMonitor, false); window.addEventListener("unhandledrejection", fesErrorMonitor, false); }; } if (typeof p5 !== "undefined") p5.registerAddon(fesCore); /** * Measures dissimilarity between two strings by calculating * the Levenshtein distance. * * If the "distance" between them is small enough, it is * reasonable to think that one is the misspelled version of the other. * * Specifically, this uses the Wagner–Fischer algorithm. * * @method computeEditDistance * @private * @param {String} w1 the first word * @param {String} w2 the second word * * @returns {Number} the "distance" between the two words, a smaller value * indicates that the words are similar */ function computeEditDistance(w1, w2) { const l1 = w1.length, l2 = w2.length; if (l1 === 0) return w2; if (l2 === 0) return w1; let prev = []; let cur = []; for (let j = 0; j < l2 + 1; j++) cur[j] = j; prev = cur; for (let i = 1; i < l1 + 1; i++) { cur = []; for (let j = 0; j < l2 + 1; j++) if (j === 0) cur[j] = i; else { let a1 = w1[i - 1], a2 = w2[j - 1]; let temp = 999999; let cost = a1.toLowerCase() === a2.toLowerCase() ? 0 : 1; temp = temp > cost + prev[j - 1] ? cost + prev[j - 1] : temp; temp = temp > 1 + cur[j - 1] ? 1 + cur[j - 1] : temp; temp = temp > 1 + prev[j] ? 1 + prev[j] : temp; cur[j] = temp; } prev = cur; } return cur[l2]; } //#endregion export { fesCore as default };