rbtr-lang-svelte 2026.9.0.dev1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rbtr_lang_svelte-2026.9.0.dev1/LICENSE +21 -0
- rbtr_lang_svelte-2026.9.0.dev1/PKG-INFO +78 -0
- rbtr_lang_svelte-2026.9.0.dev1/README.md +52 -0
- rbtr_lang_svelte-2026.9.0.dev1/pyproject.toml +67 -0
- rbtr_lang_svelte-2026.9.0.dev1/pyproject.toml.orig +64 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/__init__.py +1 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/injections.scm +41 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/plugin.py +121 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/py.typed +0 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/tests/__init__.py +0 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/tests/__snapshots__/test_samples/test_edges_match_snapshot.json +3 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/tests/__snapshots__/test_samples/test_extraction_matches_snapshot.json +173 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/tests/samples/svelte/Greeting.svelte +28 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/tests/samples/svelte/greeter.ts +5 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/tests/test_extraction.py +62 -0
- rbtr_lang_svelte-2026.9.0.dev1/src/rbtr_lang_svelte/tests/test_samples.py +86 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alejandro Giacometti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rbtr-lang-svelte
|
|
3
|
+
Version: 2026.9.0.dev1
|
|
4
|
+
Summary: rbtr — Svelte language plugin (and shared SFC machinery)
|
|
5
|
+
Keywords: code-search,code-index,tree-sitter,static-analysis,semantic-search,developer-tools,svelte
|
|
6
|
+
Author: Alejandro Giacometti
|
|
7
|
+
Author-email: Alejandro Giacometti <alejandro.giacometti@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
15
|
+
Classifier: Topic :: Text Processing :: Indexing
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Dist: rbtr==2026.9.0.dev1
|
|
18
|
+
Requires-Dist: tree-sitter-svelte
|
|
19
|
+
Requires-Python: >=3.13
|
|
20
|
+
Project-URL: Homepage, https://github.com/janrito/rbtr
|
|
21
|
+
Project-URL: Repository, https://github.com/janrito/rbtr
|
|
22
|
+
Project-URL: Documentation, https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-svelte#readme
|
|
23
|
+
Project-URL: Issues, https://github.com/janrito/rbtr/issues
|
|
24
|
+
Project-URL: Changelog, https://github.com/janrito/rbtr/releases
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# rbtr-lang-svelte
|
|
28
|
+
|
|
29
|
+
Svelte support for [rbtr]. Optional plugin — install with
|
|
30
|
+
`pip install rbtr[svelte]`. Also hosts the shared single-file-component
|
|
31
|
+
machinery that [rbtr-lang-vue] reuses.
|
|
32
|
+
|
|
33
|
+
[rbtr]: https://github.com/janrito/rbtr/tree/main/packages/rbtr#readme
|
|
34
|
+
[rbtr-lang-vue]: https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-vue#readme
|
|
35
|
+
|
|
36
|
+
## What it ingests
|
|
37
|
+
|
|
38
|
+
`.svelte` components. A component mixes several languages in one file, and
|
|
39
|
+
each part is indexed as itself:
|
|
40
|
+
|
|
41
|
+
- The **markup template** becomes one host **doc-section** chunk (named after
|
|
42
|
+
the file), so component markup is searchable.
|
|
43
|
+
- A **comment** above the markup is its own chunk, so the note explaining
|
|
44
|
+
what the component is for stays searchable.
|
|
45
|
+
- The `<script>` and `<style>` blocks are **delegated** to their embedded
|
|
46
|
+
language by injection (see below) — not re-parsed here.
|
|
47
|
+
|
|
48
|
+
## Chunks produced
|
|
49
|
+
|
|
50
|
+
```svelte
|
|
51
|
+
<script lang="ts">
|
|
52
|
+
import { store } from "./store"; // → import (typescript)
|
|
53
|
+
export let name = "world"; // → variable "name" (typescript)
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<h1 class="greeting">Hello {name}</h1> <!-- → doc_section "Greeting" (svelte host) -->
|
|
57
|
+
|
|
58
|
+
<style lang="scss">
|
|
59
|
+
.greeting { color: red; } // → class ".greeting" (scss)
|
|
60
|
+
</style>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Embedded / injected chunks
|
|
64
|
+
|
|
65
|
+
The `lang` attribute picks the dialect; a bare block uses the fallback:
|
|
66
|
+
|
|
67
|
+
- `<script lang="ts">` → **TypeScript**, bare `<script>` → **JavaScript**
|
|
68
|
+
- `<style lang="scss">` → **SCSS**, `lang="less"` → **Less**, bare `<style>`
|
|
69
|
+
→ **CSS**
|
|
70
|
+
|
|
71
|
+
These chunks carry their *embedded* language; the delegated extraction runs
|
|
72
|
+
only when the corresponding plugin (e.g. `rbtr-lang-javascript`,
|
|
73
|
+
`rbtr-lang-css`) is installed.
|
|
74
|
+
|
|
75
|
+
## Grammar & dependencies
|
|
76
|
+
|
|
77
|
+
Uses the `tree-sitter-svelte` grammar. Ships `chunk_sfc` and `injections.scm`,
|
|
78
|
+
which `rbtr-lang-vue` imports for `.vue` files (they share the SFC shape).
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# rbtr-lang-svelte
|
|
2
|
+
|
|
3
|
+
Svelte support for [rbtr]. Optional plugin — install with
|
|
4
|
+
`pip install rbtr[svelte]`. Also hosts the shared single-file-component
|
|
5
|
+
machinery that [rbtr-lang-vue] reuses.
|
|
6
|
+
|
|
7
|
+
[rbtr]: https://github.com/janrito/rbtr/tree/main/packages/rbtr#readme
|
|
8
|
+
[rbtr-lang-vue]: https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-vue#readme
|
|
9
|
+
|
|
10
|
+
## What it ingests
|
|
11
|
+
|
|
12
|
+
`.svelte` components. A component mixes several languages in one file, and
|
|
13
|
+
each part is indexed as itself:
|
|
14
|
+
|
|
15
|
+
- The **markup template** becomes one host **doc-section** chunk (named after
|
|
16
|
+
the file), so component markup is searchable.
|
|
17
|
+
- A **comment** above the markup is its own chunk, so the note explaining
|
|
18
|
+
what the component is for stays searchable.
|
|
19
|
+
- The `<script>` and `<style>` blocks are **delegated** to their embedded
|
|
20
|
+
language by injection (see below) — not re-parsed here.
|
|
21
|
+
|
|
22
|
+
## Chunks produced
|
|
23
|
+
|
|
24
|
+
```svelte
|
|
25
|
+
<script lang="ts">
|
|
26
|
+
import { store } from "./store"; // → import (typescript)
|
|
27
|
+
export let name = "world"; // → variable "name" (typescript)
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<h1 class="greeting">Hello {name}</h1> <!-- → doc_section "Greeting" (svelte host) -->
|
|
31
|
+
|
|
32
|
+
<style lang="scss">
|
|
33
|
+
.greeting { color: red; } // → class ".greeting" (scss)
|
|
34
|
+
</style>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Embedded / injected chunks
|
|
38
|
+
|
|
39
|
+
The `lang` attribute picks the dialect; a bare block uses the fallback:
|
|
40
|
+
|
|
41
|
+
- `<script lang="ts">` → **TypeScript**, bare `<script>` → **JavaScript**
|
|
42
|
+
- `<style lang="scss">` → **SCSS**, `lang="less"` → **Less**, bare `<style>`
|
|
43
|
+
→ **CSS**
|
|
44
|
+
|
|
45
|
+
These chunks carry their *embedded* language; the delegated extraction runs
|
|
46
|
+
only when the corresponding plugin (e.g. `rbtr-lang-javascript`,
|
|
47
|
+
`rbtr-lang-css`) is installed.
|
|
48
|
+
|
|
49
|
+
## Grammar & dependencies
|
|
50
|
+
|
|
51
|
+
Uses the `tree-sitter-svelte` grammar. Ships `chunk_sfc` and `injections.scm`,
|
|
52
|
+
which `rbtr-lang-vue` imports for `.vue` files (they share the SFC shape).
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.26,<1"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[tool.uv.build-backend]
|
|
6
|
+
module-name = "rbtr_lang_svelte"
|
|
7
|
+
|
|
8
|
+
[tool.uv.sources.rbtr]
|
|
9
|
+
workspace = true
|
|
10
|
+
|
|
11
|
+
[tool.uv.sources.rbtr-lang-javascript]
|
|
12
|
+
workspace = true
|
|
13
|
+
|
|
14
|
+
[tool.uv.sources.rbtr-lang-scss]
|
|
15
|
+
workspace = true
|
|
16
|
+
|
|
17
|
+
[project]
|
|
18
|
+
name = "rbtr-lang-svelte"
|
|
19
|
+
version = "2026.9.0-dev1"
|
|
20
|
+
description = "rbtr — Svelte language plugin (and shared SFC machinery)"
|
|
21
|
+
readme = "README.md"
|
|
22
|
+
license = "MIT"
|
|
23
|
+
license-files = ["LICENSE"]
|
|
24
|
+
keywords = [
|
|
25
|
+
"code-search",
|
|
26
|
+
"code-index",
|
|
27
|
+
"tree-sitter",
|
|
28
|
+
"static-analysis",
|
|
29
|
+
"semantic-search",
|
|
30
|
+
"developer-tools",
|
|
31
|
+
"svelte",
|
|
32
|
+
]
|
|
33
|
+
classifiers = [
|
|
34
|
+
"Development Status :: 4 - Beta",
|
|
35
|
+
"Intended Audience :: Developers",
|
|
36
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
37
|
+
"Programming Language :: Python :: 3.13",
|
|
38
|
+
"Topic :: Software Development :: Libraries",
|
|
39
|
+
"Topic :: Text Processing :: Indexing",
|
|
40
|
+
"Typing :: Typed",
|
|
41
|
+
]
|
|
42
|
+
requires-python = ">=3.13"
|
|
43
|
+
dependencies = [
|
|
44
|
+
"rbtr==2026.9.0-dev1",
|
|
45
|
+
"tree-sitter-svelte",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[[project.authors]]
|
|
49
|
+
name = "Alejandro Giacometti"
|
|
50
|
+
email = "alejandro.giacometti@gmail.com"
|
|
51
|
+
|
|
52
|
+
[project.urls]
|
|
53
|
+
Homepage = "https://github.com/janrito/rbtr"
|
|
54
|
+
Repository = "https://github.com/janrito/rbtr"
|
|
55
|
+
Documentation = "https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-svelte#readme"
|
|
56
|
+
Issues = "https://github.com/janrito/rbtr/issues"
|
|
57
|
+
Changelog = "https://github.com/janrito/rbtr/releases"
|
|
58
|
+
|
|
59
|
+
[project.entry-points."rbtr.languages"]
|
|
60
|
+
svelte = "rbtr_lang_svelte.plugin:svelte"
|
|
61
|
+
|
|
62
|
+
[dependency-groups]
|
|
63
|
+
dev = [
|
|
64
|
+
"rbtr[test]",
|
|
65
|
+
"rbtr-lang-javascript",
|
|
66
|
+
"rbtr-lang-scss",
|
|
67
|
+
]
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.26,<1"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[tool.uv.build-backend]
|
|
6
|
+
module-name = "rbtr_lang_svelte"
|
|
7
|
+
# No source-exclude: the plugin's tests, samples, and snapshots ship with
|
|
8
|
+
# the wheel (they are the language's golden record), and injections.scm ships
|
|
9
|
+
# as package data (reused by rbtr-lang-vue).
|
|
10
|
+
|
|
11
|
+
[project]
|
|
12
|
+
name = "rbtr-lang-svelte"
|
|
13
|
+
version = "2026.9.0-dev1"
|
|
14
|
+
description = "rbtr — Svelte language plugin (and shared SFC machinery)"
|
|
15
|
+
readme = "README.md"
|
|
16
|
+
license = "MIT"
|
|
17
|
+
license-files = ["LICENSE"]
|
|
18
|
+
authors = [
|
|
19
|
+
{ name = "Alejandro Giacometti", email = "alejandro.giacometti@gmail.com" },
|
|
20
|
+
]
|
|
21
|
+
keywords = [
|
|
22
|
+
"code-search",
|
|
23
|
+
"code-index",
|
|
24
|
+
"tree-sitter",
|
|
25
|
+
"static-analysis",
|
|
26
|
+
"semantic-search",
|
|
27
|
+
"developer-tools",
|
|
28
|
+
"svelte",
|
|
29
|
+
]
|
|
30
|
+
classifiers = [
|
|
31
|
+
"Development Status :: 4 - Beta",
|
|
32
|
+
"Intended Audience :: Developers",
|
|
33
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
34
|
+
"Programming Language :: Python :: 3.13",
|
|
35
|
+
"Topic :: Software Development :: Libraries",
|
|
36
|
+
"Topic :: Text Processing :: Indexing",
|
|
37
|
+
"Typing :: Typed",
|
|
38
|
+
]
|
|
39
|
+
requires-python = ">=3.13"
|
|
40
|
+
dependencies = [
|
|
41
|
+
"rbtr==2026.9.0-dev1",
|
|
42
|
+
"tree-sitter-svelte",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://github.com/janrito/rbtr"
|
|
47
|
+
Repository = "https://github.com/janrito/rbtr"
|
|
48
|
+
Documentation = "https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-svelte#readme"
|
|
49
|
+
Issues = "https://github.com/janrito/rbtr/issues"
|
|
50
|
+
Changelog = "https://github.com/janrito/rbtr/releases"
|
|
51
|
+
|
|
52
|
+
[project.entry-points."rbtr.languages"]
|
|
53
|
+
# The value resolves to the language's `LanguageRegistration` (named by its
|
|
54
|
+
# id) — see the rbtr ARCHITECTURE "External plugins" note.
|
|
55
|
+
svelte = "rbtr_lang_svelte.plugin:svelte"
|
|
56
|
+
|
|
57
|
+
[tool.uv.sources]
|
|
58
|
+
rbtr = { workspace = true }
|
|
59
|
+
rbtr-lang-javascript = { workspace = true }
|
|
60
|
+
rbtr-lang-scss = { workspace = true }
|
|
61
|
+
|
|
62
|
+
[dependency-groups]
|
|
63
|
+
# The sample embeds lang="ts" and lang="scss" blocks (scss pulls css).
|
|
64
|
+
dev = ["rbtr[test]", "rbtr-lang-javascript", "rbtr-lang-scss"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Single-file component (Svelte, Vue) language plugin package."""
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
; <script>/<style> blocks delegate to JS/TS/CSS/SCSS/Less. A `lang="…"`
|
|
2
|
+
; attribute selects the dialect (priority 1); the bare block is the
|
|
3
|
+
; fallback (priority 0).
|
|
4
|
+
|
|
5
|
+
((script_element
|
|
6
|
+
(start_tag
|
|
7
|
+
(attribute (attribute_name) @_attr
|
|
8
|
+
(quoted_attribute_value (attribute_value) @_lang)))
|
|
9
|
+
(raw_text) @injection.content)
|
|
10
|
+
(#eq? @_attr "lang")
|
|
11
|
+
(#any-of? @_lang "ts" "typescript")
|
|
12
|
+
(#set! injection.language "typescript")
|
|
13
|
+
(#set! injection.priority "1"))
|
|
14
|
+
|
|
15
|
+
((script_element (raw_text) @injection.content)
|
|
16
|
+
(#set! injection.language "javascript")
|
|
17
|
+
(#set! injection.priority "0"))
|
|
18
|
+
|
|
19
|
+
((style_element
|
|
20
|
+
(start_tag
|
|
21
|
+
(attribute (attribute_name) @_attr
|
|
22
|
+
(quoted_attribute_value (attribute_value) @_lang)))
|
|
23
|
+
(raw_text) @injection.content)
|
|
24
|
+
(#eq? @_attr "lang")
|
|
25
|
+
(#any-of? @_lang "scss" "sass")
|
|
26
|
+
(#set! injection.language "scss")
|
|
27
|
+
(#set! injection.priority "1"))
|
|
28
|
+
|
|
29
|
+
((style_element
|
|
30
|
+
(start_tag
|
|
31
|
+
(attribute (attribute_name) @_attr
|
|
32
|
+
(quoted_attribute_value (attribute_value) @_lang)))
|
|
33
|
+
(raw_text) @injection.content)
|
|
34
|
+
(#eq? @_attr "lang")
|
|
35
|
+
(#any-of? @_lang "less")
|
|
36
|
+
(#set! injection.language "less")
|
|
37
|
+
(#set! injection.priority "1"))
|
|
38
|
+
|
|
39
|
+
((style_element (raw_text) @injection.content)
|
|
40
|
+
(#set! injection.language "css")
|
|
41
|
+
(#set! injection.priority "0"))
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Svelte plugin, plus the single-file-component (SFC) machinery shared with Vue.
|
|
2
|
+
|
|
3
|
+
A Svelte (or Vue) component embeds code in several languages in one
|
|
4
|
+
file: a `<script>` block (JavaScript/TypeScript), a `<style>`
|
|
5
|
+
block (CSS/SCSS/Less), and a markup template. The `<script>` and
|
|
6
|
+
`<style>` blocks are delegated to their embedded language by the
|
|
7
|
+
engine's injection mechanism (`injection_query`); the chunker emits
|
|
8
|
+
only the markup template, as a host chunk so component markup is
|
|
9
|
+
searchable.
|
|
10
|
+
|
|
11
|
+
The template chunk's `language` is left blank for the orchestrator
|
|
12
|
+
to fill with the host language (svelte/vue); delegated chunks carry
|
|
13
|
+
their embedded language, set by the target's extractor.
|
|
14
|
+
|
|
15
|
+
`chunk_sfc` and `injections.scm` are shared: the `rbtr-lang-vue`
|
|
16
|
+
package reuses both for `.vue` files, which expose the same
|
|
17
|
+
`<script>`/`<style>` shape.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from pathlib import PurePosixPath
|
|
23
|
+
from typing import TYPE_CHECKING
|
|
24
|
+
|
|
25
|
+
from tree_sitter import Parser
|
|
26
|
+
|
|
27
|
+
from rbtr.domain.models import Chunk, ChunkKind
|
|
28
|
+
from rbtr.languages.chunks import last_line
|
|
29
|
+
from rbtr.languages.registration import LanguageRegistration, load_query
|
|
30
|
+
|
|
31
|
+
if TYPE_CHECKING:
|
|
32
|
+
from collections.abc import Iterator
|
|
33
|
+
|
|
34
|
+
from tree_sitter import Language, Node, Range
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _template_chunk(
|
|
38
|
+
file_path: str, blob_sha: str, content_bytes: bytes, nodes: list[Node]
|
|
39
|
+
) -> Chunk | None:
|
|
40
|
+
"""Emit the markup template as one host-language chunk, or `None`.
|
|
41
|
+
|
|
42
|
+
Returns `None` when the component has no template markup (a script-only
|
|
43
|
+
component); the orchestrator records the host language for dedup in that
|
|
44
|
+
case. `language` is left blank for the orchestrator to fill (svelte/vue);
|
|
45
|
+
spans the markup nodes.
|
|
46
|
+
"""
|
|
47
|
+
if not nodes:
|
|
48
|
+
return None
|
|
49
|
+
start = min(n.start_byte for n in nodes)
|
|
50
|
+
end = max(n.end_byte for n in nodes)
|
|
51
|
+
text = content_bytes[start:end].decode("utf-8", errors="replace").strip()
|
|
52
|
+
if not text:
|
|
53
|
+
return None
|
|
54
|
+
name = PurePosixPath(file_path).stem
|
|
55
|
+
line_start = min(n.start_point[0] for n in nodes)
|
|
56
|
+
line_end = max(last_line(n) for n in nodes)
|
|
57
|
+
return Chunk(
|
|
58
|
+
blob_sha=blob_sha,
|
|
59
|
+
file_path=file_path,
|
|
60
|
+
kind=ChunkKind.DOC_SECTION,
|
|
61
|
+
name=name,
|
|
62
|
+
scope="",
|
|
63
|
+
content=text,
|
|
64
|
+
line_start=line_start + 1,
|
|
65
|
+
line_end=line_end,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def chunk_sfc(
|
|
70
|
+
file_path: str,
|
|
71
|
+
blob_sha: str,
|
|
72
|
+
content: str,
|
|
73
|
+
grammar: Language,
|
|
74
|
+
ranges: list[Range] | None = None,
|
|
75
|
+
) -> Iterator[Chunk]:
|
|
76
|
+
"""Emit the SFC's markup template, and any comment above it.
|
|
77
|
+
|
|
78
|
+
The `<script>`/`<style>` blocks are handled by the engine's injection
|
|
79
|
+
mechanism (`_SFC_INJECTIONS`), not here. A top-level comment is not
|
|
80
|
+
markup, so it does not belong to the template chunk; it is content
|
|
81
|
+
someone searches for, so it gets a chunk of its own.
|
|
82
|
+
"""
|
|
83
|
+
content_bytes = content.encode("utf-8")
|
|
84
|
+
parser = Parser(grammar)
|
|
85
|
+
if ranges is not None:
|
|
86
|
+
parser.included_ranges = ranges
|
|
87
|
+
tree = parser.parse(content_bytes)
|
|
88
|
+
template_nodes = [
|
|
89
|
+
el
|
|
90
|
+
for el in tree.root_node.children
|
|
91
|
+
if el.type not in ("script_element", "style_element", "comment")
|
|
92
|
+
]
|
|
93
|
+
template = _template_chunk(file_path, blob_sha, content_bytes, template_nodes)
|
|
94
|
+
if template is not None:
|
|
95
|
+
yield template
|
|
96
|
+
|
|
97
|
+
for node in tree.root_node.children:
|
|
98
|
+
if node.type != "comment":
|
|
99
|
+
continue
|
|
100
|
+
text = content_bytes[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
|
|
101
|
+
yield Chunk(
|
|
102
|
+
blob_sha=blob_sha,
|
|
103
|
+
file_path=file_path,
|
|
104
|
+
kind=ChunkKind.COMMENT,
|
|
105
|
+
name="",
|
|
106
|
+
scope="",
|
|
107
|
+
content=text.strip(),
|
|
108
|
+
line_start=node.start_point[0] + 1,
|
|
109
|
+
line_end=last_line(node),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
svelte = LanguageRegistration(
|
|
114
|
+
id="svelte",
|
|
115
|
+
extensions=frozenset({".svelte"}),
|
|
116
|
+
grammar_module="tree_sitter_svelte",
|
|
117
|
+
injection_query=load_query(__package__, "injections"),
|
|
118
|
+
extraction_serial=3,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
svelte.chunker(chunk_sfc)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"blob_sha": "sha1",
|
|
4
|
+
"file_path": "Greeting.svelte",
|
|
5
|
+
"kind": "doc_section",
|
|
6
|
+
"name": "Greeting",
|
|
7
|
+
"scope": "",
|
|
8
|
+
"language": "svelte",
|
|
9
|
+
"file_language": "svelte",
|
|
10
|
+
"content": "<h1 class=\"greeting\">{shout()}</h1>",
|
|
11
|
+
"line_start": 28,
|
|
12
|
+
"line_end": 28,
|
|
13
|
+
"metadata": {
|
|
14
|
+
"module": "",
|
|
15
|
+
"names": "",
|
|
16
|
+
"dots": "",
|
|
17
|
+
"language_hint": ""
|
|
18
|
+
},
|
|
19
|
+
"id": "d9707b44a8167bb9"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"blob_sha": "sha1",
|
|
23
|
+
"file_path": "Greeting.svelte",
|
|
24
|
+
"kind": "comment",
|
|
25
|
+
"name": "",
|
|
26
|
+
"scope": "",
|
|
27
|
+
"language": "svelte",
|
|
28
|
+
"file_language": "svelte",
|
|
29
|
+
"content": "<!--\n Svelte single-file component.\n\n The SFC plugin delegates the <script> block to TypeScript and the\n <style> block to SCSS; their symbols carry those languages and their\n imports resolve through the matching resolver. The markup template is\n not extracted.\n-->",
|
|
30
|
+
"line_start": 1,
|
|
31
|
+
"line_end": 8,
|
|
32
|
+
"metadata": {
|
|
33
|
+
"module": "",
|
|
34
|
+
"names": "",
|
|
35
|
+
"dots": "",
|
|
36
|
+
"language_hint": ""
|
|
37
|
+
},
|
|
38
|
+
"id": "5f2bbd1d04760dc6"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"blob_sha": "sha1",
|
|
42
|
+
"file_path": "Greeting.svelte",
|
|
43
|
+
"kind": "import",
|
|
44
|
+
"name": "import { greet } from \"./greeter\";",
|
|
45
|
+
"scope": "",
|
|
46
|
+
"language": "typescript",
|
|
47
|
+
"file_language": "svelte",
|
|
48
|
+
"content": "import { greet } from \"./greeter\";",
|
|
49
|
+
"line_start": 10,
|
|
50
|
+
"line_end": 10,
|
|
51
|
+
"metadata": {
|
|
52
|
+
"module": "greeter",
|
|
53
|
+
"names": "greet",
|
|
54
|
+
"dots": "1",
|
|
55
|
+
"language_hint": ""
|
|
56
|
+
},
|
|
57
|
+
"id": "9e08409ab07d56f2"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"blob_sha": "sha1",
|
|
61
|
+
"file_path": "Greeting.svelte",
|
|
62
|
+
"kind": "variable",
|
|
63
|
+
"name": "name",
|
|
64
|
+
"scope": "",
|
|
65
|
+
"language": "typescript",
|
|
66
|
+
"file_language": "svelte",
|
|
67
|
+
"content": "name: string = \"world\"",
|
|
68
|
+
"line_start": 12,
|
|
69
|
+
"line_end": 12,
|
|
70
|
+
"metadata": {
|
|
71
|
+
"module": "",
|
|
72
|
+
"names": "",
|
|
73
|
+
"dots": "",
|
|
74
|
+
"language_hint": ""
|
|
75
|
+
},
|
|
76
|
+
"id": "030615b8d1bfaaf0"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"blob_sha": "sha1",
|
|
80
|
+
"file_path": "Greeting.svelte",
|
|
81
|
+
"kind": "function",
|
|
82
|
+
"name": "shout",
|
|
83
|
+
"scope": "",
|
|
84
|
+
"language": "typescript",
|
|
85
|
+
"file_language": "svelte",
|
|
86
|
+
"content": "function shout(): string {\n return greet(name).toUpperCase();\n }",
|
|
87
|
+
"line_start": 14,
|
|
88
|
+
"line_end": 16,
|
|
89
|
+
"metadata": {
|
|
90
|
+
"module": "",
|
|
91
|
+
"names": "",
|
|
92
|
+
"dots": "",
|
|
93
|
+
"language_hint": ""
|
|
94
|
+
},
|
|
95
|
+
"id": "ca91fc7dc3b14adc"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"blob_sha": "sha1",
|
|
99
|
+
"file_path": "Greeting.svelte",
|
|
100
|
+
"kind": "variable",
|
|
101
|
+
"name": "$accent",
|
|
102
|
+
"scope": "",
|
|
103
|
+
"language": "scss",
|
|
104
|
+
"file_language": "svelte",
|
|
105
|
+
"content": "$accent: #00aa77;",
|
|
106
|
+
"line_start": 20,
|
|
107
|
+
"line_end": 20,
|
|
108
|
+
"metadata": {
|
|
109
|
+
"module": "",
|
|
110
|
+
"names": "",
|
|
111
|
+
"dots": "",
|
|
112
|
+
"language_hint": ""
|
|
113
|
+
},
|
|
114
|
+
"id": "83127d65b9be4d63"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
"blob_sha": "sha1",
|
|
118
|
+
"file_path": "Greeting.svelte",
|
|
119
|
+
"kind": "class",
|
|
120
|
+
"name": ".greeting",
|
|
121
|
+
"scope": "",
|
|
122
|
+
"language": "scss",
|
|
123
|
+
"file_language": "svelte",
|
|
124
|
+
"content": ".greeting {\n color: $accent;\n font-weight: bold;\n }",
|
|
125
|
+
"line_start": 22,
|
|
126
|
+
"line_end": 25,
|
|
127
|
+
"metadata": {
|
|
128
|
+
"module": "",
|
|
129
|
+
"names": "",
|
|
130
|
+
"dots": "",
|
|
131
|
+
"language_hint": ""
|
|
132
|
+
},
|
|
133
|
+
"id": "d263e499d49e38f4"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"blob_sha": "sha1",
|
|
137
|
+
"file_path": "greeter.ts",
|
|
138
|
+
"kind": "comment",
|
|
139
|
+
"name": "",
|
|
140
|
+
"scope": "",
|
|
141
|
+
"language": "typescript",
|
|
142
|
+
"file_language": "typescript",
|
|
143
|
+
"content": "// Imported by Greeting.svelte's <script> block.",
|
|
144
|
+
"line_start": 1,
|
|
145
|
+
"line_end": 1,
|
|
146
|
+
"metadata": {
|
|
147
|
+
"module": "",
|
|
148
|
+
"names": "",
|
|
149
|
+
"dots": "",
|
|
150
|
+
"language_hint": ""
|
|
151
|
+
},
|
|
152
|
+
"id": "b0eec444d8e60c22"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
"blob_sha": "sha1",
|
|
156
|
+
"file_path": "greeter.ts",
|
|
157
|
+
"kind": "function",
|
|
158
|
+
"name": "greet",
|
|
159
|
+
"scope": "",
|
|
160
|
+
"language": "typescript",
|
|
161
|
+
"file_language": "typescript",
|
|
162
|
+
"content": "function greet(name: string): string {\n return `Hello, ${name}`;\n}",
|
|
163
|
+
"line_start": 3,
|
|
164
|
+
"line_end": 5,
|
|
165
|
+
"metadata": {
|
|
166
|
+
"module": "",
|
|
167
|
+
"names": "",
|
|
168
|
+
"dots": "",
|
|
169
|
+
"language_hint": ""
|
|
170
|
+
},
|
|
171
|
+
"id": "babbec3806fb9d96"
|
|
172
|
+
}
|
|
173
|
+
]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Svelte single-file component.
|
|
3
|
+
|
|
4
|
+
The SFC plugin delegates the <script> block to TypeScript and the
|
|
5
|
+
<style> block to SCSS; their symbols carry those languages and their
|
|
6
|
+
imports resolve through the matching resolver. The markup template is
|
|
7
|
+
not extracted.
|
|
8
|
+
-->
|
|
9
|
+
<script lang="ts">
|
|
10
|
+
import { greet } from "./greeter";
|
|
11
|
+
|
|
12
|
+
export let name: string = "world";
|
|
13
|
+
|
|
14
|
+
function shout(): string {
|
|
15
|
+
return greet(name).toUpperCase();
|
|
16
|
+
}
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
<style lang="scss">
|
|
20
|
+
$accent: #00aa77;
|
|
21
|
+
|
|
22
|
+
.greeting {
|
|
23
|
+
color: $accent;
|
|
24
|
+
font-weight: bold;
|
|
25
|
+
}
|
|
26
|
+
</style>
|
|
27
|
+
|
|
28
|
+
<h1 class="greeting">{shout()}</h1>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""SFC chunker edge cases: the markup template as a host chunk."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from rbtr.domain.models import ChunkKind
|
|
6
|
+
from rbtr.git import FileEntry
|
|
7
|
+
from rbtr.languages.extract import extract_file
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_svelte_template_extracted_as_host_chunk() -> None:
|
|
11
|
+
"""The SFC markup template is a searchable host (`svelte`) chunk.
|
|
12
|
+
|
|
13
|
+
Distinct from the delegated `<script>`/`<style>` chunks: the template is
|
|
14
|
+
the component's own markup, so it carries the host language.
|
|
15
|
+
"""
|
|
16
|
+
src = """\
|
|
17
|
+
<script lang="ts">
|
|
18
|
+
export let name: string = "world";
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<h1 class="greeting">Hello {name}</h1>
|
|
22
|
+
"""
|
|
23
|
+
chunks = extract_file(FileEntry("input", "sha1", src.encode()), "svelte")
|
|
24
|
+
host = [c for c in chunks if c.language == "svelte"]
|
|
25
|
+
assert host, "expected a svelte-language chunk for the template markup"
|
|
26
|
+
assert "greeting" in host[0].content
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_svelte_without_template_still_emits_host_chunk() -> None:
|
|
30
|
+
"""A script-only SFC still emits one (empty) host chunk.
|
|
31
|
+
|
|
32
|
+
The host chunk records the host language/version for dedup, so a
|
|
33
|
+
svelte-plugin bump invalidates the file even with no template markup.
|
|
34
|
+
"""
|
|
35
|
+
src = '<script lang="ts">\n export const x = 1;\n</script>\n'
|
|
36
|
+
chunks = extract_file(FileEntry("input", "sha1", src.encode()), "svelte")
|
|
37
|
+
host = [c for c in chunks if c.language == "svelte"]
|
|
38
|
+
assert len(host) == 1
|
|
39
|
+
assert host[0].content == ""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_sfc_top_level_comment_is_its_own_chunk() -> None:
|
|
43
|
+
"""A comment above the component's markup is searchable content.
|
|
44
|
+
|
|
45
|
+
The template chunk excludes it, so it needs a chunk of its own or the
|
|
46
|
+
banner explaining what a component is for cannot be found.
|
|
47
|
+
"""
|
|
48
|
+
src = """\
|
|
49
|
+
<!--
|
|
50
|
+
Greeting: shouts a name.
|
|
51
|
+
-->
|
|
52
|
+
|
|
53
|
+
<script>
|
|
54
|
+
let name = "world";
|
|
55
|
+
</script>
|
|
56
|
+
|
|
57
|
+
<h1>{name}</h1>
|
|
58
|
+
"""
|
|
59
|
+
chunks = extract_file(FileEntry("input", "sha1", src.encode()), "svelte")
|
|
60
|
+
comments = [c for c in chunks if c.kind == ChunkKind.COMMENT]
|
|
61
|
+
assert [(c.line_start, c.line_end) for c in comments] == [(1, 3)]
|
|
62
|
+
assert "shouts a name" in comments[0].content
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""Svelte sample extraction: the `samples/svelte/` project through the real pipeline."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
from tree_sitter import Parser
|
|
10
|
+
|
|
11
|
+
from rbtr.domain.models import Chunk, ChunkKind, Edge
|
|
12
|
+
from rbtr.git import FileEntry
|
|
13
|
+
from rbtr.languages.edges import build_resolution_map, infer_import_edges
|
|
14
|
+
from rbtr.languages.extract import extract_file
|
|
15
|
+
from rbtr.languages.manager import get_manager
|
|
16
|
+
from rbtr.testing import assert_document_fully_chunked, render_edges
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from syrupy.assertion import SnapshotAssertion
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.fixture
|
|
23
|
+
def project() -> list[tuple[str, str]]:
|
|
24
|
+
root = Path(__file__).parent / "samples" / "svelte"
|
|
25
|
+
return [
|
|
26
|
+
(str(p.relative_to(root)), p.read_text()) for p in sorted(root.rglob("*")) if p.is_file()
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.fixture
|
|
31
|
+
def chunks(project: list[tuple[str, str]]) -> list[Chunk]:
|
|
32
|
+
manager = get_manager()
|
|
33
|
+
out: list[Chunk] = []
|
|
34
|
+
for path, text in project:
|
|
35
|
+
lang = manager.detect_language(path) or "svelte"
|
|
36
|
+
out.extend(extract_file(FileEntry(path, "sha1", text.encode()), lang))
|
|
37
|
+
return out
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@pytest.fixture
|
|
41
|
+
def edges(project: list[tuple[str, str]], chunks: list[Chunk]) -> list[Edge]:
|
|
42
|
+
manager = get_manager()
|
|
43
|
+
repo_files = {path for path, _ in project}
|
|
44
|
+
return infer_import_edges(chunks, repo_files, build_resolution_map(manager))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_emits_expected_kinds(chunks: list[Chunk]) -> None:
|
|
48
|
+
"""The `<script lang="ts">` and `<style lang="scss">` blocks are delegated; the template is a host chunk."""
|
|
49
|
+
kinds = {c.kind for c in chunks}
|
|
50
|
+
assert {
|
|
51
|
+
ChunkKind.FUNCTION,
|
|
52
|
+
ChunkKind.VARIABLE,
|
|
53
|
+
ChunkKind.DOC_SECTION,
|
|
54
|
+
ChunkKind.IMPORT,
|
|
55
|
+
} <= kinds
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_parses_cleanly(project: list[tuple[str, str]]) -> None:
|
|
59
|
+
manager = get_manager()
|
|
60
|
+
for path, text in project:
|
|
61
|
+
grammar = manager.grammar(manager.detect_language(path) or "svelte")
|
|
62
|
+
assert grammar is not None
|
|
63
|
+
assert not Parser(grammar).parse(text.encode()).root_node.has_error, path
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_extraction_matches_snapshot(chunks: list[Chunk], snapshot_json: SnapshotAssertion) -> None:
|
|
67
|
+
assert chunks == snapshot_json
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_edges_match_snapshot(
|
|
71
|
+
chunks: list[Chunk], edges: list[Edge], snapshot_json: SnapshotAssertion
|
|
72
|
+
) -> None:
|
|
73
|
+
assert render_edges(edges, chunks) == snapshot_json
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_every_block_of_every_document_is_chunked(
|
|
77
|
+
project: list[tuple[str, str]], chunks: list[Chunk]
|
|
78
|
+
) -> None:
|
|
79
|
+
"""A chunker owns the whole document, so no top-level block is dropped."""
|
|
80
|
+
manager = get_manager()
|
|
81
|
+
for path, text in project:
|
|
82
|
+
if manager.detect_language(path) != "svelte":
|
|
83
|
+
continue
|
|
84
|
+
grammar = manager.grammar("svelte")
|
|
85
|
+
assert grammar is not None
|
|
86
|
+
assert_document_fully_chunked(path, text, chunks, grammar)
|