### Install tree-sitter-vue and tree-sitter
Source: https://github.com/c-gamble/tree-sitter-vue/blob/master/README.md
Installs the necessary packages for using the tree-sitter-vue grammar. This includes the Vue grammar itself and the core tree-sitter library.
```sh
npm install tree-sitter-vue tree-sitter
```
--------------------------------
### Parse Vue Code Example
Source: https://github.com/c-gamble/tree-sitter-vue/blob/master/docs/index.html
This JavaScript code snippet demonstrates how to set the content of the code and query editors in the Tree-sitter Vue Playground. It initializes the playground with a sample Vue.js template, script, and style block. Dependencies include the CodeMirror library for the editors.
```javascript
((codeExample, queryExample) => {
const handle = setInterval(() => {
const $codeEditor = document.querySelector('.CodeMirror');
const $queryEditor = document.querySelector('#query-container .CodeMirror');
const $queryCheckbox = document.querySelector('#query-checkbox');
if ($codeEditor && $queryEditor) {
$codeEditor.CodeMirror.setValue(codeExample);
if (queryExample) {
$queryEditor.CodeMirror.setValue(queryExample);
if (!$queryCheckbox.checked) $queryCheckbox.click();
}
clearInterval(handle);
}
}, 500);
})(` Hello, {{ name }}!
`;
const vueTree = vueParser.parse(component);
// Extract script blocks for secondary parsing
const extractScriptContent = (node) => {
if (node.type === 'script_element') {
const rawText = node.children.find(c => c.type === 'raw_text');
const startTag = node.children.find(c => c.type === 'start_tag');
const langAttr = startTag?.children.find(c => c.type === 'script_lang');
return {
content: rawText?.text || '',
language: langAttr?.text.includes('ts') ? 'typescript' : 'javascript',
startByte: rawText?.startIndex || 0
};
}
for (const child of node.children) {
const result = extractScriptContent(child);
if (result) return result;
}
return null;
};
const scriptBlock = extractScriptContent(vueTree.rootNode);
if (scriptBlock && scriptBlock.language === 'typescript') {
const tsParser = new Parser();
tsParser.setLanguage(TypeScript);
const tsTree = tsParser.parse(scriptBlock.content);
console.log('TypeScript AST:', tsTree.rootNode.toString().substring(0, 200));
}
// Similar approach for style blocks with CSS/SCSS parsers
console.log('Injection pattern enabled multi-language parsing');
```
--------------------------------
### Parse Multi-Block Vue Components with tree-sitter-vue
Source: https://context7.com/c-gamble/tree-sitter-vue/llms.txt
Parses complete Vue single-file components, extracting template, script, and style blocks along with their language attributes. It utilizes the tree-sitter-vue parser and requires the 'tree-sitter' and 'tree-sitter-vue' npm packages. The output is an array of objects, each detailing a block's type, tag name, language, and line numbers.
```javascript
const Parser = require("tree-sitter");
const Vue = require("tree-sitter-vue");
const parser = new Parser();
parser.setLanguage(Vue);
const fullComponent = `
`;
const tree = parser.parse(fullComponent);
// Extract block types
const extractBlocks = (node) => {
const blocks = [];
if (['template_element', 'script_element', 'style_element'].includes(node.type)) {
const startTag = node.children.find(c => c.type === 'start_tag');
const tagName = startTag?.children.find(c => c.type === 'tag_name')?.text;
const lang = startTag?.children.find(c =>
c.type === 'script_lang' || c.type === 'style_lang'
)?.text;
blocks.push({
blockType: node.type,
tagName: tagName,
language: lang || 'default',
startLine: node.startPosition.row,
endLine: node.endPosition.row
});
}
node.children.forEach(child => blocks.push(...extractBlocks(child)));
return blocks;
};
console.log('Component blocks:', extractBlocks(tree.rootNode));
```
--------------------------------
### Parse Built-in Vue Components with tree-sitter-vue
Source: https://context7.com/c-gamble/tree-sitter-vue/llms.txt
Parses Vue's built-in components such as Suspense and dynamic component syntax, generating proper AST nodes. This functionality relies on the tree-sitter-vue parser and requires 'tree-sitter' and 'tree-sitter-vue'. The output identifies these components and extracts relevant attributes like 'props' for Suspense and the 'is' attribute for dynamic components.
```javascript
const Parser = require("tree-sitter");
const Vue = require("tree-sitter-vue");
const parser = new Parser();
parser.setLanguage(Vue);
const builtInComponents = `
Main content
Dynamic content
`;
const tree = parser.parse(builtInComponents);
// Find built-in components
const findBuiltInComponents = (node) => {
const components = [];
if (node.type === 'suspense') {
const props = node.children.filter(c => c.type === 'props' || c.type === 'attribute');
components.push({
component: 'Suspense',
props: props.map(p => p.text),
line: node.startPosition.row
});
}
if (node.type === 'vue_component') {
const isAttr = node.children.find(c =>
c.type === 'quoted_attribute_value' || c.type === 'attribute_value'
);
components.push({
component: 'component',
is: isAttr?.text,
line: node.startPosition.row
});
}
node.children.forEach(child => components.push(...findBuiltInComponents(child)));
return components;
};
console.log('Built-in components:', findBuiltInComponents(tree.rootNode));
```
--------------------------------
### Parse CSS Style Block in Vue SFC
Source: https://github.com/c-gamble/tree-sitter-vue/blob/master/corpus/style.txt
Illustrates parsing a standard CSS style block within a Vue SFC. The parser recognizes the CSS language, the style content including directives like @include, and the surrounding tags.
```tree-sitter-vue
(component
(style_element
(start_tag
(tag_name)
(style_lang
(css_val)))
(raw_text)
(end_tag
(tag_name))
))
```
```vue
```
--------------------------------
### Vue.js Directives: Modifiers
Source: https://github.com/c-gamble/tree-sitter-vue/blob/master/corpus/spec.txt
This snippet showcases the grammar's recognition of Vue.js directives with modifiers, such as v-on:submit.prevent. It details how the directive name, argument, and modifiers are structured.
```tree-sitter grammar
(component
(template_element
(start_tag
(tag_name))
(text)
(element
(start_tag
(tag_name)
(directive_attribute
(directive_name)
(directive_argument)
(directive_modifiers
(directive_modifier))
(quoted_attribute_value
(attribute_value))))
(text)
(end_tag
(tag_name)))
```
--------------------------------
### TypeScript Script Element in Vue Component
Source: https://github.com/c-gamble/tree-sitter-vue/blob/master/corpus/script.txt
This snippet demonstrates a basic TypeScript script block within a Vue.js component. It declares a number variable 'ax' and a function 'x' that logs the variable. This is a common pattern for component logic in Vue.
```vue
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.