@tekojs/core 1.1.1

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
+ MIT License
2
+
3
+ Copyright (c) 2026 Teko
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 ADDED
@@ -0,0 +1,2 @@
1
+ # @tekojs/core
2
+ Tokenizer, parser e erros da linguagem Teko.
@@ -0,0 +1 @@
1
+ export * from './src/index.js';
package/dist/index.js ADDED
@@ -0,0 +1,148 @@
1
+ // src/tokenizer.ts
2
+ function tokenize(input) {
3
+ const tokens = [];
4
+ let i = 0;
5
+ while (i < input.length) {
6
+ if (input.startsWith("{{{", i)) {
7
+ const end = input.indexOf("}}}", i);
8
+ if (end === -1) throw new Error("Express\xE3o raw n\xE3o fechada");
9
+ const value2 = input.slice(i + 3, end).trim();
10
+ tokens.push({ type: "expr", value: value2, escaped: false });
11
+ i = end + 3;
12
+ continue;
13
+ }
14
+ if (input.startsWith("{{", i)) {
15
+ const end = input.indexOf("}}", i);
16
+ if (end === -1) throw new Error("Express\xE3o n\xE3o fechada");
17
+ const value2 = input.slice(i + 2, end).trim();
18
+ tokens.push({ type: "expr", value: value2, escaped: true });
19
+ i = end + 2;
20
+ continue;
21
+ }
22
+ if (input.startsWith("@", i)) {
23
+ const nextLine = input.indexOf("\n", i);
24
+ const end = nextLine === -1 ? input.length : nextLine;
25
+ const maybeDirective = input.slice(i, end).trim();
26
+ if (maybeDirective.startsWith("@if") || maybeDirective.startsWith("@else") || maybeDirective.startsWith("@end") || maybeDirective.startsWith("@each") || maybeDirective.startsWith("@slot") || /^@[a-zA-Z0-9._]+\(/.test(maybeDirective)) {
27
+ tokens.push({ type: "directive", value: maybeDirective });
28
+ i = end;
29
+ continue;
30
+ }
31
+ }
32
+ let next = input.length;
33
+ const exprIndex = input.indexOf("{{", i);
34
+ const dirIndex = input.indexOf("@", i);
35
+ if (exprIndex !== -1) next = Math.min(next, exprIndex);
36
+ if (dirIndex !== -1) next = Math.min(next, dirIndex);
37
+ const value = input.slice(i, next);
38
+ tokens.push({ type: "text", value });
39
+ i = next;
40
+ }
41
+ return tokens.filter((t) => !(t.type === "text" && t.value.length === 0));
42
+ }
43
+
44
+ // src/parser.ts
45
+ function parse(source) {
46
+ const tokens = tokenize(source);
47
+ let index = 0;
48
+ function peek() {
49
+ return tokens[index];
50
+ }
51
+ function next() {
52
+ return tokens[index++];
53
+ }
54
+ function parseBlock(stopDirectives = []) {
55
+ const body = [];
56
+ while (index < tokens.length) {
57
+ const token = peek();
58
+ if (!token) break;
59
+ if (token.type === "directive") {
60
+ const directiveName = token.value.split(/[ (]/)[0];
61
+ if (stopDirectives.includes(directiveName)) break;
62
+ if (directiveName === "@if") {
63
+ body.push(parseIf());
64
+ continue;
65
+ }
66
+ if (directiveName === "@each") {
67
+ body.push(parseEach());
68
+ continue;
69
+ }
70
+ if (directiveName === "@slot") {
71
+ body.push(parseSlot());
72
+ continue;
73
+ }
74
+ if (/^@[a-zA-Z0-9._]+\(/.test(token.value)) {
75
+ body.push(parseComponent());
76
+ continue;
77
+ }
78
+ if (directiveName === "@else" || directiveName === "@end") break;
79
+ }
80
+ next();
81
+ if (token.type === "text") {
82
+ body.push({ type: "Text", value: token.value });
83
+ } else if (token.type === "expr") {
84
+ body.push({ type: "Expression", value: token.value, escaped: token.escaped });
85
+ }
86
+ }
87
+ return body;
88
+ }
89
+ function parseIf() {
90
+ const token = next();
91
+ if (!token || token.type !== "directive") throw new Error("Esperado @if");
92
+ const match = token.value.match(/^@if\((.*)\)$/);
93
+ if (!match) throw new Error(`Diretiva inv\xE1lida: ${token.value}`);
94
+ const consequent = parseBlock(["@else", "@end"]);
95
+ let alternate = [];
96
+ const maybeElse = peek();
97
+ if (maybeElse?.type === "directive" && maybeElse.value.startsWith("@else")) {
98
+ next();
99
+ alternate = parseBlock(["@end"]);
100
+ }
101
+ const end = next();
102
+ if (!end || end.type !== "directive" || !end.value.startsWith("@end")) {
103
+ throw new Error("@if sem @end");
104
+ }
105
+ return { type: "If", test: match[1].trim(), consequent, alternate };
106
+ }
107
+ function parseEach() {
108
+ const token = next();
109
+ if (!token || token.type !== "directive") throw new Error("Esperado @each");
110
+ const match = token.value.match(/^@each\(([a-zA-Z_$][a-zA-Z0-9_$]*)\s+in\s+(.*)\)$/);
111
+ if (!match) throw new Error(`Diretiva inv\xE1lida: ${token.value}`);
112
+ const body = parseBlock(["@end"]);
113
+ const end = next();
114
+ if (!end || end.type !== "directive" || !end.value.startsWith("@end")) {
115
+ throw new Error("@each sem @end");
116
+ }
117
+ return { type: "Each", item: match[1], iterable: match[2].trim(), body };
118
+ }
119
+ function parseComponent() {
120
+ const token = next();
121
+ if (!token || token.type !== "directive") throw new Error("Esperado componente");
122
+ const match = token.value.match(/^@([a-zA-Z0-9._]+)\((.*)\)$/);
123
+ if (!match) throw new Error(`Componente inv\xE1lido: ${token.value}`);
124
+ const children = parseBlock(["@end"]);
125
+ const end = next();
126
+ if (!end || end.type !== "directive" || !end.value.startsWith("@end")) {
127
+ throw new Error(`Componente ${match[1]} sem @end`);
128
+ }
129
+ return { type: "Component", name: match[1], props: match[2].trim() || null, children };
130
+ }
131
+ function parseSlot() {
132
+ const token = next();
133
+ if (!token || token.type !== "directive") throw new Error("Esperado @slot");
134
+ const match = token.value.match(/^@slot\(['"](.+?)['"]\)$/);
135
+ if (!match) throw new Error(`Slot inv\xE1lido: ${token.value}`);
136
+ const children = parseBlock(["@end"]);
137
+ const end = next();
138
+ if (!end || end.type !== "directive" || !end.value.startsWith("@end")) {
139
+ throw new Error(`Slot ${match[1]} sem @end`);
140
+ }
141
+ return { type: "Slot", name: match[1], children };
142
+ }
143
+ return { type: "Template", body: parseBlock() };
144
+ }
145
+ export {
146
+ parse,
147
+ tokenize
148
+ };
@@ -0,0 +1,2 @@
1
+ export * from './tokenizer.js';
2
+ export * from './parser.js';
@@ -0,0 +1,2 @@
1
+ import type { TemplateNode } from '@tekojs/types';
2
+ export declare function parse(source: string): TemplateNode;
@@ -0,0 +1,12 @@
1
+ export type Token = {
2
+ type: 'text';
3
+ value: string;
4
+ } | {
5
+ type: 'expr';
6
+ value: string;
7
+ escaped: boolean;
8
+ } | {
9
+ type: 'directive';
10
+ value: string;
11
+ };
12
+ export declare function tokenize(input: string): Token[];
package/package.json ADDED
@@ -0,0 +1,101 @@
1
+ {
2
+ "name": "@tekojs/core",
3
+ "version": "1.1.1",
4
+ "description": "Core functionalities for the Teko ecosystem",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/src/index.js"
12
+ }
13
+ },
14
+ "engines": {
15
+ "node": ">=18.0.0"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "!dist/tests",
20
+ "!dist/**/*.test.*",
21
+ "dist/**/*.d.ts",
22
+ "dist/**/*.js"
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/tekojs/core.git"
27
+ },
28
+ "homepage": "https://github.com/tekojs/core#readme",
29
+ "bugs": {
30
+ "url": "https://github.com/tekojs/core/issues"
31
+ },
32
+ "scripts": {
33
+ "pretest": "npm run lint",
34
+ "test": "c8 npm run quick:test",
35
+ "lint": "eslint .",
36
+ "format": "prettier --write .",
37
+ "typecheck": "tsc --noEmit",
38
+ "clean": "del-cli dist",
39
+ "precompile": "npm run lint && npm run clean",
40
+ "compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
41
+ "build": "npm run compile",
42
+ "version": "npm run build",
43
+ "prepublishOnly": "npm run build",
44
+ "quick:test": "node --import=ts-node-maintained/register/esm --enable-source-maps bin/test.ts",
45
+ "prepare": "husky",
46
+ "commitlint": "commitlint --edit",
47
+ "commit": "cz"
48
+ },
49
+ "devDependencies": {
50
+ "@arapucajs/tsconfig": "^1.0.0",
51
+ "@commitlint/cli": "^20.5.0",
52
+ "@commitlint/config-conventional": "^20.5.0",
53
+ "@types/node": "^25.5.2",
54
+ "@typescript-eslint/eslint-plugin": "^8.29.0",
55
+ "@typescript-eslint/parser": "^8.29.0",
56
+ "c8": "^11.0.0",
57
+ "commitizen": "^4.3.1",
58
+ "cz-conventional-changelog": "^3.3.0",
59
+ "del-cli": "^7.0.0",
60
+ "eslint": "^9.23.0",
61
+ "eslint-config-prettier": "^10.1.1",
62
+ "husky": "^9.1.7",
63
+ "prettier": "^3.5.2",
64
+ "qs": "^6.15.0",
65
+ "tsdown": "^0.21.7",
66
+ "tsup": "^8.5.1",
67
+ "typescript": "^6.0.2"
68
+ },
69
+ "config": {
70
+ "commitizen": {
71
+ "path": "./node_modules/cz-conventional-changelog"
72
+ }
73
+ },
74
+ "keywords": [
75
+ "teko",
76
+ "ssr",
77
+ "templates",
78
+ "frontend"
79
+ ],
80
+ "author": "Jefte Costa <jefteamorim@gmail.com>",
81
+ "license": "MIT",
82
+ "publishConfig": {
83
+ "access": "public",
84
+ "provenance": true
85
+ },
86
+ "tsup": {
87
+ "entry": [
88
+ "./index.ts",
89
+ "./src/types.ts"
90
+ ],
91
+ "outDir": "./dist",
92
+ "clean": true,
93
+ "format": "esm",
94
+ "dts": false,
95
+ "sourcemap": false,
96
+ "target": "esnext"
97
+ },
98
+ "dependencies": {
99
+ "@tekojs/types": "^1.2.0"
100
+ }
101
+ }