Skip to content

Commit f5589f9

Browse files
committed
fix(core): make Template a string primitive
Signed-off-by: 90dy <90dy@proton.me>
1 parent 85a6823 commit f5589f9

File tree

6 files changed

+18
-21
lines changed

6 files changed

+18
-21
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.5.20",
2+
"version": "0.5.23",
33
"author": "90dy",
44
"license": "MIT",
55
"workspace": [

src/core/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"exports": {
2121
".": "./mod.ts"
2222
},
23-
"version": "0.5.20"
23+
"version": "0.5.21"
2424
}

src/core/mod.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ export const LANGUAGES: Record<string, LanguageDefinition> = {
300300
},
301301
};
302302

303-
class TemplateDocument<T> extends String {
303+
type Template<T> = string & TemplateClass<T>;
304+
305+
class TemplateClass<T = unknown> extends String {
304306
public readonly type: string;
305307
public readonly raw: string;
306308
public data?: T;
@@ -350,36 +352,36 @@ class TemplateDocument<T> extends String {
350352
return this.valueOf();
351353
}
352354

353-
indent(value: false | number): TemplateDocument<T> {
354-
return new TemplateDocument<T>(
355+
indent(value: false | number): Template<T> {
356+
return new TemplateClass<T>(
355357
this.type,
356358
{ raw: [this.raw] },
357359
[],
358360
undefined,
359361
{
360362
indent: value,
361363
},
362-
);
364+
) as Template<T>;
363365
}
364-
noindent(): TemplateDocument<T> {
366+
noindent(): Template<T> {
365367
return this.indent(false);
366368
}
367-
throw(): TemplateDocument<T> {
369+
throw(): Template<T> | never {
368370
if (this.error) {
369371
throw this.error;
370372
}
371-
return this;
373+
return this as unknown as Template<T>;
372374
}
373375
parse<TParsed = T>(
374376
parser?: (text: string) => TParsed,
375-
): TemplateDocument<TParsed> {
377+
): Template<TParsed> {
376378
this.parser = parser ?? this.parser;
377379
try {
378380
this.data = this.parser?.(this.raw) as T;
379381
} catch (error) {
380382
this.error = error as Error;
381383
}
382-
return this as unknown as TemplateDocument<TParsed>;
384+
return this as unknown as Template<TParsed>;
383385
}
384386
}
385387

@@ -396,16 +398,16 @@ export function ext<Type extends string>(
396398
indent: false | number;
397399
},
398400
): Ext<Type> {
399-
return (
401+
return <T>(
400402
template: { raw: readonly string[] | ArrayLike<string> },
401403
...substitutions: any[]
402-
) => new TemplateDocument(type, template, substitutions, parser, options);
404+
) => new TemplateClass(type, template, substitutions, parser, options) as Template<T>;
403405
}
404406

405407
type Ext<Type extends string> = <T>(
406408
template: { raw: readonly string[] | ArrayLike<string> },
407409
...substitutions: any[]
408-
) => TemplateDocument<T>;
410+
) => Template<T>;
409411

410412
// Export all template functions individually
411413
// Web languages

src/core/mod_test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,6 @@ Deno.test("Template functions return TemplateDocument instances", () => {
119119
const cssTemplate = css`.test { color: red; }`;
120120
const jsTemplate = js`console.log("Test");`;
121121

122-
// Test that templates are instances of String
123-
assertEquals(htmlTemplate instanceof String, true);
124-
assertEquals(cssTemplate instanceof String, true);
125-
assertEquals(jsTemplate instanceof String, true);
126-
127122
// Test that templates have the correct type property
128123
assertEquals(htmlTemplate.type, "html");
129124
assertEquals(cssTemplate.type, "css");

src/gen/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929
"bin": {
3030
"tmpl-gen": "./runner.ts"
3131
},
32-
"version": "0.5.20"
32+
"version": "0.5.21"
3333
}

vscode-extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "TypeScript Template Engine",
44
"description": "Use TypeScript as a template engine with syntax highlighting for template tag functions",
55
"publisher": "90dy",
6-
"version": "0.5.20",
6+
"version": "0.5.21",
77
"icon": "icon.png",
88
"engines": {
99
"vscode": "^1.60.0"

0 commit comments

Comments
 (0)