如何使用 typescript 中的 node-tree-sitter 模块?
How do I use the node-tree-sitter module from typescript?
通过查看拉取请求和问题,我发现 node-tree-sitter 模块中有打字稿定义(可能当前已过时);我如何从 typescript 访问这些定义,什么相当于以下 node-tree-sitter 示例 javascript typescript 中的代码?
const Parser = require('tree-sitter');
const JavaScript = require('tree-sitter-javascript');
const parser = new Parser();
parser.setLanguage(JavaScript);
const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
tree-sitter
模块没有单独的 @types/tree-sitter
模块(例如 some other modules) but instead bundles a tree-sitter.d.ts
类型定义文件和 tree-sitter
模块本身。您可以在您的 node_modules/tree-sitter
目录。当您导入 tree-sitter
时,TypeScript 会自动找到此文件。因此您可以在 TypeScript 中重写示例 JavaScript 代码,如下所示:
import Parser = require('tree-sitter');
import JavaScript = require('tree-sitter-javascript');
const parser: Parser = new Parser();
parser.setLanguage(JavaScript);
const sourceCode: string = 'let x = 1; console.log(x);';
const tree: Parser.Tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
通过查看拉取请求和问题,我发现 node-tree-sitter 模块中有打字稿定义(可能当前已过时);我如何从 typescript 访问这些定义,什么相当于以下 node-tree-sitter 示例 javascript typescript 中的代码?
const Parser = require('tree-sitter');
const JavaScript = require('tree-sitter-javascript');
const parser = new Parser();
parser.setLanguage(JavaScript);
const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
tree-sitter
模块没有单独的 @types/tree-sitter
模块(例如 some other modules) but instead bundles a tree-sitter.d.ts
类型定义文件和 tree-sitter
模块本身。您可以在您的 node_modules/tree-sitter
目录。当您导入 tree-sitter
时,TypeScript 会自动找到此文件。因此您可以在 TypeScript 中重写示例 JavaScript 代码,如下所示:
import Parser = require('tree-sitter');
import JavaScript = require('tree-sitter-javascript');
const parser: Parser = new Parser();
parser.setLanguage(JavaScript);
const sourceCode: string = 'let x = 1; console.log(x);';
const tree: Parser.Tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());