-
Notifications
You must be signed in to change notification settings - Fork 149
var function to class #356
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
Comments
Thanks for reporting. This should be fairly easy to fix. Though it does highlight a problem with the |
I don't think there's any way to fix |
Aha... now that I look closer at this, I see that the main problem here is really the order in which the transforms are applied.
The online tool currently uses this problematic order. The command line tool will enforce you to list each transform explicitly. So... one simple fix I can make is just to change the default order in the online tool. A more proper fix would be to restrict the arrow transform so it won't kick in with this code. That's pretty tricky. Probably won't do. I'll also document this deficiency of |
Order fixed now in the online tool. |
Thanks for the quick fix
A while ago I refactored from one traverse per transform to merging all visitors and only traversing once. |
This likely isn't the right path for Lebab.
|
Manually creating a giant visitor would be hard to maintain true
it's |
Aha.. I see. This approach would indeed make more sense. Though I fail to see how it would solve any ordering issues. Well, I guess you can hard-code the order in which these visitors get merged, but similarly I can just hard-code the order in which the separate visitors are executed. |
The difference is that it applies the "bigger" transforms first on enter (can also achieve the opposite with exit) With merging you don't have to hard-code the order of different nodes types which already eliminates most issues found anoth 8000 er edge-case: var b, a = function() {}
a.prototype.foo = function() {} let b;
const a = () => {};
a.prototype.foo = () => {} Comparison of sequential vs merged (not meant to be safe, just a few example transforms): https://astexplorer.net/#/gist/7283141e13dab314521744603a95e9b7/768cf204e16b620bb89ccc7b1e169ef73938e19e |
Thanks for the explanation. I think I get the idea now. But for better or worse the approach of ESTraverse library is quite different from Babel/traverse. Most notably, ESTraverse provides two methods of traversal: As you might have noticed, this project isn't really in an active development. Occasionally I fix a bug or two, but I'd really like to avoid doing any major changes. I was really expecting this project to die many years ago and I'm kinda surprised that people still use it in 2024... but I guess there's almost infinite supply of old JS codebases that one day will decide that they'd like to switch to more modern ECMAScript. And then there are always people who use it for things it was never intended for. Regarding this example: var b, a = function() {}
a.prototype.foo = function() {} This concrete case could indeed be fixed by changing the order of transforms. Applying The main question from Lebab perspective is: would an actual person write a class like this. Probably not. Therefore it's not really a big problem when Lebab fails to convert it to class. (I can come up with many-many examples where the class transform will fail.) The goal of Lebab is to pick up common patterns in old ECMAScript and convert them to modern ones. I don't think this example represents a common pattern. (Though I could be mistaken.) Like, when I change this code slightly, then the order-fix won't be of help because the var b = 1, a = function() {}
a.prototype.foo = function() {} Of course, even if Lebab fails to convert it to class, it should do its best to not break the code (e.g. by changing constructor function into an arrow function). |
currently only
function a() {}
is detected but there's another common version:current output:
expected output:
It may be a bit unsafe because of hoisting but would avoid the error when calling new and is more readable
Another safer but not as pretty alternative:
related: #266
The text was updated successfully, but these errors were encountered: