(e, log)
| 1078 | * @returns {Boolean} true |
| 1079 | */ |
| 1080 | const helpForMisusedAtTopLevelCode = (e, log) => { |
| 1081 | if (!log) { |
| 1082 | log = console.log.bind(console); |
| 1083 | } |
| 1084 | |
| 1085 | if (!misusedAtTopLevelCode) { |
| 1086 | defineMisusedAtTopLevelCode(); |
| 1087 | } |
| 1088 | |
| 1089 | // If we find that we're logging lots of false positives, we can |
| 1090 | // uncomment the following code to avoid displaying anything if the |
| 1091 | // user's code isn't likely to be using p5's global mode. (Note that |
| 1092 | // setup/draw are more likely to be defined due to JS function hoisting.) |
| 1093 | // |
| 1094 | //if (!('setup' in window || 'draw' in window)) { |
| 1095 | // return; |
| 1096 | //} |
| 1097 | |
| 1098 | misusedAtTopLevelCode.some(symbol => { |
| 1099 | // Note that while just checking for the occurrence of the |
| 1100 | // symbol name in the error message could result in false positives, |
| 1101 | // a more rigorous test is difficult because different browsers |
| 1102 | // log different messages, and the format of those messages may |
| 1103 | // change over time. |
| 1104 | // |
| 1105 | // For example, if the user uses 'PI' in their code, it may result |
| 1106 | // in any one of the following messages: |
| 1107 | // |
| 1108 | // * 'PI' is undefined (Microsoft Edge) |
| 1109 | // * ReferenceError: PI is undefined (Firefox) |
| 1110 | // * Uncaught ReferenceError: PI is not defined (Chrome) |
| 1111 | |
| 1112 | if (e.message && e.message.match(`\\W?${symbol.name}\\W`) !== null) { |
| 1113 | const symbolName = |
| 1114 | symbol.type === 'function' ? `${symbol.name}()` : symbol.name; |
| 1115 | if (typeof IS_MINIFIED !== 'undefined') { |
| 1116 | log( |
| 1117 | `Did you just try to use p5.js's ${symbolName} ${ |
| 1118 | symbol.type |
| 1119 | }? If so, you may want to move it into your sketch's setup() function.\n\nFor more details, see: ${FAQ_URL}` |
| 1120 | ); |
| 1121 | } else { |
| 1122 | log( |
| 1123 | translator('fes.misusedTopLevel', { |
| 1124 | symbolName, |
| 1125 | symbolType: symbol.type, |
| 1126 | url: FAQ_URL |
| 1127 | }) |
| 1128 | ); |
| 1129 | } |
| 1130 | return true; |
| 1131 | } |
| 1132 | }); |
| 1133 | }; |
| 1134 | |
| 1135 | // Exposing this primarily for unit testing. |
| 1136 | fn._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode; |
nothing calls this directly
no test coverage detected