tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
331 stars 103 forks source link

Incorrect highlight in typescript in certain piece of code? #171

Closed cmdcolin closed 2 years ago

cmdcolin commented 2 years ago

I am using a stripped down config on neovim 0.5 using nvim.appimage and it seems like perhaps highlighting gets weird on this particular code snippet. Doing small changes to the code can restore highlighting also, but it is not clear what the culprit is (I don't have any experience with the grammars...)


call plug#begin(stdpath('data') . '/plugged')
Plug 'tpope/vim-commentary'
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
call plug#end()

" configure treesitter
lua << EOF
require'nvim-treesitter.configs'.setup {
  ensure_installed = "all", -- one of "all", "maintained" (parsers with maintainers), or a list of languages
  highlight = {
    enable = true,              -- false will disable the whole extension
  },
}
EOF

Running :TSUpdate all parsers are up to date

I tried to strip this code out of a larger file that produces the minimal reproducible example

import { useEffect, useRef, useState } from 'react'
export function useDebouncedCallback(
  callback: any,
  wait = 400,
) {
  const argsRef = useRef<any>()
  const timeout = useRef<ReturnType<typeof setTimeout>>()

  const cleanup = () => {}

  return function debouncedCallback(...args: any) {
    argsRef.current = args

    // clear debounce timer
    cleanup()

    // start waiting again
    timeout.current = setTimeout(() => {
      if (argsRef.current) {
        callback(...argsRef.current)
      }
    }, wait)
  }
}

export function findParentThatIs<T extends any>(
  node: any,
  predicate: T,
) {
  return findParentThat(node, predicate) as any
}

export function assembleLocString(region: any): string {
  const { assemblyName, refName, start, end } = region
  const assemblyNameString = assemblyName ? `{${assemblyName}}` : ''
  let startString
  if (start !== undefined) {
    startString = `:${(start + 1).toLocaleString('en-US')}`
  } else if (end !== undefined) {
    startString = ':1'
  } else {
    startString = ''
  }
  let endString
  if (end !== undefined) {
    endString =
      start !== undefined && start + 1 === end
        ? ''
        : `..${end.toLocaleString('en-US')}`
  } else {
    endString = start !== undefined ? '..' : ''
  }
  return `${assemblyNameString}${refName}${startString}${endString}`
}

Picture of code highlighting, can see that after "let startString" it stops highlighting

Screenshot from 2021-07-14 12-38-48

There is possibly another weird thing with the useRef at the top, the two useRefs are colored differently

Screenshot from 2021-07-14 12-39-18

Here is a bunch of detailed version info if it helps

Version info ``` nvim --version NVIM v0.5.0 Build type: RelWithDebInfo LuaJIT 2.1.0-beta3 Compilation: /usr/bin/gcc-11 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/home/runner/work/neovim/neovim/build/config -I/home/runner/work/neovim/neovim/src -I/home/runner/work/neovim/neovim/.deps/usr/include -I/usr/include -I/home/runner/work/neovim/neovim/build/src/nvim/auto -I/home/runner/work/neovim/neovim/build/include Compiled by runner@fv-az242-526 Features: +acl +iconv +tui See ":help feature-compile" system vimrc file: "$VIM/sysinit.vim" fall-back for $VIM: " /home/runner/work/neovim/neovim/build/nvim.AppDir/usr/share/nvim" Run :checkhealth for more info ``` checkhealth ``` health#nvim_treesitter#check ======================================================================== ## Installation - WARNING: `tree-sitter` executable not found (parser generator, only needed for :TSInstallFromGrammar, not required for :TSInstall) - OK: `node` found v15.14.0 (only needed for :TSInstallFromGrammar) - OK: `git` executable found. - OK: `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl" } - OK: Neovim was compiled with tree-sitter runtime ABI version 13 (required >=13). Parsers must be compatible with runtime ABI. ## Parser/Features H L F I J - rust ✓ ✓ ✓ ✓ ✓ - ledger ✓ . ✓ ✓ ✓ - toml ✓ ✓ ✓ ✓ ✓ - glimmer ✓ . . . . - python ✓ ✓ ✓ ✓ ✓ - gomod ✓ . . . . - graphql ✓ . . ✓ ✓ - jsonc ✓ ✓ ✓ ✓ ✓ - ruby ✓ ✓ ✓ ✓ ✓ - elm . . . . . - bash ✓ ✓ ✓ . ✓ - nix ✓ ✓ ✓ . ✓ - fish ✓ ✓ ✓ ✓ ✓ - dart ✓ ✓ . ✓ ✓ - php ✓ ✓ ✓ ✓ ✓ - fennel ✓ ✓ . . ✓ - java ✓ ✓ . ✓ ✓ - kotlin ✓ . . . ✓ - ql ✓ ✓ . ✓ ✓ - html ✓ ✓ ✓ ✓ ✓ - verilog ✓ ✓ ✓ . ✓ - julia ✓ ✓ ✓ ✓ ✓ - comment ✓ . . . . - jsdoc ✓ . . . . - scss ✓ . . ✓ . - sparql ✓ ✓ ✓ ✓ ✓ - erlang . . . . . - turtle ✓ ✓ ✓ ✓ ✓ - ocaml ✓ ✓ ✓ . ✓ - svelte ✓ . ✓ ✓ ✓ - r ✓ ✓ . . . - beancount ✓ . ✓ . . - c_sharp ✓ . ✓ . ✓ - latex ✓ . ✓ . ✓ - typescript ✓ ✓ ✓ ✓ ✓ - bibtex ✓ . ✓ ✓ . - zig ✓ ✓ ✓ ✓ ✓ - fortran ✓ . ✓ ✓ . - cmake ✓ . ✓ . . - javascript ✓ ✓ ✓ ✓ ✓ - go ✓ ✓ ✓ ✓ ✓ - query ✓ ✓ ✓ ✓ ✓ - ocaml_interface✓ ✓ ✓ . ✓ - clojure ✓ ✓ ✓ . ✓ - yaml ✓ ✓ ✓ ✓ ✓ - lua ✓ ✓ ✓ ✓ ✓ - vue ✓ . ✓ . ✓ - json ✓ ✓ ✓ ✓ . - commonlisp ✓ ✓ ✓ . . - css ✓ . ✓ ✓ ✓ - tsx ✓ ✓ ✓ ✓ ✓ - regex ✓ . . . . - cpp ✓ ✓ ✓ ✓ ✓ - scala . . . . . - cuda ✓ ✓ ✓ ✓ . - supercollider ✓ ✓ ✓ ✓ ✓ - dockerfile ✓ . . . ✓ - rst ✓ ✓ . . ✓ - haskell ✓ . . . ✓ - c ✓ ✓ ✓ ✓ ✓ - hcl ✓ . ✓ . ✓ Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections +) multiple parsers found, only one will be used x) errors found in the query, try to run :TSUpdate {lang} health#nvim#check ======================================================================== ## Configuration - OK: no issues found ## Performance - OK: Build type: RelWithDebInfo ## Remote Plugins - OK: Up to date ## terminal - INFO: key_backspace (kbs) terminfo entry: key_backspace=^H - INFO: key_dc (kdch1) terminfo entry: key_dc=\E[3~ - INFO: $VTE_VERSION='6200' - INFO: $COLORTERM='truecolor' ## tmux - OK: escape-time: 10 - INFO: Checking stuff - WARNING: `focus-events` is not enabled. |'autoread'| may not work. - ADVICE: - (tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf: set-option -g focus-events on - INFO: $TERM: screen-256color - WARNING: Neither Tc nor RGB capability set. True colors are disabled. |'termguicolors'| won't work properly. - ADVICE: - Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux: set-option -sa terminal-overrides ',XXX:RGB' - For older tmux versions use this instead: set-option -ga terminal-overrides ',XXX:Tc' health#provider#check ======================================================================== ## Clipboard (optional) - OK: Clipboard tool found: xclip ## Python 2 provider (optional) - WARNING: No Python executable found that can `import neovim`. Using the first available executable for diagnostics. - ERROR: Python provider error: - ADVICE: - provider/pythonx: Could not load Python 2: /usr/bin/python2 does not have the "neovim" module. :help |provider-python| /usr/bin/python2.7 does not have the "neovim" module. :help |provider-python| python2.6 not found in search path or not executable. /home/linuxbrew/.linuxbrew/opt/python@3.9/libexec/bin/python is Python 3.9 and cannot provide Python 2. - INFO: Executable: Not found ## Python 3 provider (optional) - INFO: `g:python3_host_prog` is not set. Searching for python3.8 in the environment. - INFO: Executable: /usr/bin/python3.8 - INFO: Python version: 3.8.10 - INFO: pynvim version: 0.4.1 (outdated; from /usr/lib/python3/dist-packages/neovim) - WARNING: Latest pynvim is NOT installed: 0.4.3 ## Python virtualenv - OK: no $VIRTUAL_ENV ## Ruby provider (optional) - INFO: Ruby: ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux-gnu] - WARNING: `neovim-ruby-host` not found. - ADVICE: - Run `gem install neovim` to ensure the neovim RubyGem is installed. - Run `gem environment` to ensure the gem bin directory is in $PATH. - If you are using rvm/rbenv/chruby, try "rehashing". - See :help |g:ruby_host_prog| for non-standard gem installations. ## Node.js provider (optional) - INFO: Node.js: v15.14.0 - WARNING: Missing "neovim" npm (or yarn) package. - ADVICE: - Run in shell: npm install -g neovim - Run in shell (if you use yarn): yarn global add neovim ## Perl provider (optional) - ERROR: perl provider error: - ADVICE: - "Neovim::Ext" cpan module is not installed health#treesitter#check ======================================================================== ## Checking treesitter configuration - INFO: Runtime ABI version : 13 - OK: Loaded parser for bash: ABI version 13 - OK: Loaded parser for beancount: ABI version 13 - OK: Loaded parser for bibtex: ABI version 13 - OK: Loaded parser for c: ABI version 13 - OK: Loaded parser for c_sharp: ABI version 13 - OK: Loaded parser for clojure: ABI version 13 - OK: Loaded parser for cmake: ABI version 13 - OK: Loaded parser for comment: ABI version 13 - OK: Loaded parser for commonlisp: ABI version 13 - OK: Loaded parser for cpp: ABI version 13 - OK: Loaded parser for css: ABI version 13 - OK: Loaded parser for cuda: ABI version 13 - OK: Loaded parser for dart: ABI version 13 - ERROR: Impossible to load parser for devicetree: ...p/usr/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /home/cdiesh/.local/share/nvim/plugged/nvim-treesitter/parser/devicetree.so: supported between 13 and 13, found 12 - OK: Loaded parser for dockerfile: ABI version 13 - OK: Loaded parser for elm: ABI version 13 - OK: Loaded parser for erlang: ABI version 13 - OK: Loaded parser for fennel: ABI version 13 - OK: Loaded parser for fish: ABI version 13 - OK: Loaded parser for fortran: ABI version 13 - ERROR: Impossible to load parser for gdscript: ...p/usr/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /home/cdiesh/.local/share/nvim/plugged/nvim-treesitter/parser/gdscript.so: supported between 13 and 13, found 12 - OK: Loaded parser for glimmer: ABI version 13 - OK: Loaded parser for go: ABI version 13 - OK: Loaded parser for gomod: ABI version 13 - OK: Loaded parser for graphql: ABI version 13 - OK: Loaded parser for haskell: ABI version 13 - OK: Loaded parser for hcl: ABI version 13 - OK: Loaded parser for html: ABI version 13 - OK: Loaded parser for java: ABI version 13 - OK: Loaded parser for javascript: ABI version 13 - OK: Loaded parser for jsdoc: ABI version 13 - OK: Loaded parser for json: ABI version 13 - OK: Loaded parser for jsonc: ABI version 13 - OK: Loaded parser for julia: ABI version 13 - OK: Loaded parser for kotlin: ABI version 13 - OK: Loaded parser for latex: ABI version 13 - OK: Loaded parser for ledger: ABI version 13 - OK: Loaded parser for lua: ABI version 13 - OK: Loaded parser for nix: ABI version 13 - OK: Loaded parser for ocaml: ABI version 13 - OK: Loaded parser for ocaml_interface: ABI version 13 - ERROR: Impossible to load parser for ocamllex: ...p/usr/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /home/cdiesh/.local/share/nvim/plugged/nvim-treesitter/parser/ocamllex.so: supported between 13 and 13, found 11 - OK: Loaded parser for php: ABI version 13 - OK: Loaded parser for python: ABI version 13 - OK: Loaded parser for ql: ABI version 13 - OK: Loaded parser for query: ABI version 13 - OK: Loaded parser for r: ABI version 13 - OK: Loaded parser for regex: ABI version 13 - OK: Loaded parser for rst: ABI version 13 - OK: Loaded parser for ruby: ABI version 13 - OK: Loaded parser for rust: ABI version 13 - OK: Loaded parser for scala: ABI version 13 - OK: Loaded parser for scss: ABI version 13 - OK: Loaded parser for sparql: ABI version 13 - OK: Loaded parser for supercollider: ABI version 13 - OK: Loaded parser for svelte: ABI version 13 - ERROR: Impossible to load parser for swift: ...p/usr/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /home/cdiesh/.local/share/nvim/plugged/nvim-treesitter/parser/swift.so: supported between 13 and 13, found 10 - OK: Loaded parser for teal: ABI version 13 - OK: Loaded parser for toml: ABI version 13 - OK: Loaded parser for tsx: ABI version 13 - OK: Loaded parser for turtle: ABI version 13 - OK: Loaded parser for typescript: ABI version 13 - OK: Loaded parser for verilog: ABI version 13 - OK: Loaded parser for vue: ABI version 13 - OK: Loaded parser for yaml: ABI version 13 - OK: Loaded parser for zig: ABI version 13 - OK: Loaded parser for c: ABI version 13 ```
resolritter commented 2 years ago

The problem occurs due to useRef<ReturnType<typeof setTimeout>>, that is, typeof used in the generic arguments of a generic type argument of a generic type.

Reduced reproduction: type T = Foo<Bar<typeof baz>>