@willbooster/tree-sitter-embedded-template 0.0.0-placeholder.0 → 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Max Brunsfeld
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.
package/README.md CHANGED
@@ -1,3 +1,135 @@
1
1
  # @willbooster/tree-sitter-embedded-template
2
2
 
3
- This version is a placeholder that reserves the package name so that releases can be published from GitHub Actions with trusted publishing. Use 1.0.0 or later.
3
+ [![Test](https://github.com/WillBooster/tree-sitter-embedded-template/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-embedded-template/actions/workflows/test.yml)
4
+ [![Test rust](https://github.com/WillBooster/tree-sitter-embedded-template/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-embedded-template/actions/workflows/test-rust.yml)
5
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
6
+ [![wbfy](https://img.shields.io/badge/wbfy-20.36.11-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
7
+ [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-embedded-template.svg)](https://crates.io/crates/willbooster-tree-sitter-embedded-template)
8
+
9
+ Embedded template grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
10
+ [tree-sitter/tree-sitter-embedded-template](https://github.com/tree-sitter/tree-sitter-embedded-template). We are grateful
11
+ to its authors and contributors. This is not an official release of that project.
12
+
13
+ It parses templating languages such as ERB and EJS, in which scripting code is embedded in text with the delimiters
14
+ `<%` and `%>`. This fork keeps the grammar of upstream's version 0.25.0 and generates the parser with the
15
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator. The parser loads only with that fork's runtime, version 2.0.0 or later
16
+ ([willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter),
17
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter)); upstream's runtime rejects it.
18
+
19
+ ## Usage
20
+
21
+ The npm package ships `tree-sitter-embedded_template.wasm` for
22
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 2.0.0 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. In Node.js and Bun, load the grammar from its path:
23
+
24
+ ```js
25
+ import { fileURLToPath } from 'node:url';
26
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
27
+
28
+ await Parser.init();
29
+ const parser = new Parser();
30
+ const wasmPath = fileURLToPath(
31
+ import.meta.resolve('@willbooster/tree-sitter-embedded-template/tree-sitter-embedded_template.wasm')
32
+ );
33
+ parser.setLanguage(await Language.load(wasmPath));
34
+ const tree = parser.parse('<ul><% items.each do |item| %><li><%= item %></li><% end %></ul>\n');
35
+ ```
36
+
37
+ In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
38
+
39
+ ```js
40
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
41
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
42
+ import grammarUrl from '@willbooster/tree-sitter-embedded-template/tree-sitter-embedded_template.wasm?url';
43
+
44
+ await Parser.init({ locateFile: () => runtimeUrl });
45
+ const parser = new Parser();
46
+ parser.setLanguage(await Language.load(grammarUrl));
47
+ ```
48
+
49
+ Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
50
+ Node.js compatibility:
51
+
52
+ ```js
53
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
54
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
55
+ import grammar from '@willbooster/tree-sitter-embedded-template/tree-sitter-embedded_template.wasm';
56
+
57
+ await Parser.init({ wasmModule: runtime });
58
+ const parser = new Parser();
59
+ parser.setLanguage(await Language.load(grammar));
60
+ ```
61
+
62
+ The package also ships `grammar.js`, the queries in `queries/`, and the node types in `src/node-types.json`.
63
+
64
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-embedded-template) and on
65
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
66
+ fuzzed with:
67
+
68
+ ```toml
69
+ [dependencies]
70
+ tree-sitter = { package = "willbooster-tree-sitter", version = "2.0.0" }
71
+ tree-sitter-embedded-template = { package = "willbooster-tree-sitter-embedded-template", version = "1" }
72
+ ```
73
+
74
+ ```rust
75
+ let mut parser = tree_sitter::Parser::new();
76
+ parser.set_language(&tree_sitter_embedded_template::LANGUAGE.into())?;
77
+ ```
78
+
79
+ ## Development
80
+
81
+ ```sh
82
+ mise install
83
+ bun install --frozen-lockfile
84
+ bun run test/ci-setup # installs Chromium for the browser test
85
+ bun run build/ci
86
+ bun run test
87
+ script/parse-examples
88
+ cargo test
89
+ ```
90
+
91
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
92
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
93
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
94
+ with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary that runs here. Run other CLI commands through it as
95
+ well (e.g. `script/tree-sitter parse --grammar-path . file.erb`).
96
+
97
+ `bun run generate` records a fresh generation profile from the applicable `test/corpus` cases, then generates
98
+ compact parser tables. It would also profile files tracked in `examples/`, but this repository tracks none: the
99
+ directory holds only the clones `script/parse-examples` makes. The parser also embeds metadata from `tree-sitter.json`.
100
+ After changing a grammar, `tree-sitter.json`, or a corpus case, regenerate and commit `src/`.
101
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
102
+ and the release build regenerate the parsers before compiling them.
103
+
104
+ `bun run test` runs:
105
+
106
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
107
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
108
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
109
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
110
+ - a check that the real-world ERB and EJS templates in `examples/` fail to parse exactly as listed in
111
+ `script/known-failures.txt`, which is empty now. The first run clones the repositories. The example repositories are pinned to commits in
112
+ `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples` rewrites it;
113
+ review its diff before committing;
114
+ - a performance check (`test/unit/performance.test.ts`) that parsing a template whose last directive is still open
115
+ takes linear time, since consumers parse files while they are being edited. It loads the Wasm build through
116
+ @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
117
+ - a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
118
+ `willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
119
+ tests, the fuzzer, and the CLI on the latter;
120
+ - tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
121
+ Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
122
+
123
+ The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
124
+ by every checkout; `script/fuzz-corpus` builds a parser of its own in `.tmp/fuzz` for each run and deletes it
125
+ afterwards.
126
+
127
+ CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser natively, and fuzzes the parser
128
+ with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
129
+
130
+ ### References
131
+
132
+ - [ERB Documentation](https://docs.ruby-lang.org/en/master/ERB.html)
133
+ - [EJS Documentation](https://ejs.co/#docs)
134
+ - [etlua Documentation](https://github.com/leafo/etlua)
135
+ - [Eta Documentation](https://eta.js.org/docs)
package/grammar.js ADDED
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @file Embedded Template grammar for tree-sitter
3
+ * @author Max Brunsfeld <maxbrunsfeld@gmail.com>
4
+ * @license MIT
5
+ */
6
+
7
+ /// <reference types="tree-sitter-cli/dsl" />
8
+ // @ts-check
9
+
10
+ module.exports = grammar({
11
+ name: 'embedded_template',
12
+
13
+ extras: _ => [],
14
+
15
+ rules: {
16
+ template: $ => repeat(choice(
17
+ $.directive,
18
+ $.output_directive,
19
+ $.comment_directive,
20
+ $.graphql_directive,
21
+ $.content,
22
+ )),
23
+
24
+ code: _ => repeat1(choice(/[^%=_-]+|[%=_-]/, '%%>')),
25
+
26
+ content: _ => prec.right(repeat1(choice(/[^<]+|</, '<%%'))),
27
+
28
+ directive: $ => seq(
29
+ choice('<%', '<%_', '<%|', '<%~'),
30
+ optional($.code),
31
+ choice('%>', '-%>', '_%>'),
32
+ ),
33
+
34
+ output_directive: $ => seq(
35
+ choice('<%=', '<%==', '<%|=', '<%|==', '<%-'),
36
+ optional($.code),
37
+ choice('%>', '-%>', '=%>'),
38
+ ),
39
+
40
+ comment_directive: $ => seq(
41
+ '<%#',
42
+ optional(alias($.code, $.comment)),
43
+ '%>',
44
+ ),
45
+
46
+ graphql_directive: $ => seq(
47
+ '<%graphql',
48
+ optional($.code),
49
+ '%>',
50
+ ),
51
+ },
52
+ });
package/package.json CHANGED
@@ -1,13 +1,73 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-embedded-template",
3
- "version": "0.0.0-placeholder.0",
4
- "description": "Placeholder for WillBooster's fork of tree-sitter-embedded-template; the first release is 1.0.0",
3
+ "version": "1.0.0",
4
+ "description": "Embedded template (ERB, EJS) grammar for tree-sitter",
5
+ "keywords": [
6
+ "incremental",
7
+ "parsing",
8
+ "tree-sitter",
9
+ "embedded-template"
10
+ ],
5
11
  "repository": {
6
12
  "type": "git",
7
13
  "url": "git+https://github.com/WillBooster/tree-sitter-embedded-template.git"
8
14
  },
9
15
  "license": "MIT",
10
16
  "author": "WillBooster Inc.",
17
+ "files": [
18
+ "grammar.js",
19
+ "queries/*.scm",
20
+ "src/node-types.json",
21
+ "tree-sitter.json",
22
+ "*.wasm"
23
+ ],
24
+ "scripts": {
25
+ "build-wasm": "script/generate --build-wasm",
26
+ "build/ci": "bun run build-wasm",
27
+ "cleanup": "bun wb lint --fix --format",
28
+ "format": "bun wb lint --format",
29
+ "generate": "script/generate",
30
+ "lint": "bun wb lint",
31
+ "lint-fix": "bun wb lint --fix",
32
+ "prepare": "(bun lefthook install || true) && (test ! -f .lefthook/normalize-bun-lockfile.sh || bash .lefthook/normalize-bun-lockfile.sh)",
33
+ "release": "wb release",
34
+ "prestart": "bun run build-wasm",
35
+ "start": "script/tree-sitter playground",
36
+ "test": "bun wb test",
37
+ "test/ci": "bun wb test-on-ci",
38
+ "test/ci-setup": "playwright install --with-deps chromium",
39
+ "typecheck": "bun wb typecheck",
40
+ "verify": "bun wb verify",
41
+ "verify-full": "bun wb verify --full"
42
+ },
43
+ "devDependencies": {
44
+ "@tsconfig/bun": "1.0.11",
45
+ "@types/bun": "1.4.2",
46
+ "@vitest/browser-playwright": "5.0.3",
47
+ "@willbooster/oxfmt-config": "1.2.2",
48
+ "@willbooster/oxlint-config": "1.4.8",
49
+ "@willbooster/wb": "22.24.0",
50
+ "@willbooster/web-tree-sitter": "2.0.1",
51
+ "conventional-changelog-conventionalcommits": "10.4.0",
52
+ "lefthook": "2.1.16",
53
+ "oxfmt": "0.71.0",
54
+ "oxlint": "1.86.0",
55
+ "oxlint-tsgolint": "7.0.2003",
56
+ "playwright": "1.63.0",
57
+ "semantic-release": "25.0.9",
58
+ "sort-package-json": "4.0.0",
59
+ "typescript": "7.0.2",
60
+ "vitest": "5.0.3",
61
+ "wrangler": "4.147.0"
62
+ },
63
+ "peerDependencies": {
64
+ "@willbooster/web-tree-sitter": "^2.0.0"
65
+ },
66
+ "peerDependenciesMeta": {
67
+ "@willbooster/web-tree-sitter": {
68
+ "optional": true
69
+ }
70
+ },
11
71
  "publishConfig": {
12
72
  "access": "public",
13
73
  "registry": "https://registry.npmjs.org/"
@@ -0,0 +1,12 @@
1
+ (comment_directive) @comment
2
+
3
+ [
4
+ "<%#"
5
+ "<%"
6
+ "<%="
7
+ "<%_"
8
+ "<%-"
9
+ "%>"
10
+ "-%>"
11
+ "_%>"
12
+ ] @keyword
@@ -0,0 +1,7 @@
1
+ ((content) @injection.content
2
+ (#set! injection.language "html")
3
+ (#set! injection.combined))
4
+
5
+ ((code) @injection.content
6
+ (#set! injection.language "javascript")
7
+ (#set! injection.combined))
@@ -0,0 +1,7 @@
1
+ ((content) @injection.content
2
+ (#set! injection.language "html")
3
+ (#set! injection.combined))
4
+
5
+ ((code) @injection.content
6
+ (#set! injection.language "ruby")
7
+ (#set! injection.combined))
@@ -0,0 +1,7 @@
1
+ ((content) @injection.content
2
+ (#set! injection.language "html")
3
+ (#set! injection.combined))
4
+
5
+ ((code) @injection.content
6
+ (#set! injection.language "lua")
7
+ (#set! injection.combined))
@@ -0,0 +1,177 @@
1
+ [
2
+ {
3
+ "type": "code",
4
+ "named": true,
5
+ "fields": {}
6
+ },
7
+ {
8
+ "type": "comment",
9
+ "named": true,
10
+ "fields": {}
11
+ },
12
+ {
13
+ "type": "comment_directive",
14
+ "named": true,
15
+ "fields": {},
16
+ "children": {
17
+ "multiple": false,
18
+ "required": false,
19
+ "types": [
20
+ {
21
+ "type": "comment",
22
+ "named": true
23
+ }
24
+ ]
25
+ }
26
+ },
27
+ {
28
+ "type": "content",
29
+ "named": true,
30
+ "fields": {}
31
+ },
32
+ {
33
+ "type": "directive",
34
+ "named": true,
35
+ "fields": {},
36
+ "children": {
37
+ "multiple": false,
38
+ "required": false,
39
+ "types": [
40
+ {
41
+ "type": "code",
42
+ "named": true
43
+ }
44
+ ]
45
+ }
46
+ },
47
+ {
48
+ "type": "graphql_directive",
49
+ "named": true,
50
+ "fields": {},
51
+ "children": {
52
+ "multiple": false,
53
+ "required": false,
54
+ "types": [
55
+ {
56
+ "type": "code",
57
+ "named": true
58
+ }
59
+ ]
60
+ }
61
+ },
62
+ {
63
+ "type": "output_directive",
64
+ "named": true,
65
+ "fields": {},
66
+ "children": {
67
+ "multiple": false,
68
+ "required": false,
69
+ "types": [
70
+ {
71
+ "type": "code",
72
+ "named": true
73
+ }
74
+ ]
75
+ }
76
+ },
77
+ {
78
+ "type": "template",
79
+ "named": true,
80
+ "root": true,
81
+ "fields": {},
82
+ "children": {
83
+ "multiple": true,
84
+ "required": false,
85
+ "types": [
86
+ {
87
+ "type": "comment_directive",
88
+ "named": true
89
+ },
90
+ {
91
+ "type": "content",
92
+ "named": true
93
+ },
94
+ {
95
+ "type": "directive",
96
+ "named": true
97
+ },
98
+ {
99
+ "type": "graphql_directive",
100
+ "named": true
101
+ },
102
+ {
103
+ "type": "output_directive",
104
+ "named": true
105
+ }
106
+ ]
107
+ }
108
+ },
109
+ {
110
+ "type": "%%>",
111
+ "named": false
112
+ },
113
+ {
114
+ "type": "%>",
115
+ "named": false
116
+ },
117
+ {
118
+ "type": "-%>",
119
+ "named": false
120
+ },
121
+ {
122
+ "type": "<%",
123
+ "named": false
124
+ },
125
+ {
126
+ "type": "<%#",
127
+ "named": false
128
+ },
129
+ {
130
+ "type": "<%%",
131
+ "named": false
132
+ },
133
+ {
134
+ "type": "<%-",
135
+ "named": false
136
+ },
137
+ {
138
+ "type": "<%=",
139
+ "named": false
140
+ },
141
+ {
142
+ "type": "<%==",
143
+ "named": false
144
+ },
145
+ {
146
+ "type": "<%_",
147
+ "named": false
148
+ },
149
+ {
150
+ "type": "<%graphql",
151
+ "named": false
152
+ },
153
+ {
154
+ "type": "<%|",
155
+ "named": false
156
+ },
157
+ {
158
+ "type": "<%|=",
159
+ "named": false
160
+ },
161
+ {
162
+ "type": "<%|==",
163
+ "named": false
164
+ },
165
+ {
166
+ "type": "<%~",
167
+ "named": false
168
+ },
169
+ {
170
+ "type": "=%>",
171
+ "named": false
172
+ },
173
+ {
174
+ "type": "_%>",
175
+ "named": false
176
+ }
177
+ ]
@@ -0,0 +1,61 @@
1
+ {
2
+ "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json",
3
+ "grammars": [
4
+ {
5
+ "name": "embedded-template",
6
+ "camelcase": "EmbeddedTemplate",
7
+ "scope": "text.html.ejs",
8
+ "path": ".",
9
+ "file-types": [
10
+ "ejs"
11
+ ],
12
+ "injections": "queries/injections-ejs.scm",
13
+ "injection-regex": "ejs"
14
+ },
15
+ {
16
+ "name": "embedded-template",
17
+ "camelcase": "EmbeddedTemplate",
18
+ "scope": "text.html.erb",
19
+ "path": ".",
20
+ "file-types": [
21
+ "erb"
22
+ ],
23
+ "injections": "queries/injections-erb.scm",
24
+ "injection-regex": "erb"
25
+ },
26
+ {
27
+ "name": "embedded-template",
28
+ "camelcase": "EmbeddedTemplate",
29
+ "scope": "text.html.etlua",
30
+ "path": ".",
31
+ "file-types": [
32
+ "etlua"
33
+ ],
34
+ "injections": "queries/injections-etlua.scm",
35
+ "injection-regex": "etlua"
36
+ }
37
+ ],
38
+ "metadata": {
39
+ "version": "1.0.0",
40
+ "license": "MIT",
41
+ "description": "Embedded template (ERB, EJS) grammar for tree-sitter",
42
+ "authors": [
43
+ {
44
+ "name": "WillBooster Inc."
45
+ }
46
+ ],
47
+ "links": {
48
+ "repository": "https://github.com/WillBooster/tree-sitter-embedded-template"
49
+ }
50
+ },
51
+ "bindings": {
52
+ "c": false,
53
+ "go": false,
54
+ "java": false,
55
+ "node": false,
56
+ "python": false,
57
+ "rust": true,
58
+ "swift": false,
59
+ "zig": false
60
+ }
61
+ }