{"id":13433537,"url":"https://github.com/tree-sitter/node-tree-sitter","last_synced_at":"2025-05-13T19:06:52.374Z","repository":{"id":14764037,"uuid":"17485435","full_name":"tree-sitter/node-tree-sitter","owner":"tree-sitter","description":"Node.js bindings for tree-sitter","archived":false,"fork":false,"pushed_at":"2025-03-28T22:13:36.000Z","size":676,"stargazers_count":722,"open_issues_count":15,"forks_count":130,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-05-12T06:56:20.900Z","etag":null,"topics":["binding","javascript","nodejs","tree-sitter"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/tree-sitter","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tree-sitter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"tree-sitter","patreon":null,"open_collective":"tree-sitter","ko_fi":"amaanq","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"lfx_crowdfunding":null,"polar":null,"buy_me_a_coffee":null,"thanks_dev":null,"custom":null}},"created_at":"2014-03-06T17:04:37.000Z","updated_at":"2025-05-08T15:56:57.000Z","dependencies_parsed_at":"2024-03-28T22:34:40.595Z","dependency_job_id":"0f3b52cd-4784-42dc-a9cb-a77a7646f8f0","html_url":"https://github.com/tree-sitter/node-tree-sitter","commit_stats":{"total_commits":640,"total_committers":33,"mean_commits":"19.393939393939394","dds":"0.20156249999999998","last_synced_commit":"ff76451ef4267a8dbb4f3bbbba7f0ecc9216702d"},"previous_names":[],"tags_count":167,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tree-sitter%2Fnode-tree-sitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tree-sitter%2Fnode-tree-sitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tree-sitter%2Fnode-tree-sitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tree-sitter%2Fnode-tree-sitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tree-sitter","download_url":"https://codeload.github.com/tree-sitter/node-tree-sitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253692389,"owners_count":21948316,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binding","javascript","nodejs","tree-sitter"],"created_at":"2024-07-31T02:01:28.604Z","updated_at":"2025-05-13T19:06:52.322Z","avatar_url":"https://github.com/tree-sitter.png","language":"JavaScript","readme":"# Node Tree-sitter\n\n[![CI][ci]](https://github.com/tree-sitter/node-tree-sitter/actions/workflows/ci.yml)\n[![npm][npm]](https://npmjs.com/package/tree-sitter)\n[![docs][docs]](https://tree-sitter.github.io/node-tree-sitter)\n\nThis module provides Node.js bindings to the [tree-sitter] parsing library.\n\n## Installation\n\n```sh\nnpm install tree-sitter\n```\n\n## Basic Usage\n\n### Prerequisites\n\nFirst, you'll need a Tree-sitter grammar for the language you want to parse. There are many [existing grammars][grammars],\nsuch as [tree-sitter-javascript][javascript]. These grammars can typically be installed with a package manager like NPM,\nso long as the author has published them.\n\n```sh\nnpm install tree-sitter-javascript\n```\n\nYou can also develop a new grammar by using the [Tree-sitter CLI][cli] and following the [docs][ts docs].\n\n### Parsing Source Code\n\nOnce you've got your grammar, create a parser with that grammar.\n\n```javascript\nconst Parser = require('tree-sitter');\nconst JavaScript = require('tree-sitter-javascript');\n\nconst parser = new Parser();\nparser.setLanguage(JavaScript);\n```\n\nThen you can parse some source code,\n\n```javascript\nconst sourceCode = 'let x = 1; console.log(x);';\nconst tree = parser.parse(sourceCode);\n```\n\nand inspect the syntax tree.\n\n```javascript\nconsole.log(tree.rootNode.toString());\n\n// (program\n//   (lexical_declaration\n//     (variable_declarator (identifier) (number)))\n//   (expression_statement\n//     (call_expression\n//       (member_expression (identifier) (property_identifier))\n//       (arguments (identifier)))))\n\nconst callExpression = tree.rootNode.child(1).firstChild;\nconsole.log(callExpression);\n\n// {\n//   type: 'call_expression',\n//   startPosition: {row: 0, column: 16},\n//   endPosition: {row: 0, column: 30},\n//   startIndex: 0,\n//   endIndex: 30\n// }\n```\n\nIf your source code *changes*, you can update the syntax tree. This is much faster than the first parse.\n\n```javascript\n// In the code, we replaced 'let' with 'const'.\n// So, we set our old end index to 3, and our new end index to 5.\n// Note that the end index is exclusive.\nconst newSourceCode = 'const x = 1; console.log(x);';\n//                        ^ ^\n// indices:               3 5\n// points:            (0,3) (0,5)\n\ntree.edit({\n  startIndex: 0,\n  oldEndIndex: 3,\n  newEndIndex: 5,\n  startPosition: {row: 0, column: 0},\n  oldEndPosition: {row: 0, column: 3},\n  newEndPosition: {row: 0, column: 5},\n});\n\nconst newTree = parser.parse(newSourceCode, tree);\n```\n\n### Parsing Text From a Custom Data Structure\n\nIf your text is stored in a data structure other than a single string, such as a rope or array, you can parse it by supplying\na callback to `parse` instead of a string:\n\n```javascript\nconst sourceLines = [\n  'let x = 1;',\n  'console.log(x);'\n];\n\nconst tree = parser.parse((index, position) =\u003e {\n  let line = sourceLines[position.row];\n  if (line) {\n    return line.slice(position.column);\n  }\n});\n```\n\n### Further Reading\n\nIt's recommended that you read the [Tree-sitter documentation][usage docs] on using parsers to get a higher-level overview\nof the API. Once you're comfortable with the basics, you can explore the [full API documentation](https://tree-sitter.github.io/node-tree-sitter),\nwhich should map closely to the C API, though there are some differences.\n\n[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/node-tree-sitter/ci.yml?logo=github\u0026label=CI\n[cli]: https://github.com/tree-sitter/tree-sitter/tree/master/cli\n[docs]: https://img.shields.io/badge/docs-website-blue\n[npm]: https://img.shields.io/npm/v/tree-sitter?logo=npm\n[grammars]: https://github.com/tree-sitter/tree-sitter/wiki/List-of-parsers\n[javascript]: http://github.com/tree-sitter/tree-sitter-javascript\n[ts docs]: https://tree-sitter.github.io/tree-sitter/creating-parsers\n[usage docs]: https://tree-sitter.github.io/tree-sitter/using-parsers\n","funding_links":["https://github.com/sponsors/tree-sitter","https://opencollective.com/tree-sitter","https://ko-fi.com/amaanq"],"categories":["JavaScript","C++","Language bindings","nodejs"],"sub_categories":["Others"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftree-sitter%2Fnode-tree-sitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftree-sitter%2Fnode-tree-sitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftree-sitter%2Fnode-tree-sitter/lists"}