-
Notifications
You must be signed in to change notification settings - Fork 26
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
Compiler chokes on regular expression literal #87
Comments
Hi @fschopp - The underlying issue is that regular expressions are hard to parse in JS -- they're not context free, meaning the parser needs to distinguish whether a '/' is occurring in a context where a division operator is possible or where a regular expression is possible. The surplus parser isn't sophisticated enough to handle that. Originally, this was a conscious tradeoff -- the early versions of surplus often did runtime rather than build-time compilation, and this tradeoff allowed the parser to be smaller and run faster. Now that surplus is almost exclusively build-time (except for surplus-toys), that's not so much of a concern. This could be fixed, as I mentioned in #45, by transitioning to a smarter parser, like acorn.js. The hold-up on that is that, well, outside of this issue, the current parser is working fairly well, and the code changes would be large. I'm sort of waiting for some other issue to come along and tip the pro/con balance in favor of that work, but until then, I haven't undertaken the rewrite. Sorry to not have an immediate answer here. More reports like this definitely tip the pro/con list. I'll keep you posted. |
Ah, I think I just got the significance of what you had to say about Parcel and .js assets. So in the parcel pipleine, both .ts and .tsx get converted to .js. So if parcel-plugin-surplus didn't parse .js assets, you couldn't use surplus on .tsx files with it, as they first get converted to .js. If that's right, I see the issue. Is there no way in a parcel plugin to tell what the original asset type was? I wonder if the application to .js files could occur only if the original file was .tsx. I don't use parcel, unfortunately, and know even less about its plugin api. |
Hi Adam – thanks for the thorough explanation. For some reason I did not notice #45 – indeed, my report is a duplicate. I'll close this issue. I agree that a smarter parser would be nice to have, but the failure with Parcel is probably better addressed in parcel-plugin-surplus and/or in parcel itself. One option would be that parcel-plugin-surplus only claims responsibility for .jsx and .tsx files. For .tsx it would then also have to invoke the TypeScript compiler. Another option would be to make parcel distinguish between .js and .jsx assets, and also between .ts and .tsx. Then parcel-plugin-surplus only needs to claim responsibility for .jsx assets. I didn't have the time for such a pull request, so for the time being my project is using a little script that invokes the surplus compiler to transform .jsx into .js and also uses package source-map to coalesce the tsc and surplus-generated source maps (i.e., a .js to .tsx source map). Parcel then only ever sees .js assets in the HTML. The downside of this workaround is, of course, that parcel no longer rebuilds automatically if I modify the source files. |
Given file
compiler-bug.js
with contentthe following fails:
The problem is that the Surplus compiler does not skip regular expression literals.
Just for background: The line is taken from the jQuery source code. The reason I ran into the issue in the first place is that parcel-plugin-surplus runs the surplus compiler also on .js assets. A reason for that could be that Parcel does not distinguish between .tsx and .ts files, so they both get transformed into a .js asset.
The text was updated successfully, but these errors were encountered: