Skip to content

Commit f0f2cea

Browse files
committed
refactor: use tag instead of ext
Signed-off-by: 90dy <90dy@proton.me>
1 parent 89501a5 commit f0f2cea

8 files changed

Lines changed: 76 additions & 65 deletions

File tree

deno.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.5.25",
2+
"version": "0.6.0",
33
"author": "90dy",
44
"license": "MIT",
55
"workspace": [
@@ -17,7 +17,7 @@
1717
"gen": "deno run -A src/gen/main.ts"
1818
},
1919
"imports": {
20-
"@std/assert": "jsr:@std/assert@1",
20+
"@std/assert": "jsr:@std/assert@^1.0.13",
2121
"@deno/dnt": "jsr:@deno/dnt@^0.41.3",
2222
"@std/path": "jsr:@std/path@^1.0.9",
2323
"@std/yaml": "jsr:@std/yaml@^1.0.6",
@@ -27,5 +27,13 @@
2727
"repository": {
2828
"type": "git",
2929
"url": "https://github.com/90dy/typescript-template-engine"
30+
},
31+
"lint": {
32+
"rules": {
33+
"exclude": [
34+
"no-unused-vars",
35+
"no-unused-labels"
36+
]
37+
}
3038
}
3139
}

main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
svg, diff, proto, sol,
2727

2828
// Language utilities
29-
ext,
29+
tag,
3030
} from "./src/mod.ts";
3131

3232
/**

main_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEquals } from "@std/assert";
2-
import { html, css, js, sql, json, md, ext } from "./src/mod.ts";
2+
import { html, css, tag } from "./src/mod.ts";
33

44
Deno.test("HTML template test", () => {
55
const title = "Test Title";
@@ -47,7 +47,7 @@ Deno.test("Custom extension template test", () => {
4747
const name = "Test";
4848
const version = "1.0.0";
4949

50-
const result = ext("yaml")`
50+
const result = tag("yaml")`
5151
name: ${name}
5252
version: ${version}
5353
`;

src/core/mod.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -399,84 +399,84 @@ class TemplateClass<T = unknown> extends String {
399399
* @param extension The file extension (without the dot)
400400
* @returns A template tag function for the specified extension
401401
*/
402-
export function ext<Type extends string>(
402+
export function tag<Type extends string>(
403403
type: Type,
404404
parser?: (text: string) => unknown,
405405
options?: {
406406
indent: false | number;
407407
},
408-
): Ext<Type> {
408+
): Tag<Type> {
409409
return <T>(
410410
template: { raw: readonly string[] | ArrayLike<string> },
411411
...substitutions: any[]
412412
) => new TemplateClass(type, template, substitutions, parser, options) as Template<T>;
413413
}
414414

415-
type Ext<Type extends string> = <T>(
415+
type Tag<Type extends string> = <T>(
416416
template: { raw: readonly string[] | ArrayLike<string> },
417417
...substitutions: any[]
418418
) => Template<T>;
419419

420420
// Export all template functions individually
421421
// Web languages
422-
export const html: Ext<"html"> = ext("html");
423-
export const css: Ext<"css"> = ext("css");
424-
export const js: Ext<"js"> = ext("js");
425-
export const ts: Ext<"ts"> = ext("ts");
426-
export const jsx: Ext<"jsx"> = ext("jsx");
427-
export const tsx: Ext<"tsx"> = ext("tsx");
422+
export const html: Tag<"html"> = tag("html");
423+
export const css: Tag<"css"> = tag("css");
424+
export const js: Tag<"js"> = tag("js");
425+
export const ts: Tag<"ts"> = tag("ts");
426+
export const jsx: Tag<"jsx"> = tag("jsx");
427+
export const tsx: Tag<"tsx"> = tag("tsx");
428428

429429
// Data formats
430-
export const json: Ext<"json"> = ext("json", JSON.parse);
431-
export const xml: Ext<"xml"> = ext("xml");
432-
export const yaml: Ext<"yaml"> = ext("yaml", YAML.parse);
433-
export const toml: Ext<"toml"> = ext("toml");
434-
export const ini: Ext<"ini"> = ext("ini");
435-
export const csv: Ext<"csv"> = ext("csv");
430+
export const json: Tag<"json"> = tag("json", JSON.parse);
431+
export const xml: Tag<"xml"> = tag("xml");
432+
export const yaml: Tag<"yaml"> = tag("yaml", YAML.parse);
433+
export const toml: Tag<"toml"> = tag("toml");
434+
export const ini: Tag<"ini"> = tag("ini");
435+
export const csv: Tag<"csv"> = tag("csv");
436436

437437
// Markup languages
438-
export const md: Ext<"md"> = ext("md");
439-
export const tex: Ext<"tex"> = ext("tex");
440-
export const rst: Ext<"rst"> = ext("rst");
438+
export const md: Tag<"md"> = tag("md");
439+
export const tex: Tag<"tex"> = tag("tex");
440+
export const rst: Tag<"rst"> = tag("rst");
441441

442442
// Query languages
443-
export const sql: Ext<"sql"> = ext("sql");
444-
export const graphql: Ext<"graphql"> = ext("graphql");
443+
export const sql: Tag<"sql"> = tag("sql");
444+
export const graphql: Tag<"graphql"> = tag("graphql");
445445

446446
// Shell scripting
447-
export const sh: Ext<"sh"> = ext("sh");
448-
export const ps1: Ext<"ps1"> = ext("ps1");
449-
export const bat: Ext<"bat"> = ext("bat");
447+
export const sh: Tag<"sh"> = tag("sh");
448+
export const ps1: Tag<"ps1"> = tag("ps1");
449+
export const bat: Tag<"bat"> = tag("bat");
450450

451451
// Programming languages
452-
export const py: Ext<"py"> = ext("py");
453-
export const rb: Ext<"rb"> = ext("rb");
454-
export const go: Ext<"go"> = ext("go");
455-
export const rs: Ext<"rs"> = ext("rs");
456-
export const c: Ext<"c"> = ext("c");
457-
export const cpp: Ext<"cpp"> = ext("cpp");
458-
export const cs: Ext<"cs"> = ext("cs");
459-
export const java: Ext<"java"> = ext("java");
460-
export const php: Ext<"php"> = ext("php");
461-
export const swift: Ext<"swift"> = ext("swift");
462-
export const kt: Ext<"kt"> = ext("kt");
463-
export const scala: Ext<"scala"> = ext("scala");
464-
export const dart: Ext<"dart"> = ext("dart");
465-
export const lua: Ext<"lua"> = ext("lua");
466-
export const pl: Ext<"pl"> = ext("pl");
467-
export const r: Ext<"r"> = ext("r");
468-
export const elm: Ext<"elm"> = ext("elm");
469-
export const fs: Ext<"fs"> = ext("fs");
470-
export const clj: Ext<"clj"> = ext("clj");
471-
export const hs: Ext<"hs"> = ext("hs");
452+
export const py: Tag<"py"> = tag("py");
453+
export const rb: Tag<"rb"> = tag("rb");
454+
export const go: Tag<"go"> = tag("go");
455+
export const rs: Tag<"rs"> = tag("rs");
456+
export const c: Tag<"c"> = tag("c");
457+
export const cpp: Tag<"cpp"> = tag("cpp");
458+
export const cs: Tag<"cs"> = tag("cs");
459+
export const java: Tag<"java"> = tag("java");
460+
export const php: Tag<"php"> = tag("php");
461+
export const swift: Tag<"swift"> = tag("swift");
462+
export const kt: Tag<"kt"> = tag("kt");
463+
export const scala: Tag<"scala"> = tag("scala");
464+
export const dart: Tag<"dart"> = tag("dart");
465+
export const lua: Tag<"lua"> = tag("lua");
466+
export const pl: Tag<"pl"> = tag("pl");
467+
export const r: Tag<"r"> = tag("r");
468+
export const elm: Tag<"elm"> = tag("elm");
469+
export const fs: Tag<"fs"> = tag("fs");
470+
export const clj: Tag<"clj"> = tag("clj");
471+
export const hs: Tag<"hs"> = tag("hs");
472472

473473
// Configuration files
474-
export const dockerfile: Ext<"dockerfile"> = ext("dockerfile");
475-
export const makefile: Ext<"makefile"> = ext("makefile");
476-
export const mk: Ext<"mk"> = ext("mk");
474+
export const dockerfile: Tag<"dockerfile"> = tag("dockerfile");
475+
export const makefile: Tag<"makefile"> = tag("makefile");
476+
export const mk: Tag<"mk"> = tag("mk");
477477

478478
// Other
479-
export const svg: Ext<"svg"> = ext("svg");
480-
export const diff: Ext<"diff"> = ext("diff");
481-
export const proto: Ext<"proto"> = ext("proto");
482-
export const sol: Ext<"sol"> = ext("sol");
479+
export const svg: Tag<"svg"> = tag("svg");
480+
export const diff: Tag<"diff"> = tag("diff");
481+
export const proto: Tag<"proto"> = tag("proto");
482+
export const sol: Tag<"sol"> = tag("sol");

src/core/mod_test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
diff,
4747
proto,
4848
sol,
49-
ext,
49+
tag,
5050
LANGUAGES,
5151
} from "./mod.ts";
5252

@@ -98,7 +98,7 @@ Deno.test("Core module exports all template functions", () => {
9898
assertEquals(typeof diff, "function");
9999
assertEquals(typeof proto, "function");
100100
assertEquals(typeof sol, "function");
101-
assertEquals(typeof ext, "function");
101+
assertEquals(typeof tag, "function");
102102
});
103103

104104
Deno.test("LANGUAGES object contains all supported languages", () => {
@@ -200,7 +200,7 @@ Deno.test("Template indentation works correctly", () => {
200200
});
201201

202202
Deno.test("Custom extension template works correctly", () => {
203-
const customTemplate = ext("custom")`Test content`;
203+
const customTemplate = tag("custom")`Test content`;
204204

205205
// Test that the template has the correct type
206206
assertEquals(customTemplate.type, "custom");
@@ -209,7 +209,7 @@ Deno.test("Custom extension template works correctly", () => {
209209

210210
Deno.test("Template with parser works correctly", () => {
211211
const parser = (text: string) => ({ parsed: text });
212-
const customTemplate = ext("custom", parser)<{ parsed: string }>`Test content`.parse();
212+
const customTemplate = tag("custom", parser)<{ parsed: string }>`Test content`.parse();
213213

214214
// Test that the template has parsed data
215215
assertEquals(customTemplate.data !== undefined, true);

src/gen/main.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
22
import * as path from "@std/path";
33
import { LANGUAGES } from "@tmpl/core";
44

5-
let [destination, source = destination] = Deno.args
5+
const [destination, source = destination] = Deno.args;
66

77
async function gen(templatePath: string): Promise<string> {
88
const { default: content } = await import(templatePath);
@@ -13,7 +13,7 @@ async function gen(templatePath: string): Promise<string> {
1313
}
1414
}
1515

16-
const errors: any[] = [];
16+
const errors: unknown[] = [];
1717

1818
if (destination) {
1919
const templateFiles = await fs
@@ -73,12 +73,15 @@ if (destination) {
7373
);
7474
}
7575
}
76-
7776
}
7877

7978
if (errors.length > 0) {
8079
errors.forEach((error) => {
81-
console.warn(error.message);
80+
if (error instanceof Error) {
81+
console.warn(error.message);
82+
} else {
83+
console.warn("Unknown error:", error);
84+
}
8285
});
8386
Deno.exit(1);
8487
}

src/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export {
3434
svg, diff, proto, sol,
3535

3636
// Language utilities
37-
ext,
37+
tag,
3838
type LanguageDefinition,
3939
LANGUAGES,
4040
} from "./core/mod.ts";

vscode-extension/generate-syntaxes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function generateSyntaxConfig(langKey: string, langDef: typeof LANGUAGES[keyof t
256256
* @returns The grammars section for package.json
257257
*/
258258
function generatePackageJsonGrammars() {
259-
return Object.entries(LANGUAGES).map(([langKey, langDef]) => {
259+
return Object.entries(LANGUAGES).map(([_langKey, langDef]) => {
260260
const extension = langDef.extension;
261261

262262
// Map special language identifiers

0 commit comments

Comments
 (0)