microsoft / microsoft/TypeScript

TSServer navtree — Way to differentiate function declarations from function expressions

Open
#29,036 1 comment 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: API Domain: LS: TSServer In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.3.0-dev.20181214

Search Terms:

  • tsserver
  • navtree

Problem
For https://github.com/Microsoft/vscode/issues/22981, we'd like a way to differentiate function delcarations from function expressions. Consider a file:

function foo(f) { }

foo(function bar() { })

const car = function dar(f) { }

VS Code uses the navtree to get possible code lens locations. We would only like to show references code lenses on function foo and perhaps on const car =, not on the expression foo(function bar() {}).

Using the navtree request against the TS Server, we cannot tell function declarations like foo from function expressions like bar :

 {
            "text": "bar",
            "kind": "function",
            "kindModifiers": "",
            "spans": [
                {
                    "start": {
                        "line": 3,
                        "offset": 5
                    },
                    "end": {
                        "line": 3,
                        "offset": 23
                    }
                }
            ],
            "nameSpan": {
                "start": {
                    "line": 3,
                    "offset": 14
                },
                "end": {
                    "line": 3,
                    "offset": 17
                }
            }
        },
        {
            "text": "dar",
            "kind": "function",
            "kindModifiers": "",
            "spans": [
                {
                    "start": {
                        "line": 5,
                        "offset": 13
                    },
                    "end": {
                        "line": 5,
                        "offset": 32
                    }
                }
            ],
            "nameSpan": {
                "start": {
                    "line": 5,
                    "offset": 22
                },
                "end": {
                    "line": 5,
                    "offset": 25
                }
            }
        },
        {
            "text": "foo",
            "kind": "function",
            "kindModifiers": "",
            "spans": [
                {
                    "start": {
                        "line": 1,
                        "offset": 1
                    },
                    "end": {
                        "line": 1,
                        "offset": 20
                    }
                }
            ],
            "nameSpan": {
                "start": {
                    "line": 1,
                    "offset": 10
                },
                "end": {
                    "line": 1,
                    "offset": 13
                }
            }
        }
    ]

Proposal
Differentiate:

function foo(f) { }

by using a kindModifier of "definition" or a similar name (or do the inverse and mark function bar() { } with a kindModifier of "expression")

Also, we correctly differentiate car using "kind": "const" in the case of:

const car = function(f) { }

However only dar is returned for for:

const car = function dar(f) { }

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 by tracing the tsserver navtree request and its response construction, using the reported function declaration and function expression examples as the expected behavior. Determine where kind and kindModifiers are assigned, then verify that the response distinguishes declarations from expressions, including named functions assigned to const variables.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.