Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

web-tree-sitter

Package Overview
Dependencies
Maintainers
0
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web-tree-sitter

Tree-sitter bindings for the web

  • 0.24.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
531K
increased by5.9%
Maintainers
0
Weekly downloads
 
Created

What is web-tree-sitter?

web-tree-sitter is a JavaScript library that provides a way to parse and analyze code using the Tree-sitter parsing library. It is designed to work in web environments and allows developers to build syntax trees for various programming languages, enabling tasks such as syntax highlighting, code navigation, and static analysis.

What are web-tree-sitter's main functionalities?

Initialize Parser

This code demonstrates how to initialize the Tree-sitter parser with a specific language (JavaScript in this case). It loads the language WASM file and sets it to the parser.

const Parser = require('web-tree-sitter');

async function initializeParser() {
  await Parser.init();
  const parser = new Parser();
  const Lang = await Parser.Language.load('tree-sitter-javascript.wasm');
  parser.setLanguage(Lang);
  return parser;
}

initializeParser().then(parser => console.log('Parser initialized'));

Parse Code

This code sample shows how to parse a string of JavaScript code into a syntax tree. The resulting tree can be used for further analysis or manipulation.

const Parser = require('web-tree-sitter');

async function parseCode(code) {
  await Parser.init();
  const parser = new Parser();
  const Lang = await Parser.Language.load('tree-sitter-javascript.wasm');
  parser.setLanguage(Lang);
  const tree = parser.parse(code);
  return tree;
}

parseCode('const x = 42;').then(tree => console.log(tree.rootNode.toString()));

Query Syntax Tree

This example demonstrates how to query a syntax tree using Tree-sitter's query language. It searches for function declarations and captures the function names.

const Parser = require('web-tree-sitter');

async function querySyntaxTree(code) {
  await Parser.init();
  const parser = new Parser();
  const Lang = await Parser.Language.load('tree-sitter-javascript.wasm');
  parser.setLanguage(Lang);
  const tree = parser.parse(code);
  const query = Lang.query('(function_declaration name: (identifier) @function-name)');
  const matches = query.matches(tree.rootNode);
  return matches;
}

querySyntaxTree('function foo() {}').then(matches => console.log(matches));

Other packages similar to web-tree-sitter

Keywords

FAQs

Package last updated on 11 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc