Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Syntax highlighting breaks on ' in JSX #220

Closed

Description

@rkuykendall

I'm not sure if this is the right repo, but from what I can tell this is what Github uses for syntax highlighting. This code looks fine in my Atom, but breaks on github at a ' in JSX.

'use strict';

const React = require('react')

module.exports = React.createClass({
  render: function() {
    let {feeds, log} = this.props;

    log.info(feeds);
    return <div className="feed-list">
      <h3>News Feed's</h3>
      <ul>
        {feeds.map(function(feed) {
          return <li key={feed.name} className={feed.fetched ? 'loaded' : 'loading'}>
            {feed.data && feed.data.length > 0 ?
              <span>{feed.name} <span className='light'>({feed.data.length})</span></span>
              : 'feed.name' }
          </li>
        })}
      </ul>
    </div>;
  }
});

Activity

  1. Victorystick commented on Sep 4, 2015

    @Victorystick
    Contributor

    What you're seeing is language-javascript not supporting JSX at all...

    <h3> is tokenized as operator, Identifier, operator. It doesn't know JSX considers it an opening tag. Once ' is reached out tries matching what it considers a string literal. However, the literal isn't closed before the newline, which is a syntax error.

  2. rkuykendall commented on Sep 4, 2015

    @rkuykendall
    Author

    Do you know why this is broken on Github snippets but not the Atom editor? Are they not using the same repos?

  3. Victorystick commented on Sep 4, 2015

    @Victorystick
    Contributor

    They are both using the same repo, but they're not always using the exact same version. language-javascript is changing pretty fast, so it's hard to keep up.

  4. Victorystick commented on Sep 4, 2015

    @Victorystick
    Contributor

    Are you running the latest version of Atom? It was only released two days ago, and I'm pretty sure it includes this particular behavior for strings.

  5. winstliu commented on Sep 4, 2015

    @winstliu
    Contributor

    @rkuykendall what file extension does your file have? .jsx or .js?

  6. rkuykendall commented on Sep 4, 2015

    @rkuykendall
    Author

    @50Wliu The original was .js, but I made a Gist with .es6 and .jsx and got the same thing.

    @Victorystick Ah, I just double-checked my Atom and it looks like I'm using the Babel ES6 JavaScript syntax highlighter, and switching it to Javascript does indeed act like this.

  7. winstliu commented on Sep 4, 2015

    @winstliu
    Contributor

    language-javascript doesn't support JSX at this point. @pchaigno do you know what language Github uses to parse .es6 and .jsx files?

  8. pchaigno commented on Sep 4, 2015

    @pchaigno
    Contributor

    .es6 and .jsx files are highlighted as JavaScript currently. We could use @rkuykendall's grammar...
    @rkuykendall Is this the grammar you were mentioning?

  9. mehcode commented on Sep 5, 2015

    @mehcode

    For the record I use atom-react and it auto detects jsx in .js files by the React import and colorizes just the JSX.

  10. pchaigno commented on Sep 5, 2015

    @pchaigno
    Contributor

    @50Wliu Do you have some examples of .es6 files where the current highlighting fails? I want to test the grammars mentioned so far to see which one we should use.
    Do .jsx and .es6 files have the same syntax?

  11. Victorystick commented on Sep 5, 2015

    @Victorystick
    Contributor

    ECMAScript 6 (2015) and JSX are two different things, with their own grammars and syntaxes. language-javascript should already support all ES6 features, so it could be used for .es6 files.

    JSX is an unofficial extension to ECMAScript 6, and adds support for XML-like tags. Since it isn't part of the standard, it isn't supported by language-javascript. Therefore, .jsx files should be highlighted using a different grammar, such as language-babel.

    tl;dr; JS == ES6 && JSX != ES6

  12. winstliu commented on Sep 5, 2015

    @winstliu
    Contributor

    Sorry, wasn't thinking correctly last night when I wrote that up. @Victorystick is 100% correct.

  13. rkuykendall commented on Sep 5, 2015

    @rkuykendall
    Author

    @pchaigno Yes, language-babel is the package my current Atom uses. I actually forgot I had that installed. Also I agree with @Victorystick that not supporting it in language-javascript makes perfect sense. Thanks for making the new issue on Linguist!

    Edit: Thanks for making a Pull Request on Linugist!!

  14. pchaigno commented on Sep 8, 2015

    @pchaigno
    Contributor

    This should be fixed in a couple weeks with the next release of Linguist ;)

  15. rkuykendall commented on Sep 8, 2015

    @rkuykendall
    Author

    Thanks for all your help @pchaigno!

  16. reyronald commented on Jan 24, 2018

    @reyronald

    Hello guys, I'm sorry to comment on a three year old issue, hope I don't get any of you upset.

    As of now, most of the JSX code out there actually resides in .js files and not .jsx files. Just as an example, if I'm not mistaken none of the official React code by Facebook on any of their repositories use the jsx extension.

    What implications would it have to use the JSX grammar to parse .js files? Would it to be too crazy of an idea to do this? I'm aware that JSX is not part of the official JavaScript spec, but in practical terms, there are lots of people writing JSX code in their pure JavaScript files, and in a way it has become the standard practice.

    There's a lot of code in pull requests that I review on a daily basis that looks like this and it's annoying :/.

    const node = (
      <div>
        I'm Ronald.
      </div>
    );
  17. winstliu commented on Jan 25, 2018

    @winstliu
    Contributor

    What implications would it have to use the JSX grammar to parse .js files?

    For the vast majority of cases I don't think there will be an issue. Atom can already do this. However for GitHub you'll have to ask the Linguist about that possibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions