-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
JSX conditionals show error #292
Comments
@jacksonrayhamilton Also ping. |
To fix this we need parsing support for JSX. @benzitohhh, my suggestion in the meantime is to disable In your init file: (require 'flycheck)
(require 'js2-mode)
(add-to-list 'auto-mode-alist '("\\.jsx?\\'" . js2-jsx-mode))
(setq js2-mode-show-parse-errors nil)
(setq js2-mode-show-strict-warnings nil)
;; Disable JSCS linting (optional but if you're using ESLint you probably don't
;; need this).
(let ((checkers (get 'javascript-eslint 'flycheck-next-checkers)))
(put 'javascript-eslint 'flycheck-next-checkers
(remove '(warning . javascript-jscs) checkers)))
(defun setup-js2-mode ()
(flycheck-select-checker 'javascript-eslint)
(flycheck-mode))
(add-hook 'js2-mode-hook #'setup-js2-mode) In a file called {
"ecmaFeatures": {
"jsx": true
}
} @dgutov, how would you like to handle this? Disable linting inside JSX blocks until parsing support is added? |
Can we even do that reliably? Disable showing errors inside E4X literals, but still show them outside? |
Perhaps I shouldn't be absolute about "needing JSX parsing support." There might be another way around this that involves improving or adapting the E4X code.
I'll have to take another look at the code, but I was thinking of binding some variable for the duration of parsing E4X that would prevent errors from being recorded. |
Might be. But my first reaction is also "needs smarter JSX parser".
The thing to worry about is whether certain parse errors might make the parser think that the E4X literal ends much later in the buffer than it actually does. But yes, it can work. |
@jacksonrayhamilton ok thanks for the workaround |
Another thing to consider: Maybe dedicated linter programs aren't just a "workaround," but are actually the best way forward. I don't use js-mode's linting, because I believe the 3rd-party solutions (JSLint, JSHint, JSCS, ESLint...) are superior. They are highly featureful, modernized and configurable. They also work outside of Emacs, which is a dealbreaker if you work on a team with a diverse ecosystem of editors, but you all need to maintain the same code quality standards. Delegating linting elsewhere would also be a relief. We could remove quite a few options from the Customize interface, probably hundreds of lines of code, and close out #260, #237, #225, and #29. |
Would you delegate parsing to an external program as well? Or do you propose to continue parsing the buffer contents in Elisp, but never show parsing errors? That's definitely not ideal. |
@jacksonrayhamilton Ah ok cool, will definitely look into using a linter |
I think that'd also be a good idea.
Having an AST is still useful for other purposes, like finding nodes, refactoring and coloring--things that are best done in the editor. We just don't have to set the expectation that we also do error checking, because that can be done independently. |
That would be #288.
If the AST has errors, it can be incorrect, including places arbitrarily far after the error. If we don't signal the error somehow, the user might wonder why sometimes an AST-based operation fails. |
Maybe we could just drop warnings, and still report errors. Users could still have feedback OOTB, and maintainers could enjoy most of the reliefs I mentioned earlier. Although it won't automatically solve this issue. 😜 |
You can already disable all warnings via custom variables, but, like you said, this issue is about a parse error. |
I am having second thoughts about adding
And now the scope of this JavaScript mode will creep to non-standard extensions, which is a bad idea in terms of maintenance and forwards-compatibility. It's rather outrageous that Facebook coerces developers to use non-standard extensions as a means to stomach their otherwise-"ugly" React.js API. But what's even more disappointing is that developers take the bait. I have come under the suspicion that JSX is fundamentally a bad thing, because it mixes the concerns of HTML with JavaScript. It appeals to the emotions of web developers, but validates their fixation on a less-than-ideal application presentation layer, which will prevent the web platform from improving. And it's certainly unnecessary, even if one is in the business of generating HTML. So, I'm thinking now we ought to remove JSX support, for the betterment of the JavaScript and web development community, and for the sake of setting reasonable expectations for this mode. It is unfortunate to provide indentation support just to yank it away a few weeks later, but the benefits of setting a policy against scope creep should outweigh the (perceived) inconvenience in the long run. |
It doesn't matter much to me one way or the other, but let's face it, whether we support JSX or not, that won't have a significant impact on JSX's popularity. The main result will be that Emacs users that do work with it, will have to suffer broken indentation, or use other editors for this task.
Do you mean only
Considering we've split them into separate major modes, I wouldn't really call it scope creep. |
This can be solved by not using JSX. Lack of support should incite users to reject it.
Both.
Despite an organizational strategy, it's still an influx of features: Syntax highlighting, parsing, errors / warnings, comments, niceties like Will it be worth all this effort so that users can write HTML inside their JS? I'd say no. I thought I could ease my suffering by adding indentation support for JSX, but it seems I'm only preparing us (maintainers and users) for more suffering. |
In industrial environment, on average, you don't make most of the choices. They're either made collectively (and Emacs is not the most popular editor for JS), or by other people long before you. Then the user's choice is to pick another editor or use Also recall that until you contributed the JSX indentation support, people were using
I would be against removing
Failing indentation in some cases is unfortunate, but otherwise I'm perfectly happy to only provide indentation support, until someone else very clever comes along and contributes the rest. |
Those aren't the only choices. A person has the power to reject complexity. A superior can make the decision to not use something, and an inferior can petition against it. But if we continue to support this thing, users have a means to cope with their fate, and there will be less a chance of anyone doing anything. That sounds bleak to me. Whether people already show signs of helplessness is irrelevant to me. The popularity of Emacs is irrelevant. The creation of a thousand forests is in one acorn. We should reject this, and tell our users to do so too. Programmers have to realize that the deletion of features can be just as valuable as their addition. |
They still have that means: use This reminds me of the last year's Emacs vs. Clang discussion: https://lwn.net/Articles/583390/ RMS similarly argued that since Clang undermines the power GCC, being copyleft, afforded to FSF, Emacs shouldn't hurry to use it. You can see for yourself how well that discussion went. LLVM didn't suddenly lose in popularity either. |
This quote stood out to me:
RMS regards the rights of users to be of greater importance than the features offered by non-free compilers. If I should be a steward to the users and the codebase, then I should stand in opposition. The languages those compilers support (notably C++) are so complicated that the availability of a tool to analyze them is a point of contention. Another takeaway from that discussion is it wouldn't have happened in the first place if programmers chose simpler languages. Programmers can at least choose JS over JSX, so that's what I'd like to encourage. |
I agree with the sentiment, but there is a point at which protecting users' rights by taking away capabilities becomes an oxymoron. Here's a quote that rings closer to reality, IMHO: https://lists.gnu.org/archive/html/emacs-devel/2015-01/msg00171.html But anyway, while I disagree, I'm perfectly happy to delegate that decision to someone else, e.g. Emacs' current maintainer. @jwiegley, what do you think? |
I loved the camel proverb, btw, I will have to remember that one. Can we fork the jsx support into its own add-on module, on top of but not part of js2-mode? haskell-mode recently went through the problem of incorporating far too many features in what should have remained a core set of functionality, rather than breaking the new features into separate addons. I don't think we should disallow or discourage JSX use, but having it be separate from js2-mode development sounds like it would be preferable. |
I would say the same, but actually, it's already separate: we've added So |
@jacksonrayhamilton I do agree with you regarding JSX, but it is impossible to stop its popularity and JS2 mode has to support it one way or another. That said, I created an example repo that we can all point people at for using React without JSX: https://github.com/ustun/react-without-jsx Putting this here in case I can change some people's minds. |
Hey, wanted to apologize for getting so zealous about this. I've realized that it does not really matter if I think JSX is a "good" thing or not. It exists, and more people are using it, so it is relevant that there be support for it. The weight that a feature has with respect to being non-standard is a function of the mode's extensibility. So I'm returning to the belief that JSX is not necessarily the issue, rather it would be the parser, as we originally concluded. |
ping |
Patch welcome. |
I've taken a first crack at implementing a parser for JSX in a separate package. The very rough first version is here. Though the parser is not built for modularity, I was able to plug in the different XML parser by simply advising The parser correctly handles cases like the one highlighted in this issue as well as ternary expressions inside JSX bracketed expressions (even with crazy nesting). This is definitely not production-ready (it will hang sometimes on malformed/partially formed expressions), but the fontification and elimination of spurious syntax errors is quite nice. I think it will also be straightforward to check for unbound tag names once I figure out how scopes work in js2. |
@felipeochoa This is great. When you figure out the hangs, feel free to open a pull request to merge that code into Although to be frank, keeping it as an external package should work just as well: one advice won't affect the performance to any perceptible degree. |
I think the parser is stable now! One odd thing I noticed is that when I use it with There are a few things I haven't done since I haven't fully grokked the Rhino/js2 parser code, so would appreciate some pointers/help:
PS: Sorry to commandeer this issue, but it seemed like the most active JSX-related place |
Not really. Do you have an example?
It's used in the parser tests (see
Which function?
It's used in
You could open a new one. It's fine, though. |
@felipeochoa and @dgutov wow thanks this looks amazing - will it be available on melpa soon? |
@benzitohhh thanks! I need to add tests and generally vet it more (currently doing a lot of React so that won't be a problem) before putting it up there. Maybe in a week or two. If you're interested in being an alpha tester though, you're welcome to download it and try it out -- I haven't had any issues with it since the last batch of commits. |
OK thanks will have a look
|
Thank you very much @felipeochoa |
Nice work, @felipeochoa. Have you considered merging what is now in It seems like a bit much to have all these different modes providing different pieces of the puzzle. |
Thanks! I'm fine with either approach -- I'd rather give |
Sounds good to me. |
@felipeochoa rjsx is amazing - thanks for making it happen |
I'm +10 on this change! I've already signed the Emacs CLA, and save for a
couple of fairly self contained PRs have written all the rjsx code so it
should be easy legally to add to Emacs. Happy to relicense my code under
GPL or whatever is necessary here.
Unfortunately won't be able to do the merging myself, but by all means go
ahead here!
Thanks for cleaning up the JSX Emacs mess :)
…-Felipe
On Mon, Jun 3, 2019, 9:38 PM Jackson Ray Hamilton ***@***.***> wrote:
@felipeochoa <https://github.com/felipeochoa> In light of recent
improvements to Emacs’ built-in support for JSX, in this post
<#523 (comment)> I
resurfaced the notion of integrating RJSX’s extensions to the JS2 parser
into this repository.
It would be more valuable now for RJSX’s parsing to be a part of JS2,
since JS2’s inheritance of some of the new features provided by js-mode
feels contingent on more comprehensive support for JSX being available here
(i.e., also being able to lint it and highlight it).
I know you’ve expressed that your time is more limited lately. If you
aren’t available to undertake this task now, but if you also are at least
in agreement on the value of it, then we could plan for it and elicit help
from generous volunteers.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#292?email_source=notifications&email_token=AAU4Y4AI4PWCCW7LROBOQWLPYXIQPA5CNFSM4BT5PLV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW3HJUA#issuecomment-498496720>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAU4Y4DL2QDGZU3BRMY3NQLPYXIQPANCNFSM4BT5PLVQ>
.
|
I originally left a comment here and you must have received an email about that, but I deleted the comment right away because I decided it’d be better to create a new issue without necromancing and inheriting the weight of this issue. Here’s that comment I deleted:
In any case, message received! Thanks Felipe. |
If you've read the comments, the recommendation was to use And now you can also install a pre-release build of Emacs 29 compiled with tree-sitter, build js grammar, and try |
Ah! I missed that. Thanks |
In js2-jsx-mode (and js2-mode)...
Simple JSX expressions work ok, for example:
But the below conditional JSX statement is shown with a "missing ) in parenthetical" errorr:
The text was updated successfully, but these errors were encountered: