New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
more class to es6 #6088
more class to es6 #6088
Conversation
I've dug into it, and found the problems are all in |
You're probably right @wuyudi -- sorry I haven't had a chance to take a look just yet, but hopefully in the next day or so I'll get the time! then I can see if I can help updating either the test or the function the test is using. |
Hey @wuyudi, I think the issue comes from creating classes like this: p5.Color = class{ /* ... */ } When parsing function arguments to deliver friendlier error messages, we check p5.Color = class Color { /* ... */ } |
now there are only one 10 pending
1 failing
1782 passing (30s)
1) Global Error Handling
identifies errors happenning internally:
AssertionError: expected 2 to equal 1
+ expected - actual
-2
+1
at assert.strictEqual (http://localhost:9001/node_modules/chai/chai.js:2329:32)
at http://localhost:9001/test/unit/core/error_helpers.js:564:14 |
Does it still happen if you add a name for all the classes you've converted? |
I add names for four classes lol, so "this" won't become a problem anymore. |
it seems that the log is unrelated to the named class. |
For this one, it looks like maybe it's from methods on the class still being defined by setting a function on the class prototype rather than using an ES6 class method. The check to see if an error is internal happens here: p5.js/src/core/friendly_errors/fes_core.js Line 507 in ab3e2ad
The test in question runs this code: function setup() {
let cnv = createCanvas(400, 400);
cnv.mouseClicked(); // this is an error, a callback should be passed here
} When the error is caught and it looks at the stack on v1.6.0, it looks like this: After these changes, it looks like this: It seems like this is partially a result of how Babel transforms classes, but I'm not 100% sure. @limzykenneth do you have any extra insights into what part might be adding the extra info to the name? I was looking at the options here https://babeljs.io/docs/babel-plugin-transform-classes but couldn't get anything to fix it. Another option might be to find another way, other than the stack trace function name, to check if the error happened in an internal function or not. Maybe checking if the most recent item in the stack trace comes from a file called |
So I did some digging, not gone all the way but might have something. While babel does indeed produce different code with the class syntax as opposed to the prototype syntax (class syntax essentially got transpiled into a sort of generator function while prototype mainly stay what they are), the main problem is that the messaged logged when an internal function setup() {
let cnv = createCanvas(400, 400);
cnv.mouseClicked(); // this is an error, a callback should be passed here
} I don't know why this is not showing up in the test on the main branch when it shows up on built version of the library but basically, in 1.6.0 and in the test of the main branch, the above test code logs in the FES this message: [
'\n' +
`🌸 p5.js says: [test.html, line 4] An error with message "Cannot read properties of undefined (reading 'bind')" occurred inside the p5js library when mouseClicked was called. If not stated otherwise, it might be an issue with the arguments passed to mouseClicked. (http://p5js.org/reference/#/p5/mouseClicked)`
] which is only one message, whereas this PR and the built version of the main branch logs the following: [
'\n' +
'🌸 p5.js says: \n' +
'[p5.js, line 62480] 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',
'┌[http://localhost:9001/lib/p5.js:62480:29] \n' +
'\t Error at line 62480 in Function._attachListener()\n' +
'└[http://localhost:9001/lib/p5.js:62468:41] \n' +
'\t Called from line 62468 in Function._adjustListener()\n' +
'└[http://localhost:9001/lib/p5.js:62084:39] \n' +
'\t Called from line 62084 in Renderer2D.mouseClicked()\n' +
'└[http://localhost:9001/test/test.html:4:5] \n' +
'\t Called from line 4 in setup()\n'
] which are two messages. The actual intended message in this case should probably be just the one message (ie. the former). This behaviour started in 012340e when p5.Element got converted to the class syntax and it made sense to show up here when changing the p5.Renderer syntax since it inherits p5.Element. I haven't been able to tell much more at this point as I don't fully understand FES, I'll defer to @almchung for that possibly. In any case we probably should double check p5.Element's implementation and also how it transpile through Babel. A side note, I looked into letting Babel preserve the class syntax after transpilation but there are many areas in the library where the constructors are not called with the |
@limzykenneth do you have a way to get a list of those? If so, I can help convert them in the p5.sound repo (also I think the p5.sound build is fairly old and due for a rebuild, we can make a new build and see if it's still like that) |
@davepagurek They way I got those p5.Sound errors is by changing the However, this will likely not be a viable way to solve this issue just yet because of the browsers we are supporting ("last 2 versions", "not dead"), the most problematic in that list is Opera Mini (and maybe some other even more obscure mobile browsers). |
Ah ok makes sense! So FES switches between those two messages based on whether it detects that the error happens within the library. Right now it does that on this line by checking the name of the function calls in the stacktrace to see if they include p5.js/src/core/friendly_errors/fes_core.js Line 507 in ab3e2ad
I was looking into potential changes to the Babel settings because it seems like the function name is affected by how Babel does the compilation. Potentially if we find a way for Babel to compile the classes into the exact same form as before then we can leave FES as is, but I'm not sure how feasible that is. Another option might be to make FES to look at the source file name instead of the function name to see if it originated in a file called
That said, it currently will also treat errors originated from |
Since that problem seems to lack hands, I decided to delay it lol. At least all other code is migrated. |
@davepagurek I decided to postpone rewriting those Render classes, only to convert others first. |
Sounds good, sorry for the delays @wuyudi. Without converting the Renderer classes the tests pass, although I think it means that our detection of internal errors for any of these converted classes will be slightly off, and it was just that the test checked that one specific class. @limzykenneth how do you feel about merging this in so that we don't keep this PR hanging indefinitely and we make a new issue for getting FES to handle ES6 classes before we convert any more? Is it worth the slight regression of having a less clear error message for the already converted classes in the mean time? |
I think it's fine to merge for now. I don't quite remember the timeline for the next release but we probably will have some time to sort this out if possible. |
Resolves #3758
Changes:
More class to es6, this is a continue to #6075
Screenshots of the change:
PR Checklist
npm run lint
passesNow the test throws 3 errors, but the
npm run build
result seems to work properly.