@slim-lang/core 1.2.12 → 1.2.13

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.
Files changed (48) hide show
  1. package/README.md +522 -14
  2. package/package.json +2 -6
  3. package/packages/slim/server/main.slim +433 -422
  4. package/run-dev-slim.js +10 -5
  5. package/run-slim.js +10 -20
  6. package/src/bin/api/github_get.js +7 -12
  7. package/src/bin/api/github_req.js +54 -241
  8. package/src/bin/api/lock.js +2 -1
  9. package/src/bin/api/spm.js +20 -91
  10. package/src/bin/cli.js +73 -102
  11. package/src/bin/config.default.json +11 -11
  12. package/src/bin/helpers.js +2 -7
  13. package/src/bin/spm.js +3 -7
  14. package/src/checker.js +1705 -388
  15. package/src/compile.js +131 -100
  16. package/src/external/classErrors.js +5 -6
  17. package/src/external/client.js +0 -2
  18. package/src/external/core.js +355 -237
  19. package/src/external/defaults.js +12 -4
  20. package/src/external/dom.js +663 -0
  21. package/src/external/helpers.js +87 -58
  22. package/src/external/reactive.js +601 -0
  23. package/src/external/router.js +117 -0
  24. package/src/external/slim-globals.d.ts +43 -4
  25. package/src/external/typeSyntax.js +258 -0
  26. package/src/external/types.js +1 -1
  27. package/src/format.js +183 -12
  28. package/src/handlers/errorHandler.js +9 -36
  29. package/src/jsdoc.js +36 -16
  30. package/src/modulePaths.js +15 -5
  31. package/src/reactivity.js +774 -0
  32. package/src/repl.js +1 -1
  33. package/src/suggest.js +60 -0
  34. package/src/syntax/babel.js +46 -0
  35. package/src/syntax/index.js +39 -0
  36. package/src/syntax/lower.js +666 -0
  37. package/src/syntax/markup.js +195 -0
  38. package/src/syntax/plugin.js +903 -0
  39. package/src/syntax/types.js +106 -0
  40. package/src/test-runner.js +5 -5
  41. package/src/transform.js +209 -244
  42. package/packages/slim/helpers/string.js +0 -43
  43. package/packages/slim/types/hash.js +0 -11
  44. package/src/handlers/parser/components.js +0 -316
  45. package/src/handlers/parserHandler.js +0 -789
  46. package/src/lexer.js +0 -174
  47. package/src/parser.js +0 -803
  48. package/src/sourcemap.js +0 -0
@@ -1,43 +0,0 @@
1
- import "./C:/Users/zeral/AppData/Local/Temp/claude/D--dev-slim-github/72243d74-23bd-4f88-a955-b7ed76b7e749/scratchpad/myapp/dist/external/core.js";
2
- import * as crypto from "node:crypto";
3
- import { SHA256, QuickHash } from "../types/hash.js";
4
- let _slimType = {};
5
- export const StringHelper = __typed_variable__({
6
- len(str) {
7
- let _slimType2 = {};
8
- __typed_parameter__(str, __type_spec__(__type_ref__("string")), _slimType2, "str", false, `function "len": argument<0> "str" expected string, got ${type(str)}`);
9
- if (type(str) == "string") return __typed_return__(str.length, __type_spec__(__type_ref__("int")), "len");
10
- },
11
- truncate(str, maxLength) {
12
- let _slimType3 = {},
13
- _slimType4 = {};
14
- __typed_parameter__(str, __type_spec__(__type_ref__("string")), _slimType3, "str", false, `function "truncate": argument<0> "str" expected string, got ${type(str)}`);
15
- __typed_parameter__(maxLength, __type_spec__(__type_ref__("int")), _slimType4, "maxLength", false, `function "truncate": argument<1> "maxLength" expected int, got ${type(maxLength)}`);
16
- if (str.length > maxLength) {
17
- return __typed_return__(str.slice(0, maxLength) + '...', __type_spec__(__type_ref__("string")), "truncate");
18
- }
19
- return __typed_return__(str, __type_spec__(__type_ref__("string")), "truncate");
20
- },
21
- hash(str) {
22
- let _slimType5 = {};
23
- __typed_parameter__(str, __type_spec__(__type_ref__("string")), _slimType5, "str", false, `function "hash": argument<0> "str" expected string, got ${type(str)}`);
24
- return __typed_return__(crypto.createHash("sha256").update(str).digest("hex"), __type_spec__(__type_ref__("SHA256", () => SHA256)), "hash");
25
- },
26
- quickHash(str) {
27
- let _slimType6 = {};
28
- __typed_parameter__(str, __type_spec__(__type_ref__("string")), _slimType6, "str", false, `function "quickHash": argument<0> "str" expected string, got ${type(str)}`);
29
- let hash = 0;
30
- for (let i = 0; i < str.length; i++) {
31
- hash = (hash << 5) - hash + str.charCodeAt(i);
32
- hash |= 0;
33
- }
34
- return __typed_return__(hash, __type_spec__(__type_ref__("QuickHash", () => QuickHash)), "quickHash");
35
- }
36
- }, __type_spec__(__type_ref__("object")), _slimType, "StringHelper");
37
- export { StringHelper as default };
38
- try {
39
- __lock_object__(StringHelper);
40
- } catch (__err__) {
41
- __handle_sync_error__(__err__);
42
- }
43
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6Ijs7OztBQUdBLGFBQUFBLFlBQUEsR0FBQUMsa0JBQUEsQ0FBNEM7RUFDNUNDLElBQUFDLEdBQUE7SUFBQSxJQUFBQyxVQUFBO0lBQUFDLG1CQUFBLENBQUFGLEdBQUEsRUFBQUcsYUFBQSxDQUFBQyxZQUFBLGFBQUFILFVBQUEsMEVBQUFJLElBQUEsQ0FBQUwsR0FBQTtJQUNBLElBQUFLLElBQUEsQ0FBQUwsR0FBQSxDQUFxQixxQkFBQU0sZ0JBQUEsQ0FBQU4sR0FBQSxDQUFBTyxNQUFBLEVBQUFKLGFBQUEsQ0FBQUMsWUFBQTtFQUNyQjtFQUNBSSxTQUFBUixHQUFBLEVBQUFTLFNBQUE7SUFBQSxJQUFBQyxVQUFBO01BQUFDLFVBQUE7SUFBQVQsbUJBQUEsQ0FBQUYsR0FBQSxFQUFBRyxhQUFBLENBQUFDLFlBQUEsYUFBQU0sVUFBQSwrRUFBQUwsSUFBQSxDQUFBTCxHQUFBO0lBQUFFLG1CQUFBLENBQUFPLFNBQUEsRUFBQU4sYUFBQSxDQUFBQyxZQUFBLFVBQUFPLFVBQUEsd0ZBQUFOLElBQUEsQ0FBQUksU0FBQTtJQUNBLElBQUFULEdBQUEsQ0FBQU8sTUFBQSxHQUFBRSxTQUFBO01BQ0EsT0FBQUgsZ0JBQUEsQ0FBQU4sR0FBQSxDQUFBWSxLQUFBLElBQUFILFNBQUEsV0FBQU4sYUFBQSxDQUFBQyxZQUFBO0lBQ0E7SUFDQSxPQUFBRSxnQkFBQSxDQUFBTixHQUFBLEVBQUFHLGFBQUE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ggPiBtYXhMZW5ndGgpIHtcclxuICAgICAgICAgICAgcmV0dXJuIHN0ci5zbGljZSgwLCBtYXhMZW5ndGgpICsgJy4uLic7XHJcbiAgICAgICAgfVxyXG4gICAgICAgIHJldHVybiBzdHI7XHJcbiAgICB9LFxyXG4gICAgaGFzaChzdHI6IHN0cmluZyk6IFNIQTI1NiB7XHJcbiAgICAgICAgcmV0dXJuIGNyeXB0by5jcmVhdGVIYXNoKFwic2hhMjU2XCIpLnVwZGF0ZShzdHIpLmRpZ2VzdChcImhleFwiKVxyXG4gICAgfSxcclxuICAgIHF1aWNrSGFzaChzdHI6IHN0cmluZyk6IFF1aWNrSGFzaCB7XHJcbiAgICAgICAgbGV0IGhhc2ggPSAwO1xyXG4gICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgc3RyLmxlbmd0aDsgaSsrKSB7XHJcbiAgICAgICAgICAgIGhhc2ggPSAoaGFzaCA8PCA1KSAtIGhhc2ggKyBzdHIuY2hhckNvZGVBdChpKTtcclxuICAgICAgICAgICAgaGFzaCB8PSAwO1xyXG4gICAgICAgIH1cclxuICAgICAgICByZXR1cm4gaGFzaDtcclxuICAgIH1cclxufVxyXG5cclxubG9jayBTdHJpbmdIZWxwZXI7Il19
@@ -1,11 +0,0 @@
1
- import "./C:/Users/zeral/AppData/Local/Temp/claude/D--dev-slim-github/72243d74-23bd-4f88-a955-b7ed76b7e749/scratchpad/myapp/dist/external/core.js";
2
- export const SHA256 = __type_def__("SHA256", v => {
3
- return __sizeof__(v) == 64;
4
- }, {});
5
- export const QuickHash = __type_def__("QuickHash", v => {
6
- return __sizeof__(v) == 8;
7
- }, {});
8
- try {} catch (__err__) {
9
- __handle_sync_error__(__err__);
10
- }
11
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6IjtBQUFBLGFBQUFBLE1BQUEsR0FBQUMsWUFBQSxXQUFBQyxDQUFBO0VBQUEsT0FBQUMsVUFBQSxDQUFBRCxDQUFBO0FBQUEsTUFFQztBQUNELGFBQUFFLFNBQUEsR0FBQUgsWUFBQSxjQUFBQyxDQUFBO0VBQUEsT0FBQUMsVUFBQSxDQUFBRCxDQUFBO0FBQUE7QUFBQSxjQUFBRyxPQUFBO0VBQUFDLHFCQUFBLENBQUFELE9BQUE7QUFBQSIsIm5hbWVzIjpbIlNIQTI1NiIsIl9fdHlwZV9kZWZfXyIsInYiLCJfX3NpemVvZl9fIiwiUXVpY2tIYXNoIiwiX19lcnJfXyIsIl9faGFuZGxlX3N5bmNfZXJyb3JfXyJdLCJpZ25vcmVMaXN0IjpbXSwic291cmNlcyI6WyJEOlxcZGV2XFxzbGltLWdpdGh1YlxccGFja2FnZXNcXHNsaW1cXHR5cGVzXFxoYXNoLnNsaW0iXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IHR5cGUgU0hBMjU2KHYpIHtcclxuICAgIHJldHVybiBzaXplb2YgdiA9PSA2NFxyXG59XHJcbmV4cG9ydCB0eXBlIFF1aWNrSGFzaCh2KSB7XHJcbiAgICByZXR1cm4gc2l6ZW9mIHYgPT0gOFxyXG59Il19
@@ -1,316 +0,0 @@
1
- import fs from "node:fs"
2
- import path from "node:path"
3
- import { tokenize } from "../../lexer.js"
4
- import { parseTypedArgs, buildTypedArgsResult } from "../parserHandler.js"
5
-
6
- let bemCache = null
7
- function useBEMClasses() {
8
- if (bemCache !== null) return bemCache
9
- try {
10
- const cfg = JSON.parse(fs.readFileSync(path.join(process.cwd(), "slimconfig.json"), "utf8"))
11
- bemCache = !!(cfg.components && cfg.components.useBEMClasses === true)
12
- } catch {
13
- bemCache = false
14
- }
15
- return bemCache
16
- }
17
-
18
- function kebabCase(name) {
19
- return name
20
- .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
21
- .replace(/[_\s]+/g, "-")
22
- .toLowerCase()
23
- }
24
-
25
- function elementExpr(html, name) {
26
- if (!useBEMClasses()) return `htmlToVdom(__html__\`${html}\`).toElement()`
27
- return `(() => { const __v__ = htmlToVdom(__html__\`${html}\`); __resolve_bem__(__v__, ${JSON.stringify(kebabCase(name))}); return __v__.toElement(); })()`
28
- }
29
-
30
- const OPENERS = new Set(["(", "[", "{", "${"])
31
- const CLOSERS = new Set([")", "]", "}"])
32
-
33
- function templateReturnStart(body) {
34
- let depth = 0
35
- for (const t of tokenize(body)) {
36
- if (t.type === "punct") {
37
- if (OPENERS.has(t.value)) depth++
38
- else if (CLOSERS.has(t.value)) depth--
39
- } else if (depth === 0 && t.type === "name" && t.keyword && t.value === "return") {
40
- return t.start
41
- }
42
- }
43
- return -1
44
- }
45
-
46
- function literalRanges(code) {
47
- const tokens = tokenize(code);
48
- const literal = t => t.type === "string" || t.type === "template" || t.type === "comment" || t.type === "regex";
49
-
50
- return offset => {
51
- let lo = 0, hi = tokens.length - 1;
52
- while (lo <= hi) {
53
- const mid = (lo + hi) >> 1;
54
- const t = tokens[mid];
55
- if (offset < t.start) hi = mid - 1;
56
- else if (offset >= t.end) lo = mid + 1;
57
- else return literal(t);
58
- }
59
- return false;
60
- };
61
- }
62
-
63
- function parseComponentsEdits(code) {
64
- const edits = [];
65
- const inLiteral = literalRanges(code);
66
- let i = 0;
67
-
68
- while (i < code.length) {
69
- const match = code.slice(i).match(
70
- /\b(?:(isolated|element)(?:\s*\(\s*["']([^"']+)["']\s*\))?\s+)?component\b/
71
- );
72
-
73
- if (!match) break;
74
-
75
- const modifier = match[1] ?? null;
76
- const tag = match[2] ?? null;
77
- const start = i + match.index;
78
-
79
- if (inLiteral(start + match[0].length - "component".length)) {
80
- i = start + match[0].length;
81
- continue;
82
- }
83
-
84
- let p = start + match[0].length;
85
-
86
- while (/\s/.test(code[p])) p++;
87
-
88
- const nameStart = p;
89
- while (/[a-zA-Z0-9_$]/.test(code[p])) p++;
90
- const name = code.slice(nameStart, p);
91
-
92
- while (/\s/.test(code[p])) p++;
93
-
94
- if (code[p] !== "(")
95
- throw new Error(`Expected "(" after component ${name}`);
96
-
97
- let depth = 1;
98
- const argsStart = ++p;
99
-
100
- while (depth) {
101
- if (code[p] === "(") depth++;
102
- else if (code[p] === ")") depth--;
103
- p++;
104
- }
105
-
106
- const args = code.slice(argsStart, p - 1).trim();
107
-
108
- while (/\s/.test(code[p])) p++;
109
-
110
- if (code[p] !== "{")
111
- throw new Error(`Expected "{" after component ${name}`);
112
-
113
- depth = 1;
114
- const bodyStart = ++p;
115
-
116
- while (depth) {
117
- if (code[p] === "{") depth++;
118
- else if (code[p] === "}") depth--;
119
- p++;
120
- }
121
-
122
- const body = code.slice(bodyStart, p - 1);
123
-
124
- edits.push({ start, end: p, replacement: buildComponent(name, args, body, modifier, tag) });
125
-
126
- i = p;
127
- }
128
-
129
- return edits;
130
- }
131
-
132
- function parseComponents(code) {
133
- const edits = parseComponentsEdits(code);
134
- let out = "";
135
- let cursor = 0;
136
- for (const { start, end, replacement } of edits) {
137
- out += code.slice(cursor, start) + replacement;
138
- cursor = end;
139
- }
140
- return out + code.slice(cursor);
141
- }
142
-
143
- function parseComponentArgs(name, args) {
144
- const trimmed = args.trim();
145
-
146
- if (!trimmed) return { binding: "", checks: "" };
147
-
148
- let parsed, binding;
149
-
150
- if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
151
- parsed = parseTypedArgs(trimmed.slice(1, -1));
152
- const fields = parsed.map(a => a.default !== null ? `${a.name} = ${a.default}` : a.name);
153
- binding = `{ ${fields.join(", ")} }`;
154
- } else {
155
- parsed = parseTypedArgs(trimmed);
156
-
157
- if (parsed.length !== 1 || !/^[A-Za-z_$][\w$]*$/.test(parsed[0].name)) {
158
- throw new Error(
159
- `Component "${name}" arguments must be a single object name (e.g. "props" or "props: SomeStruct") or a destructuring pattern (e.g. "{ id: int }"), got: "${trimmed}"`
160
- );
161
- }
162
-
163
- binding = parsed[0].name;
164
- }
165
-
166
- const { checks } = buildTypedArgsResult(parsed, name);
167
- return { binding, checks };
168
- }
169
-
170
- function escapeTemplateBackticks(s) {
171
- const n = s.length;
172
- let out = "";
173
- let i = 0;
174
- let depth = 0;
175
- while (i < n) {
176
- const c = s[i];
177
- if (c === "\\") { out += c + (s[i + 1] ?? ""); i += 2; continue; }
178
- if (depth === 0) {
179
- if (c === "`") { out += "\\`"; i++; continue; }
180
- if (c === "$" && s[i + 1] === "{") { out += "${"; i += 2; depth = 1; continue; }
181
- out += c; i++; continue;
182
- }
183
- if (c === "'" || c === "\"" || c === "`") {
184
- const q = c;
185
- out += c; i++;
186
- while (i < n) {
187
- if (s[i] === "\\") { out += s[i] + (s[i + 1] ?? ""); i += 2; continue; }
188
- out += s[i];
189
- if (s[i] === q) { i++; break; }
190
- i++;
191
- }
192
- continue;
193
- }
194
- if (c === "{") depth++;
195
- else if (c === "}") depth--;
196
- out += c; i++;
197
- }
198
- return out;
199
- }
200
-
201
- // Explicit tags need a hyphen; implicit tags use the slim- kebab-case prefix.
202
- function elementTag(name, explicit) {
203
- if (explicit) {
204
- if (!explicit.includes("-")) {
205
- throw new Error(
206
- `Component "${name}": a custom element tag must contain a hyphen, got "${explicit}"`
207
- );
208
- }
209
- return explicit;
210
- }
211
-
212
- const kebab = name
213
- .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
214
- .replace(/[_\s]+/g, "-")
215
- .toLowerCase();
216
-
217
- return `slim-${kebab}`;
218
- }
219
-
220
- function buildElementComponent(name, binding, before, html, explicitTag, checks = "") {
221
- const tag = elementTag(name, explicitTag);
222
- const param = binding ? `${binding} = {}` : "__unused__ = {}";
223
- const checkPrelude = checks ? `${checks};\n` : "";
224
- const quoted = JSON.stringify(tag);
225
-
226
- return `
227
- const ${name} = (__props__ = {}) => ${name}.__render__(__props__);
228
- ${name}.__component__ = true
229
- ${name}.tag = ${quoted}
230
- ${name}.__render__ = (${param}, __host__ = null) => {
231
- const s = {};
232
- const __mounts__ = [];
233
- const __connects__ = [];
234
- const __unmounts__ = [];
235
- const onMount = (fn) => __mounts__.push(fn);
236
- const onConnect = (fn) => __connects__.push(fn);
237
- const onUnmount = (fn) => __unmounts__.push(fn);
238
- ${checkPrelude}${before};
239
- const __el__ = ${elementExpr(html, name)};
240
-
241
- if (!__host__) {
242
- for (const fn of __mounts__) fn(__el__);
243
- __lifecycle__(__el__, __connects__, __unmounts__);
244
- return __el__;
245
- }
246
-
247
- __adopt_into__(__host__, __el__);
248
- for (const fn of __mounts__) fn(__host__);
249
- return { connects: __connects__, unmounts: __unmounts__ };
250
- };
251
- __define_element__(${quoted}, ${name}.__render__)
252
- `;
253
- }
254
-
255
- function buildComponent(name, args, body, modifier = null, tag = null) {
256
- const returnStart = templateReturnStart(body);
257
-
258
- if (returnStart === -1) {
259
- throw new Error(`Component "${name}" must contain return`);
260
- }
261
-
262
- const before = body.slice(0, returnStart).trim();
263
-
264
- let template = body.slice(returnStart + "return".length).trim();
265
- if (template.startsWith("(") && template.endsWith(")")) {
266
- template = template.slice(1, -1).trim();
267
- }
268
- const html = escapeTemplateBackticks(template);
269
- const { binding, checks } = parseComponentArgs(name, args);
270
- const param = binding ? `${binding} = {}` : "";
271
- const checkPrelude = checks ? `${checks};\n` : "";
272
-
273
- if (modifier === "element") {
274
- return buildElementComponent(name, binding, before, html, tag, checks);
275
- }
276
-
277
- const isolated = modifier === "isolated";
278
-
279
- if(!isolated) {
280
- return `
281
- const ${name} = (${param}) => {
282
- const s = {};
283
- const __mounts__ = [];
284
- const __connects__ = [];
285
- const __unmounts__ = [];
286
- const onMount = (fn) => __mounts__.push(fn);
287
- const onConnect = (fn) => __connects__.push(fn);
288
- const onUnmount = (fn) => __unmounts__.push(fn);
289
- ${checkPrelude}${before};
290
- const __el__ = ${elementExpr(html, name)};
291
- for (const fn of __mounts__) fn(__el__);
292
- __lifecycle__(__el__, __connects__, __unmounts__);
293
- return __el__;
294
- };
295
- ${name}.__component__ = true
296
- `;
297
- }
298
- else {
299
- const fnBody = `const __mounts__=[];const __connects__=[];const __unmounts__=[];const onMount=(fn)=>__mounts__.push(fn);const onConnect=(fn)=>__connects__.push(fn);const onUnmount=(fn)=>__unmounts__.push(fn);${before}; const __el__ = ${elementExpr(html, name)}; for (const fn of __mounts__) fn(__el__); __lifecycle__(__el__, __connects__, __unmounts__); return __el__;`;
300
-
301
- const guard = checks ? `const ${binding} = __props__;\n${checks};\n` : "";
302
-
303
- return `
304
- const ${name} = (__props__ = {}) => {
305
- ${guard}return new Function(${JSON.stringify(binding)}, ${JSON.stringify(fnBody)})(__props__);
306
- };
307
- ${name}.__component__ = true
308
- `;
309
- }
310
- }
311
-
312
- export {
313
- parseComponents,
314
- parseComponentsEdits,
315
- buildComponent
316
- }