Skip to content

When parsing a Kotlin method node, the getType method returns ERROR. #108

@yk-liang

Description

@yk-liang

Describe the bug
When I try to use tree-sitter-ng to parse the following Kotlin code, upon reaching the setHide method, theoretically: I should obtain a node of type function_declaration (with the parent node being class_body), but when I retrieve the node type using getType, I get ERROR instead.

Expected behavior
I should obtain a node of type function_declaration (with the parent node being class_body)

OS and Java version

  • OS: Linux 64bit
  • Java version: 17

Sample code:

package mtl;

import org.treesitter.TSNode;
import org.treesitter.TSParser;
import org.treesitter.TSTree;
import org.treesitter.TreeSitterKotlin;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class Main {
    public static void main(String[] args) throws IOException {
        String kotlinFilePath = "path/to/file";
        parseFile(kotlinFilePath);
    }

    public static Map<String, Object> parseFile(String absoluteFilePath) throws IOException {
        TSParser parser = new TSParser();
        parser.setLanguage(new TreeSitterKotlin());
        String sourceCode = Files.readString(Paths.get(absoluteFilePath));
        TSTree tree = parser.parseString(null, sourceCode);
        TSNode root = tree.getRootNode();
        for (int i = 0; i < root.getChildCount(); i++) {
            TSNode typeDeclarationNode = root.getChild(i);
            if ("class_declaration".equals(typeDeclarationNode.getType())) {
                for (int j = 0; j < typeDeclarationNode.getNamedChildCount(); j++) {
                    TSNode classBody = typeDeclarationNode.getNamedChild(j);
                    if ("class_body".equals(classBody.getType())) {
                        for (int k = 0; k < classBody.getNamedChildCount(); k++) {
                            TSNode child = classBody.getNamedChild(k);
                            System.out.println(child.getType());
                        }
                    }
                }
            }
        }
        return null;
    }
}

Source code:

class CartShopHideManager {
    private var hideStatusMap: MutableMap<String, Boolean>?


    init {
        hideStatusMap = HashMap()
    }

    fun itemRegistered(itemComponent: Component?): Boolean {
        (itemComponent as ItemComponent).shopViewMore?.needHide()?.let {
            if (it) {
                val parentId = (itemComponent as ItemComponent).parent?.id
                if (parentId?.let { it1 -> isHide(it1) } == true) {
                    return true
                }
            }
        }
        return false
    }


    fun isHide(parentId: String): Boolean {
        hideStatusMap = hideStatusMap ?: HashMap<String, Boolean>().apply {
            return true
        }
        hideStatusMap?.let {
            if (!parentId.isNullOrEmpty() && it[parentId] != null && it[parentId] is Boolean) {
                return it[parentId] as Boolean
            }
        }
        return true
    }

    fun setHide(parentId: String, hide: Boolean) {
        if (hideStatusMap == null || TextUtils.isEmpty(parentId)) {
            hideStatusMap = HashMap()
            (hideStatusMap as HashMap<String, Boolean>)[parentId] = true
        } else {
            hideStatusMap!![parentId] = hide
        }
    }

    fun clear() {
        hideStatusMap?.clear()
    }
}

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions