tree-sitter / tree-sitter-javascript

Javascript grammar for tree-sitter
MIT License
314 stars 108 forks source link

Import statements with newlines in them not parsing correctly #283

Closed helixbass closed 3 months ago

helixbass commented 4 months ago

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

import { b } from 
'b'; /* comment */ import
 { a } from 'a';

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

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgIzgXzgMyhEcBQA5MoQNxwD0AVHAMa4gCmAdvFRXKJLPonAIbosOPIX5kgA

The output of tree-sitter parse is the following:

(program [0, 0] - [3, 0]
  (import_statement [0, 0] - [1, 4]
    (import_clause [0, 7] - [0, 12]
      (named_imports [0, 7] - [0, 12]
        (import_specifier [0, 9] - [0, 10]
          name: (identifier [0, 9] - [0, 10]))))
    source: (string [1, 0] - [1, 3]
      (string_fragment [1, 1] - [1, 2])))
  (comment [1, 5] - [1, 18])
  (expression_statement [1, 19] - [1, 25]
    (import [1, 19] - [1, 25]))
  (statement_block [2, 1] - [2, 6]
    (expression_statement [2, 3] - [2, 4]
      (identifier [2, 3] - [2, 4])))
  (expression_statement [2, 7] - [2, 11]
    (identifier [2, 7] - [2, 11]))
  (expression_statement [2, 12] - [2, 16]
    (string [2, 12] - [2, 15]
      (string_fragment [2, 13] - [2, 14]))))
/Users/jrosse/prj/tmp-js/tmp.js 0 ms    (MISSING ";" [2, 11] - [2, 11])

It looks like this should be parsing as two top-level import_statement's but instead the second import statement is parsing as multiple top-level statements (I guess due to the newline in the middle of it)

amaanq commented 3 months ago

Thanks for your bug reports! I've fixed a few of them, the block/label stuff is quite tricky for now though sadly - let me know of other issues you run into!