tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
335 stars 104 forks source link

Parse error on import() syntax #246

Closed Wilfred closed 5 months ago

Wilfred commented 1 year ago

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

type ExpressionNamerFn = (
  expr: import('expressions').Expr,
) => string;

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript: https://www.typescriptlang.org/play?#code/C4TwDgpgBAogHmAThAziglgewHYDkCGAthIgGLZQC8UAFAFBRQQKIBcU6hYmiwNA5MySoMOFPwCUAOnhIANHQlUAfFBTBE6bAHMA3EA

The output of tree-sitter parse is the following:

$ difft --dump-ts a.ts
program (0, 0) - (3, 0)
  type_alias_declaration (0, 0) - (2, 12)
    type (0, 0) - (0, 4) "type"
    type_identifier (0, 5) - (0, 22) "ExpressionNamerFn"
    = (0, 23) - (0, 24) "="
    function_type (0, 25) - (2, 11)
      formal_parameters (0, 25) - (2, 1)
        ( (0, 25) - (0, 26) "("
        required_parameter (1, 2) - (1, 34)
          identifier (1, 2) - (1, 6) "expr"
          type_annotation (1, 6) - (1, 34)
            : (1, 6) - (1, 7) ":"
            ERROR (1, 8) - (1, 30)
              identifier (1, 8) - (1, 14) "import"
              parenthesized_type (1, 14) - (1, 29)
                ( (1, 14) - (1, 15) "("
                literal_type (1, 15) - (1, 28)
                  string (1, 15) - (1, 28)
                    ' (1, 15) - (1, 16) "'"
                    string_fragment (1, 16) - (1, 27) "expressions"
                    ' (1, 27) - (1, 28) "'"
                ) (1, 28) - (1, 29) ")"
              . (1, 29) - (1, 30) "."
            type_identifier (1, 30) - (1, 34) "Expr"
        , (1, 34) - (1, 35) ","
        ) (2, 0) - (2, 1) ")"
      => (2, 2) - (2, 4) "=>"
      predefined_type (2, 5) - (2, 11)
        string (2, 5) - (2, 11) "string"
    ; (2, 11) - (2, 12) ";"
amaanq commented 1 year ago

this is becoming a bit snowflakey, but not hard to fix...I guess

DGCK81LNN commented 8 months ago

Seemingly related:

interface Event {
  name: string
  remark?: string
  time: import("@js-temporal/polyfill").Temporal.PlainDateTime
  icon: string
}

icon: string” is not highlighted correctly.

amaanq commented 5 months ago

Hey @Wilfred I've fixed this now, hopefully difftastic will benefit from the latest update!

Wilfred commented 4 months ago

Works nicely, thanks @amaanq! :)

amaanq commented 4 months ago

np :)