{"name":"web-tree-sitter","version":"0.24.6","description":"Tree-sitter bindings for the web","main":"tree-sitter.js","types":"tree-sitter-web.d.ts","directories":{"test":"test"},"scripts":{"test":"mocha","prepack":"cp ../../LICENSE .","prepublishOnly":"node check-artifacts-fresh.js"},"repository":{"type":"git","url":"git+https://github.com/tree-sitter/tree-sitter.git"},"keywords":["incremental","parsing"],"author":{"name":"Max Brunsfeld"},"license":"MIT","bugs":{"url":"https://github.com/tree-sitter/tree-sitter/issues"},"homepage":"https://github.com/tree-sitter/tree-sitter/tree/master/lib/binding_web","devDependencies":{"@types/emscripten":"^1.39.10","chai":"^4.3.7","eslint":">=8.56.0","eslint-config-google":"^0.14.0","mocha":"^10.2.0"},"_id":"web-tree-sitter@0.24.6","gitHead":"21a517c423010811147b0b1aa1e7aedc39ce91aa","_nodeVersion":"20.18.1","_npmVersion":"10.8.2","dist":{"integrity":"sha512-NVG40wwFerkvOSsrY2NE56nKpzwCpn8VT51gqZZaIfkBCMu+0JZLFVwJlZLPO01Ewfq3+Xg+NY4Bot5GD0L03g==","shasum":"05d15f15ca506a2615b7a97b3abc5183923812f2","tarball":"http://registry-npm.ywwl.com/web-tree-sitter/download/web-tree-sitter-0.24.6.tgz","fileCount":6,"unpackedSize":371469,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAexpQs9OR0ZUtfaC37N1RQlP0ACjoL3WQblRLY8pZmaAiBfQojcumHxzwsDQBaoX400n5RK2JsUKjqP32dulEKT/Q=="}],"size":115340,"noattachment":false,"key":"/web-tree-sitter/-/web-tree-sitter-0.24.6.tgz"},"_npmUser":{"name":"maxbrunsfeld","email":"maxbrunsfeld@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/web-tree-sitter_0.24.6_1735271170425_0.2058957520507707"},"_hasShrinkwrap":false,"readme":"# Web Tree-sitter\n\n[![npmjs.com badge]][npmjs.com]\n\n[npmjs.com]: https://www.npmjs.org/package/web-tree-sitter\n[npmjs.com badge]: https://img.shields.io/npm/v/web-tree-sitter.svg?color=%23BF4A4A\n\nWebAssembly bindings to the [Tree-sitter](https://github.com/tree-sitter/tree-sitter) parsing library.\n\n### Setup\n\nYou can download the `tree-sitter.js` and `tree-sitter.wasm` files from [the latest GitHub release](https://github.com/tree-sitter/tree-sitter/releases/latest) and load them using a standalone script:\n\n```html\n<script src=\"/the/path/to/tree-sitter.js\"></script>\n\n<script>\n  const Parser = window.TreeSitter;\n  Parser.init().then(() => { /* the library is ready */ });\n</script>\n```\n\nYou can also install [the `web-tree-sitter` module](https://www.npmjs.com/package/web-tree-sitter) from NPM and load it using a system like Webpack:\n\n```js\nconst Parser = require('web-tree-sitter');\nParser.init().then(() => { /* the library is ready */ });\n```\n\nYou can use this module with [deno](https://deno.land/):\n\n```js\nimport Parser from \"npm:web-tree-sitter\";\nawait Parser.init();\n// the library is ready\n```\n\n### Basic Usage\n\nFirst, create a parser:\n\n```js\nconst parser = new Parser;\n```\n\nThen assign a language to the parser. Tree-sitter languages are packaged as individual `.wasm` files (more on this below):\n\n```js\nconst JavaScript = await Parser.Language.load('/path/to/tree-sitter-javascript.wasm');\nparser.setLanguage(JavaScript);\n```\n\nNow you can parse source code:\n\n```js\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// { type: 'call_expression',\n//   startPosition: {row: 0, column: 16},\n//   endPosition: {row: 0, column: 30},\n//   startIndex: 0,\n//   endIndex: 30 }\n```\n\n### Editing\n\nIf your source code *changes*, you can update the syntax tree. This will take less time than the first parse.\n\n```javascript\n// Replace 'let' with 'const'\nconst newSourceCode = 'const x = 1; console.log(x);';\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, you can parse it by supplying a 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) => {\n  let line = sourceLines[position.row];\n  if (line) return line.slice(position.column);\n});\n```\n\n### Generate .wasm language files\n\nThe following example shows how to generate `.wasm` file for tree-sitter JavaScript grammar.\n\n**IMPORTANT**: [emscripten](https://emscripten.org/docs/getting_started/downloads.html), [docker](https://www.docker.com/), or [podman](https://podman.io) need to be installed.\n\nFirst install `tree-sitter-cli` and the tree-sitter language for which to generate `.wasm` (`tree-sitter-javascript` in this example):\n\n```sh\nnpm install --save-dev tree-sitter-cli tree-sitter-javascript\n```\n\nThen just use tree-sitter cli tool to generate the `.wasm`.\n\n```sh\nnpx tree-sitter build --wasm node_modules/tree-sitter-javascript\n```\n\nIf everything is fine, file `tree-sitter-javascript.wasm` should be generated in current directory.\n\n#### Running .wasm in Node.js\n\nNotice that executing `.wasm` files in node.js is considerably slower than running [node.js bindings](https://github.com/tree-sitter/node-tree-sitter). However could be useful for testing purposes:\n\n```javascript\nconst Parser = require('web-tree-sitter');\n\n(async () => {\n  await Parser.init();\n  const parser = new Parser();\n  const Lang = await Parser.Language.load('tree-sitter-javascript.wasm');\n  parser.setLanguage(Lang);\n  const tree = parser.parse('let x = 1;');\n  console.log(tree.rootNode.toString());\n})();\n```\n\n#### Running .wasm in browser\n\n`web-tree-sitter` can run in the browser, but there are some common pitfalls.\n\n##### Loading the .wasm file\n\n`web-tree-sitter` needs to load the `tree-sitter.wasm` file. By default, it assumes that this file is available in the\nsame path as the JavaScript code. Therefore, if the code is being served from `http://localhost:3000/bundle.js`, then\nthe wasm file should be at `http://localhost:3000/tree-sitter.wasm`.\n\nFor server side frameworks like NextJS, this can be tricky as pages are often served from a path such as\n`http://localhost:3000/_next/static/chunks/pages/index.js`. The loader will therefore look for the wasm file at\n`http://localhost:3000/_next/static/chunks/pages/tree-sitter.wasm`. The solution is to pass a `locateFile` function in\nthe `moduleOptions` argument to `Parser.init()`:\n\n```javascript\nawait Parser.init({\n  locateFile(scriptName: string, scriptDirectory: string) {\n    return scriptName;\n  },\n});\n```\n\n`locateFile` takes in two parameters, `scriptName`, i.e. the wasm file name, and `scriptDirectory`, i.e. the directory\nwhere the loader expects the script to be. It returns the path where the loader will look for the wasm file. In the NextJS\ncase, we want to return just the `scriptName` so that the loader will look at `http://localhost:3000/tree-sitter.wasm`\nand not `http://localhost:3000/_next/static/chunks/pages/tree-sitter.wasm`.\n\n##### `Can't resolve 'fs' in 'node_modules/web-tree-sitter'`\n\nMost bundlers will notice that the `tree-sitter.js` file is attempting to import `fs`, i.e. node's file system library.\nSince this doesn't exist in the browser, the bundlers will get confused. For webpack you can fix this by adding the\nfollowing to your webpack config:\n\n```javascript\n{\n  resolve: {\n    fallback: {\n      fs: false\n    }\n  }\n}\n```\n","publish_time":1735271170608,"maintainers":[{"name":"ahlinc","email":"ahlincq@gmail.com"},{"name":"maxbrunsfeld","email":"maxbrunsfeld@gmail.com"},{"name":"amaanq","email":"amaanq12@gmail.com"},{"name":"tclem","email":"timothy.clem@gmail.com"},{"name":"rewinfrey","email":"rick.winfrey@gmail.com"},{"name":"robrix","email":"rob.rix@github.com"},{"name":"patrickthomson","email":"patrickt@github.com"},{"name":"dcreager1847","email":"dcreager@dcreager.net"}],"_cnpm_publish_time":1735271170608,"dist-tags":{"latest":"0.27.2"}}