nvim-treesitter / nvim-treesitter/nvim-treesitter-textobjects

JS "generator function" expression are not recognised

Open
#736 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Tree-sitter Query
Stars
2.8k
Forks
271
Avg merge
8d 8h
Merged PRs (30d)
1

Description

Hi,

I am stumbling on an issue similar to https://github.com/nvim-treesitter/nvim-treesitter-textobjects/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up β€” it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with /queries/ecma/textobjects.scm and inspect how the function.outer captures cover regular functions versus generator_function nodes. Reproduce the issue with the provided JavaScript generator expression and :TSTextobjectSelect @function.outer, then verify that both expression generators and regular functions select the full function without breaking existing text objects.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.