acornjs / acorn-jsx

Alternative, faster React.js JSX parser
MIT License
648 stars 72 forks source link

Add transformEntities option #120

Open Qix- opened 3 years ago

Qix- commented 3 years ago

Ref #105

A bit of context is in https://github.com/acornjs/acorn-jsx/issues/105#issuecomment-742754881 but ultimately this parser should not be transforming HTML entities for the user - that's up to the compiler or the user agent.

While I personally believe this functionality should be stripped from this package entirely and downstream consumers should be the ones to make that decision, I figured that'd be less palatable than keeping things backwards compatible.

Therefore, this PR introduces the option transformEntities which is enabled by default that will handle HTML entities and transform them automatically - the current functionality. Passing false disables this and leaves HTML entities untouched.

This is imperative for proper parsing for many non-React frameworks.


A note about the implementation: to keep things performant, I (ab)use an explicit case fallthrough as a makeshift goto, avoiding duplicated code or more string fetches, etc. The branch is inevitable, but thankfully only occurs if & is detected (which is much less often than if it were to be put into default to avoid the fallthrough, for example). This was the best balance between code readability and performance in my opinion.

Qix- commented 3 years ago

By the way, Travis CI removed support for open source projects as of Nov 2. Best to move to Circle or Actions or something if you want to keep automated testing working.