tree-sitter / tree-sitter-javascript

Javascript grammar for tree-sitter
MIT License
323 stars 109 forks source link

Wrong syntax of comments in jsx. #211

Closed ashishbinu closed 9 months ago

ashishbinu commented 2 years ago

The following piece of code is valid but it is parsed incorrectly:

import React from 'react'
function App() {

  return (
    <>
      //<p>Hello World</p>
    </>
  );
}

export default App;

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:

https://www.typescriptlang.org/play?ssl=11&ssc=20&pln=1&pc=1#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wChMBXAOw2AirgEEwwAKASjgG9TS44iMClAas+-OAB4AfOIlwA9AslhpACSQAbTRDgB1aJoAmkharlSFs-uwDcpAL68kAD0iw4RpJhQVN8ZjBbIA

The is not the output of tree-sitter parse but the output from tree-sitter playground. I couldn't get the tree-sitter parse command working.

program [0, 0] - [11, 0]
  import_statement [0, 0] - [0, 25]
    import_clause [0, 7] - [0, 12]
      identifier [0, 7] - [0, 12]
    source: string [0, 18] - [0, 25]
  function_declaration [1, 0] - [8, 1]
    name: identifier [1, 9] - [1, 12]
    parameters: formal_parameters [1, 12] - [1, 14]
    body: statement_block [1, 15] - [8, 1]
      return_statement [3, 2] - [7, 4]
        parenthesized_expression [3, 9] - [7, 3]
          jsx_fragment [4, 4] - [6, 7]
            comment [4, 6] - [5, 26]
            jsx_text [5, 26] - [6, 4]
  export_statement [10, 0] - [10, 19]
    value: identifier [10, 15] - [10, 18]

I guess it should be considered as error when // or /* */ is used on jsx tags. May be the node should be called comment and it's direct child as jsx_comment with same range because these kind of comments are only related to jsx tags. For other softwares using this treesitter it might be handy to get to know if it is a jsx comment or not. For example : This commenting plugin for neovim editor was not able to implement jsx support and adding jsx_comment node might be helpful. .

// and /* */ are considered as comments by treesitter in jsx but {/* */} syntax is the correct comment syntax for jsx like mentioned here.

mjambon commented 2 years ago

thanks for reporting this!