tree-sitter / tree-sitter-go

Go grammar for tree-sitter
MIT License
317 stars 63 forks source link

Module is a node type of variable #135

Closed tkrause closed 10 months ago

tkrause commented 10 months ago

It seems that the treesitter implementation for Go does not correctly parse modules. The module is correctly parsed when inside the function declaration.

However, when a module is used in a function or method it instead becomes a type of variable.

Example Code:

package main

import (
    "fmt"
    "time"
)

// Here time is a module
func getTime(t time.Time) string {
    return t.String()
}

func main() {
        // Here, fmt and time are variables not modules
    fmt.Println("The time is: ", getTime(time.Now()))
}

Parsed Tree:

[source_file](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [15, 0]
  [package_clause](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 12]
    [package_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 8] - [0, 12]
  [import_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [2, 0] - [5, 1]
    [import_spec_list](https://tree-sitter.github.io/tree-sitter/playground#) [2, 7] - [5, 1]
      [import_spec](https://tree-sitter.github.io/tree-sitter/playground#) [3, 1] - [3, 6]
        path: [interpreted_string_literal](https://tree-sitter.github.io/tree-sitter/playground#) [3, 1] - [3, 6]
      [import_spec](https://tree-sitter.github.io/tree-sitter/playground#) [4, 1] - [4, 7]
        path: [interpreted_string_literal](https://tree-sitter.github.io/tree-sitter/playground#) [4, 1] - [4, 7]
  [function_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [7, 0] - [9, 1]
    name: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [7, 5] - [7, 12]
    parameters: [parameter_list](https://tree-sitter.github.io/tree-sitter/playground#) [7, 12] - [7, 25]
      [parameter_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [7, 13] - [7, 24]
        name: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [7, 13] - [7, 14]
        type: [qualified_type](https://tree-sitter.github.io/tree-sitter/playground#) [7, 15] - [7, 24]
          package: [package_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [7, 15] - [7, 19]
          name: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [7, 20] - [7, 24]
    result: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [7, 26] - [7, 32]
    body: [block](https://tree-sitter.github.io/tree-sitter/playground#) [7, 33] - [9, 1]
      [return_statement](https://tree-sitter.github.io/tree-sitter/playground#) [8, 1] - [8, 18]
        [expression_list](https://tree-sitter.github.io/tree-sitter/playground#) [8, 8] - [8, 18]
          [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [8, 8] - [8, 18]
            function: [selector_expression](https://tree-sitter.github.io/tree-sitter/playground#) [8, 8] - [8, 16]
              operand: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [8, 8] - [8, 9]
              field: [field_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [8, 10] - [8, 16]
            arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [8, 16] - [8, 18]
  [function_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [11, 0] - [13, 1]
    name: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [11, 5] - [11, 9]
    parameters: [parameter_list](https://tree-sitter.github.io/tree-sitter/playground#) [11, 9] - [11, 11]
    body: [block](https://tree-sitter.github.io/tree-sitter/playground#) [11, 12] - [13, 1]
      [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [12, 1] - [12, 50]
        function: [selector_expression](https://tree-sitter.github.io/tree-sitter/playground#) [12, 1] - [12, 12]
          operand: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [12, 1] - [12, 4]
          field: [field_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [12, 5] - [12, 12]
        arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [12, 12] - [12, 50]
          [interpreted_string_literal](https://tree-sitter.github.io/tree-sitter/playground#) [12, 13] - [12, 28]
          [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [12, 30] - [12, 49]
            function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [12, 30] - [12, 37]
            arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [12, 37] - [12, 49]
              [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [12, 38] - [12, 48]
                function: [selector_expression](https://tree-sitter.github.io/tree-sitter/playground#) [12, 38] - [12, 46]
                  operand: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [12, 38] - [12, 42]
                  field: [field_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [12, 43] - [12, 46]
                arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [12, 46] - [12, 48]`
amaanq commented 10 months ago

tree-sitter is not context-aware, that's what queries are for, so you can tag specific bits and correlate that with their usage later on within a scope.