tree-sitter / node-tree-sitter

Node.js bindings for tree-sitter
https://www.npmjs.com/package/tree-sitter
MIT License
648 stars 113 forks source link

[question] Why use `new Query()` to create query instance, the TypeScript will get error? #219

Closed liaodalin19903 closed 2 months ago

liaodalin19903 commented 2 months ago

When use new Query() to create query instance, the TypeScript will get error:

1)JavaScript test success: https://github.com/liaodalin19903/Test-demos/tree/bug-003-nodetreesitter/nodejs-test-demos/nodejs-demo-02-nodetreesitter

new Query() execute no problem:

const query = new Query(
  JavaScript,
  `
    (class_declaration name: (identifier) @class-name)
  `
);

2)TypeScript test fail: https://github.com/liaodalin19903/Test-demos/tree/bug-003-nodetreesitter/nodejs-test-demos/nodejs-demo-03-nodetreesitter

new Query() execute error

const query = new Query(
  TypeScript,
  `
    (class_declaration name: (identifier) @class-name)
  `
);

Error:

/usr/local/bin/node ./nodejs-test-demos/nodejs-demo-03-nodetreesitter/test.js
Uncaught Error Error: Query error of type TSQueryErrorStructure at position 31
    at <anonymous> (/Users/user/Desktop/Test/Test-demos/nodejs-test-demos/nodejs-demo-03-nodetreesitter/test.js:9:15)
    at Module._compile (internal/modules/cjs/loader:1369:14)
    at Module._extensions..js (internal/modules/cjs/loader:1427:10)
    at Module.load (internal/modules/cjs/loader:1206:32)
    at Module._load (internal/modules/cjs/loader:1022:12)
    at executeUserEntryPoint (internal/modules/run_main:135:12)
    at <anonymous> (internal/main/run_main_module:28:49)
test.js:9
Process exited with code 1
amaanq commented 2 months ago

it's type_identifier not identifier, just look at a parse tree to see what you should query for.

image

liaodalin19903 commented 2 months ago

it's type_identifier not identifier, just look at a parse tree to see what you should query for.

image

@amaanq thanks for your reply, I still have several questions:

  1. What tool did you use to print the contents of the snapshot? Is it tree-sitter-cli?

  2. where is the type_identifier definition? I didn't find it in: node-tree-sitter nor define-grammar.js

amaanq commented 2 months ago
  1. Yes it's the cli but it's a fork with the -oc flag, I'll upstream that functionality soon enough.

  2. it's aliased, and it's in the JavaScript grammar (which TypeScript extends). node-tree-sitter is just the nodejs bindings for tree-sitter.