Tree-sitter Vue Grammar

0.2.1 · maintenance · verified Sun Apr 19

`tree-sitter-vue` provides a specialized grammar for parsing Vue.js template syntax, specifically targeting Vue v2.6.0. It leverages the `tree-sitter` parsing library to generate concrete syntax trees for the template sections within `.vue` files. As of version 0.2.1, its primary focus is on the Vue 2 template syntax itself, explicitly not handling embedded languages like JavaScript within `<script>` tags or CSS within `<style>` tags. Users needing multi-language parsing must integrate other `tree-sitter` grammars for those respective sections. The project appears to be in maintenance mode, with its last commit dating to September 2021, suggesting infrequent releases unless major changes occur in Vue 2 syntax or the `tree-sitter` core. It differentiates itself by offering a robust, structural parse of Vue 2 templates, which is crucial for advanced tooling like linters, formatters, and IDE features that rely on accurate syntax analysis.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Tree-sitter parser with the Vue grammar and parse a basic Vue 2 template string, then print its root node's S-expression representation.

const Parser = require("tree-sitter");
const Vue = require("tree-sitter-vue");

const parser = new Parser();
parser.setLanguage(Vue);

const sourceCode = `
<template>
  Hello, <a :[key]="url">{{ name }}</a>!
</template>
`;

const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
/* Expected output:
(component
  (template_element
    (start_tag
      (tag_name))
      (text)
      (element
        (start_tag
          (tag_name)
          (directive_attribute
            (directive_name)
            (directive_dynamic_argument
              (directive_dynamic_argument_value))
            (quoted_attribute_value
              (attribute_value))))
        (interpolation
          (raw_text))
        (end_tag
          (tag_name)))
      (text)
    (end_tag
      (tag_name))))
*/

view raw JSON →