tree-sitter / tree-sitter-typescript

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

Type only imports and exports breaks syntax highlighting #242

Closed Korywon closed 5 months ago

Korywon commented 1 year ago

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

import { type AppTab } from '@/types'
import {
  Button,
  Grid,
  GridItem,
  useDisclosure
} from '@chakra-ui/react'

Screenshot of the bug I'm seeing:

image

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

The output of tree-sitter parse is the following:

❯ tree-sitter parse import-type.ts
(program [0, 0] - [7, 0]
  (import_statement [0, 0] - [0, 37]
    (import_clause [0, 7] - [0, 22]
      (named_imports [0, 7] - [0, 22]
        (import_specifier [0, 9] - [0, 20]
          name: (identifier [0, 14] - [0, 20]))))
    source: (string [0, 28] - [0, 37]
      (string_fragment [0, 29] - [0, 36])))
  (import_statement [1, 0] - [6, 25]
    (import_clause [1, 7] - [6, 1]
      (named_imports [1, 7] - [6, 1]
        (import_specifier [2, 2] - [2, 8]
          name: (identifier [2, 2] - [2, 8]))
        (import_specifier [3, 2] - [3, 6]
          name: (identifier [3, 2] - [3, 6]))
        (import_specifier [4, 2] - [4, 10]
          name: (identifier [4, 2] - [4, 10]))
        (import_specifier [5, 2] - [5, 15]
          name: (identifier [5, 2] - [5, 15]))))
    source: (string [6, 7] - [6, 25]
      (string_fragment [6, 8] - [6, 24]))))

I was expecting the parser to not expect = after the variable. When wrote = in front of my import, it seems to fix the highlighting. FWIW, I am using Astrovim.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html

SpanishPear commented 1 year ago

Also seeing this - perhaps related to b66d19b9b6ec3edf3d8aff0c20646acbdaa0afb3

Korywon commented 1 year ago

I noticed that import type { ... } 'package' works just fine, but doing import { type PackageType } from 'package' is what breaks it.

image image
sammyolo commented 7 months ago

o dear, this is open since April 26? anything I can do to help get this fixed?

for me this issue still persists, or is there a way to get the newer version, :TSUpdate won't do

xlboy commented 6 months ago

T-T

amaanq commented 5 months ago

seems fine on master. let me know of parse errors if not

yohendry commented 1 month ago

its happening to me