Skip to content

JS "generator function" expression are not recognised #736

@tizot

Description

@tizot

Hi,

I am stumbling on an issue similar to #633, more specifically in the case where the generator function is not declared as a statement but as an expression (for example as an argument of another function).
The command :TSTextobjectSelect @function.outer selects nothing when the cursor is placed inside a generator function, when that function is not declared with a statement.

Thanks for the help!

To reproduce

  1. Open a new file, in a neovim instance with TS installed
  2. Write/paste a generator function and assign it to a variable
    const fn = function* () {
      yield 1;
    };
  3. Put cursor inside and try select the whole function with :TSTextobjectSelect @function.outer

Expected behaviour

The full function is selected instead of nothing happening.

Image

Note that the code is correctly parsed and the "function" is parsed as a generator_function:

Image

Additional context

I have tried to add new text objects definitions in the file <config folder>/queries/ecma/textobjects.scm:

(generator_function 
  body: (statement_block)) @function.outer

(generator_function
  body: (statement_block
    .
    "{"
    .
    (_) @_start @_end
    (_)? @_end
    .
    "}"
    (#make-range! "function.inner" @_start @_end)))

But it does not work as expected (now, the :TSTextobjectSelect @function.outer command only selected generator functions and not regular functions anymore 😅). I guess I am missing something...

Output of :checkhealth nvim-treesitter

==============================================================================
nvim-treesitter:                     require("nvim-treesitter.health").check()

Installation ~
- OK `tree-sitter` found 0.25.2 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v20.11.0 (only needed for :TSInstallFromGrammar)
- OK `git` executable found.
- OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: Apple clang version 16.0.0 (clang-1600.0.26.6)
- OK Neovim was compiled with tree-sitter runtime ABI version 15 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "arm64",
  release = "24.3.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:16 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6000"
} ~

Parser/Features         H L F I J
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - diff                ✓ . ✓ . ✓
  - dockerfile          ✓ . . . ✓
  - graphql             ✓ . . ✓ ✓
  - html                ✓ ✓ ✓ ✓ ✓
  - javascript          ✓ ✓ ✓ ✓ ✓
  - json                ✓ ✓ ✓ ✓ .
  - lua                 ✓ ✓ ✓ ✓ ✓
  - markdown            ✓ . ✓ ✓ ✓
  - markdown_inline     ✓ . . . ✓
  - prisma              ✓ . ✓ . ✓
  - python              ✓ ✓ ✓ ✓ ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - regex               ✓ . . . .
  - svelte              ✓ ✓ ✓ ✓ ✓
  - tsx                 ✓ ✓ ✓ ✓ ✓
  - typescript          ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓
  - yaml                ✓ ✓ ✓ ✓ ✓

  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} ~

Output of nvim --version

NVIM v0.11.0-dev-1878+g45d7aa3301
Build type: RelWithDebInfo
LuaJIT 2.1.1736781742
Run "nvim -V1 -v" for more info

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions