buntic 0.3.1 → 0.4.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/README.md +1 -1
- package/dist/cli.js +1251 -73
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1274 -80
- package/dist/template/block-scanner.d.ts +28 -0
- package/dist/template/directives.d.ts +17 -8
- package/dist/template/engine.d.ts +2 -1
- package/dist/template/errors.d.ts +58 -0
- package/dist/template/expressions.d.ts +16 -0
- package/package.json +1 -1
- package/templates/skills/buntic/SKILL.md +50 -10
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Balanced, quote-aware HTML directive block scanner.
|
|
3
|
+
* Handles nested blocks (<if>, <unless>, <match>, <each>), operators like ">" inside
|
|
4
|
+
* quoted attributes (e.g. <if is="age > 18">), HTML comments, and raw <script>/<style> blocks.
|
|
5
|
+
*/
|
|
6
|
+
export interface TemplateBlock {
|
|
7
|
+
tag: string;
|
|
8
|
+
fullMatch: string;
|
|
9
|
+
openTag: string;
|
|
10
|
+
attrString: string;
|
|
11
|
+
inner: string;
|
|
12
|
+
start: number;
|
|
13
|
+
end: number;
|
|
14
|
+
}
|
|
15
|
+
export interface SplitSubBlocksResult {
|
|
16
|
+
body: string;
|
|
17
|
+
subBlocks: TemplateBlock[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Finds all top-level balanced blocks for the given directive tag names.
|
|
21
|
+
*/
|
|
22
|
+
export declare function findTopLevelBlocks(html: string, tagNames: string[]): TemplateBlock[];
|
|
23
|
+
/**
|
|
24
|
+
* Extracts direct (depth-0) child sub-blocks (`allowedTags`) from `inner` while ignoring
|
|
25
|
+
* identically named tags nested inside `nestedScopeTags` (e.g. an `<else>` inside a nested `<if>`).
|
|
26
|
+
* Returns the remaining body text (with extracted sub-blocks removed) and the ordered sub-blocks.
|
|
27
|
+
*/
|
|
28
|
+
export declare function splitDirectSubBlocks(inner: string, allowedTags: string[], nestedScopeTags: string[]): SplitSubBlocksResult;
|
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Template directive evaluators:
|
|
3
|
+
* - Decision blocks: <if>, <unless>, <match>/<case>/<else>/<none>/<default>
|
|
4
|
+
* - Safe expressions in is="..." and conditional attribute if="..."
|
|
5
|
+
* - Strict typed value & range matchers (string, number, boolean, min, max)
|
|
6
|
+
* - Variable interpolation {{ var ?? "fallback" }}
|
|
3
7
|
*/
|
|
4
|
-
|
|
5
|
-
export
|
|
8
|
+
import { evaluateExpression, ExpressionSyntaxError, getNestedValue, isTruthy } from "./expressions.js";
|
|
9
|
+
export { evaluateExpression, ExpressionSyntaxError, getNestedValue, isTruthy };
|
|
10
|
+
export interface DirectiveDiagnosticContext {
|
|
11
|
+
filePath?: string;
|
|
12
|
+
source?: string;
|
|
13
|
+
projectRoot?: string;
|
|
14
|
+
}
|
|
6
15
|
/**
|
|
7
|
-
* Resolves
|
|
8
|
-
* 1.
|
|
9
|
-
* 2. Self-closing elements:
|
|
10
|
-
* 3. Paired elements:
|
|
16
|
+
* Resolves all decision directives in `html`:
|
|
17
|
+
* 1. Block directives: `<if>`, `<unless>`, `<match>` (with balanced nesting)
|
|
18
|
+
* 2. Self-closing elements with conditional attribute: `<tag ... if="expr" ... />`
|
|
19
|
+
* 3. Paired elements with conditional attribute: `<tag ... if="expr" ...>...</tag>`
|
|
11
20
|
*/
|
|
12
|
-
export declare function resolveConditionals(html: string, context: Record<string, unknown
|
|
21
|
+
export declare function resolveConditionals(html: string, context: Record<string, unknown>, diag?: DirectiveDiagnosticContext): string;
|
|
13
22
|
export declare function interpolateTemplate(html: string, context: Record<string, unknown>): string;
|
|
@@ -30,10 +30,11 @@ export declare class TemplateEngine {
|
|
|
30
30
|
assertNoWebComponentConflicts(): void;
|
|
31
31
|
clearUsedWebComponents(): void;
|
|
32
32
|
resolveFilePath(rawSrc: string, currentFile: string, defaultDir: string): string;
|
|
33
|
+
private getOriginalSource;
|
|
33
34
|
private resolveLayout;
|
|
34
35
|
resolveEach(html: string, context: TemplateContext, currentFile: string, depth?: number): Promise<string>;
|
|
35
36
|
resolveComponents(html: string, context: TemplateContext, currentFile: string, depth?: number): Promise<string>;
|
|
36
37
|
private renderWebComponentAlias;
|
|
37
|
-
resolveConditionals(html: string, context: TemplateContext): string;
|
|
38
|
+
resolveConditionals(html: string, context: TemplateContext, currentFile?: string, originalSource?: string): string;
|
|
38
39
|
interpolate(html: string, context: TemplateContext): string;
|
|
39
40
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface TemplateErrorLocation {
|
|
2
|
+
filePath?: string;
|
|
3
|
+
line?: number;
|
|
4
|
+
column?: number;
|
|
5
|
+
endColumn?: number;
|
|
6
|
+
source?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface TemplateErrorOptions {
|
|
9
|
+
message: string;
|
|
10
|
+
location?: TemplateErrorLocation;
|
|
11
|
+
projectRoot?: string;
|
|
12
|
+
resolvedTarget?: string;
|
|
13
|
+
hint?: string;
|
|
14
|
+
cause?: unknown;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Formats an absolute file path cleanly relative to `projectRoot` when inside it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function formatTemplatePath(filePath: string, projectRoot?: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Converts a 0-based character offset in `source` into 1-based `{ line, column }`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function offsetToLineColumn(source: string, offset: number): {
|
|
24
|
+
line: number;
|
|
25
|
+
column: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Renders a compiler-style code frame highlighting `line` and underlining `column..endColumn`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function formatCodeFrame(source: string, line: number, column?: number, endColumn?: number, contextLines?: number): string;
|
|
31
|
+
/**
|
|
32
|
+
* Locates a template snippet inside the original template source (falling back to
|
|
33
|
+
* `fallbackHtml` + `fallbackOffset` when rendering inline/virtual templates).
|
|
34
|
+
*/
|
|
35
|
+
export declare function findSnippetLocation(options: {
|
|
36
|
+
source?: string;
|
|
37
|
+
snippet: string;
|
|
38
|
+
openTag?: string;
|
|
39
|
+
fallbackHtml?: string;
|
|
40
|
+
fallbackOffset?: number;
|
|
41
|
+
filePath?: string;
|
|
42
|
+
}): TemplateErrorLocation;
|
|
43
|
+
/**
|
|
44
|
+
* Structured template error with source file, line/column, highlighted code frame,
|
|
45
|
+
* resolved target path, underlying cause, and actionable hint.
|
|
46
|
+
*/
|
|
47
|
+
export declare class BunticTemplateError extends Error {
|
|
48
|
+
readonly summary: string;
|
|
49
|
+
readonly filePath?: string;
|
|
50
|
+
readonly line?: number;
|
|
51
|
+
readonly column?: number;
|
|
52
|
+
readonly endColumn?: number;
|
|
53
|
+
readonly codeFrame?: string;
|
|
54
|
+
readonly resolvedTarget?: string;
|
|
55
|
+
readonly causeMessage?: string;
|
|
56
|
+
readonly hint?: string;
|
|
57
|
+
constructor(options: TemplateErrorOptions);
|
|
58
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency, eval-free expression parser and evaluator for Buntic template directives.
|
|
3
|
+
* Supports nested paths (e.g. `position.x`, `items.length`), literals (numbers, strings,
|
|
4
|
+
* booleans, null, undefined), unary (`!`, `-`), relational (`<`, `<=`, `>`, `>=`),
|
|
5
|
+
* equality (`===`, `!==`, `==`, `!=`), logical (`&&`, `||`), and parentheses.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ExpressionSyntaxError extends Error {
|
|
8
|
+
readonly hint?: string;
|
|
9
|
+
constructor(message: string, hint?: string);
|
|
10
|
+
}
|
|
11
|
+
export declare function isTruthy(val: unknown): boolean;
|
|
12
|
+
export declare function getNestedValue(obj: unknown, path: string): unknown;
|
|
13
|
+
/**
|
|
14
|
+
* Safely parses and evaluates a template expression against `context`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function evaluateExpression(expr: string, context: Record<string, unknown>): unknown;
|
package/package.json
CHANGED
|
@@ -130,31 +130,71 @@ Inyecta en `<head>` el `<title>`, `<link rel="canonical">`, OpenGraph y Twitter
|
|
|
130
130
|
<seo title="Catálogo" image-key="banners/catalogo.jpg" />
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
-
### 5. Bucles e Iteración (`<each>`)
|
|
133
|
+
### 5. Bucles e Iteración (`<each>` y fallbacks `<none>`, `<else>`, `<default>`)
|
|
134
134
|
```html
|
|
135
135
|
<!-- Con Auto-spread automático de claves a las props del componente -->
|
|
136
136
|
<each src="../data/features.json">
|
|
137
137
|
<component src="../components/card.html" />
|
|
138
138
|
</each>
|
|
139
139
|
|
|
140
|
-
<!-- Con alias explícito -->
|
|
140
|
+
<!-- Con alias explícito y fallbacks -->
|
|
141
141
|
<each in="site.navLinks" as="link">
|
|
142
142
|
<a href="{{ link.url }}">{{ link.label }}</a>
|
|
143
|
+
<none><p>Sin enlaces disponibles.</p></none>
|
|
144
|
+
<else><p>Menú no definido.</p></else>
|
|
145
|
+
</each>
|
|
146
|
+
|
|
147
|
+
<!-- Fallback universal (<default> cubre tanto lista vacía como undefined/null) -->
|
|
148
|
+
<each in="props.items">
|
|
149
|
+
<li>{{ name }}</li>
|
|
150
|
+
<default><p>Nada que mostrar.</p></default>
|
|
143
151
|
</each>
|
|
144
152
|
```
|
|
145
153
|
|
|
146
|
-
### 6. Renderizado Condicional (`<if>` y atributo `if`)
|
|
154
|
+
### 6. Decisiones y Renderizado Condicional (`<if>`, `<unless>`, `<match>` y atributo `if`)
|
|
155
|
+
Todas las etiquetas de control de flujo son de **una sola palabra** (sin guiones, evitando colisiones con Web Components) y soportan **expresiones seguras en `is="..."`** (sin `{{ }}`, sin `eval`: `!`, `===`, `!==`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `&&`, `||`, paréntesis, rutas anidadas y `.length`) así como **atributos literales tipados estrictos** (`string`, `number`, `boolean`, `min`, `max`):
|
|
156
|
+
|
|
147
157
|
```html
|
|
148
|
-
<!-- Atributo condicional: elimina la etiqueta si
|
|
158
|
+
<!-- Atributo condicional: elimina la etiqueta si la expresión es falsa -->
|
|
149
159
|
<span class="badge" if="badge">{{ badge }}</span>
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
<
|
|
153
|
-
|
|
154
|
-
<
|
|
155
|
-
<
|
|
160
|
+
<span class="count" if="items.length > 0 && !isHidden">{{ items.length }}</span>
|
|
161
|
+
|
|
162
|
+
<!-- Bloque condicional con expresiones seguras y <else> -->
|
|
163
|
+
<if is="age >= 18 && age <= 30">
|
|
164
|
+
<span>Adulto joven</span>
|
|
165
|
+
<else>
|
|
166
|
+
<span>Fuera de rango</span>
|
|
167
|
+
</else>
|
|
156
168
|
</if>
|
|
157
169
|
|
|
170
|
+
<!-- Atributos tipados / rango (verificación estricta de tipo, solo literales) -->
|
|
171
|
+
<if is="age" number="0"><span>Recién nacido</span></if>
|
|
172
|
+
<if is="age" min="18" max="30"><span>Adulto joven</span></if>
|
|
173
|
+
<if is="role" string="admin"><span>Admin</span></if>
|
|
174
|
+
<if is="active" boolean="true"><span>Activo</span></if>
|
|
175
|
+
|
|
176
|
+
<!-- Inverso declarativo con <unless> -->
|
|
177
|
+
<unless is="props.product.inStock">
|
|
178
|
+
<span class="out-of-stock">Agotado</span>
|
|
179
|
+
</unless>
|
|
180
|
+
|
|
181
|
+
<!-- Selección múltiple con <match>/<case> (primera coincidencia gana) -->
|
|
182
|
+
<match is="age">
|
|
183
|
+
<case number="0">Recién nacido</case>
|
|
184
|
+
<case min="1" max="17">Menor de edad</case>
|
|
185
|
+
<case min="18" max="64">Adulto</case>
|
|
186
|
+
<case min="65">Adulto mayor</case>
|
|
187
|
+
<none>Otro valor</none>
|
|
188
|
+
<else>Edad no definida</else>
|
|
189
|
+
</match>
|
|
190
|
+
|
|
191
|
+
<!-- Fallback universal <default> en <match> (cubre <else> y <none>) -->
|
|
192
|
+
<match is="role">
|
|
193
|
+
<case string="admin">Panel Admin</case>
|
|
194
|
+
<case string="editor">Panel Editor</case>
|
|
195
|
+
<default>Invitado</default>
|
|
196
|
+
</match>
|
|
197
|
+
|
|
158
198
|
<!-- Operador de respaldo en texto -->
|
|
159
199
|
<p>Autor: {{ author ?? "Anónimo" }}</p>
|
|
160
200
|
```
|