Log helpful warnings if user overwrites p5 global functions.#1318
Conversation
|
Ok @lmccart, I think this PR is ready for review! |
| // https://github.com/processing/p5.js/issues/1317 | ||
|
|
||
| if (prop in globalObject && !(prop in propsToForciblyOverwrite)) { | ||
| throw new Error('global "' + prop + '" already exists'); |
There was a problem hiding this comment.
Can you explain a little more about what's happening here? It looks like the log call on line 587 will display the console.log warning, which is the behavior we want (as noted in line 563). So in which case would an error get thrown?
There was a problem hiding this comment.
Oh, good question... I actually ran into this problem before adding the explicit throw in lines 570-571, when I used the following sketch:
var text;
function setup() {}Basically browsers threw an actual error when I was trying to call Object.defineProperty, claiming that I was trying to "redefine a non-configurable property". So I originally added the try/catch to detect for that, but I ended up later adding lines 570-571, which check for such a condition for us, so we might be able to get rid of the try/catch...
That said, because this functionality will always be called in global mode, and because it's sort of "nice to have" functionality rather than "must have" functionality, I thought it might be a good idea to leave the try/catch with the fallback to legacy behavior in there just in case there are really weird edge cases that we don't know about... I can remove it if you want though!
There was a problem hiding this comment.
Makes sense. Would you mind just adding a little line note here so we remember what's going on and this doesn't cause confusion for others?
|
This looks great, one question inline. Limitations 1 and 2 sound find for now, your notes make sense. Regarding the potential performance hit... is it possible to do something similar like we were doing with the original friendly errors, where at build time in the minified version only, the argument checker function was overwritten so it wouldn't slow things down. I am imagining something like overwriting |
|
Yep, overwriting Do you know if p5's build system uses UglifyJS under the hood? If it does, we can actually use uglify's Global Definitions functionality to conditionally define |
|
indeed we are using uglify, that's how it worked in the previous version of friendly errors as well, I believe: |
|
Oh cool! Sure, I will add the conditional compilation bit and the comment. |
|
Ok, in 365cc78 I added a commit that only includes this fancy behavior in un-minified builds. Unlike with Instead, it's done by adding an uglifyjs I recommend using this kind of feature for Note: I originally wanted the variable to be called |
|
awesome! yes, I agree this approach would be preferable for |
This is an attempt to fix #1317 and prevent future occurrences of #1314 by allowing the overwriting of p5 globals, but logging helpful warning messages informing the user of what just happened.
Example 1
This will cause the following message to be logged to the console:
Example 2
This will cause the following message to be logged to the console:
Limitations
deleteorObject.defineProperty()won't log any warnings, but it's unlikely that users--especially novices who could really use the assistance--will be using these.mouseXormouseIsPressed. We might want to tackle those in a separate PR if we want to address them, since I think the implementation will be a bit different and independent of the changes in this PR.I know how to do this kind of thing with webpack but not browserify.Update: this is actually a feature of UglifyJS, which I mention in Log helpful warnings if user overwrites p5 global functions. #1318 (comment).This PR doesn't currently have any unit tests. I'd like to add some!We have unit tests now!