@slim-lang/core 1.2.11 → 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 -295
  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
package/src/compile.js CHANGED
@@ -1,21 +1,70 @@
1
1
  import "./handlers/errorHandler.js"
2
- import fs from "fs"
3
- import path from "path"
2
+ import fs from "node:fs"
3
+ import path from "node:path"
4
+ import { createHash } from "node:crypto"
4
5
  import { transform } from "./transform.js"
5
-
6
- import { readFile } from 'fs/promises';
7
- import { Debug } from "./external/core.js";
8
- import { stripComments } from "./parser.js";
9
- import { UseError } from "./external/classErrors.js";
10
- import { getDistPath, resolveSlimSource, PACKAGE_ROOT, distDirName } from "./modulePaths.js";
6
+ import { exportModuleTypes, importModuleTypes } from "./checker.js"
7
+ import { reactiveExports } from "./reactivity.js"
8
+ import { findUses } from "./syntax/index.js"
9
+ import { UseError } from "./external/classErrors.js"
10
+ import { getDistPath, setOutputPath, resolveSlimSource, PACKAGE_ROOT, distDirName, readJson } from "./modulePaths.js"
11
11
 
12
12
  const compiled = new Set()
13
+ const compiling = []
14
+
15
+
16
+ const CACHE_FILE = () => path.resolve(distDirName(), ".slim-cache.json")
17
+ const hash = text => createHash("sha1").update(text).digest("hex")
18
+
19
+ let cache = { key: null, files: {} }
20
+ let nextCache = { key: null, files: {} }
21
+ const changedTypes = new Set()
22
+ let reused = 0
23
+
24
+ function compilerFingerprint(config) {
25
+ const parts = [JSON.stringify(config), JSON.stringify({ jsdoc, declarations, check, release, useStyle, usePackages })]
26
+ const walk = dir => {
27
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
28
+ const full = path.join(dir, entry.name)
29
+ if (entry.isDirectory()) walk(full)
30
+ else if (entry.name.endsWith(".js")) {
31
+ const { mtimeMs, size } = fs.statSync(full)
32
+ parts.push(`${full}:${mtimeMs}:${size}`)
33
+ }
34
+ }
35
+ }
36
+ walk(path.join(PACKAGE_ROOT, "src"))
37
+ return hash(parts.join("\n"))
38
+ }
39
+
40
+ function loadCache(key) {
41
+ nextCache = { key, files: {} }
42
+ if (process.env.SLIM_FORCE === "1") return
43
+ try {
44
+ const saved = JSON.parse(fs.readFileSync(CACHE_FILE(), "utf8"))
45
+ if (saved.key === key) cache = saved
46
+ } catch {
47
+ }
48
+ }
49
+
50
+ function saveCache() {
51
+ fs.mkdirSync(path.dirname(CACHE_FILE()), { recursive: true })
52
+ fs.writeFileSync(CACHE_FILE(), JSON.stringify(nextCache))
53
+ }
13
54
 
14
55
  let usePackages = true
15
56
  let jsdoc = false
16
57
  let declarations = false
17
58
  let check = true
18
59
  let useStyle = "import"
60
+ let release = false
61
+
62
+ const debug = (...args) => console.log("[SLIM]", ...args)
63
+
64
+ function fail(error) {
65
+ console.error(`\n${error.tag ?? error.name}: ${error.message}\n`)
66
+ process.exit(1)
67
+ }
19
68
 
20
69
  function syncExternal() {
21
70
  const srcExternal = path.join(PACKAGE_ROOT, "src/external")
@@ -35,145 +84,121 @@ function syncExternal() {
35
84
  continue
36
85
  }
37
86
 
38
- if (fs.existsSync(distFull)) {
39
- const srcMtime = fs.statSync(srcFull).mtimeMs
40
- const distMtime = fs.statSync(distFull).mtimeMs
41
- if (srcMtime <= distMtime) continue
42
- }
87
+ if (fs.existsSync(distFull) && fs.statSync(srcFull).mtimeMs <= fs.statSync(distFull).mtimeMs) continue
43
88
 
44
89
  fs.copyFileSync(srcFull, distFull)
45
- Debug.log(`Synced: ${path.relative(".", distFull)}`)
90
+ debug(`Synced: ${path.relative(".", distFull)}`)
46
91
  }
47
92
  }
48
93
 
49
94
  syncDir(srcExternal, distExternal)
50
95
  }
51
96
 
52
- function extractUses(code) {
53
- code = stripComments(code)
54
- const uses = []
55
-
56
- const patterns = [
57
- /\buse\s+(@[\w$\/.-]+)\s*;?$/gm,
58
- /\buse\s+(?:\{[^}]+\}|\*\s+as\s+[\w$]+|[\w$]+\s+as\s+[\w$]+|[\w$]+)\s+from\s+(@[\w$\/.-]+)\s*;?$/gm,
59
- /\buse\s+(?:\{[^}]+\}|\*\s+as\s+[\w$]+|[\w$]+\s+as\s+[\w$]+|[\w$]+)\s+from\s+["']([^"']+)["']\s*;?$/gm,
60
- /\buse\s+["']([^"']+)["']\s*;?$/gm,
61
- ]
62
-
63
- for (const pattern of patterns) {
64
- let match
65
- while ((match = pattern.exec(code)) !== null) {
66
- const raw = match[1]
67
- if (raw) uses.push(raw)
68
- }
69
- }
70
-
71
- return [...new Set(uses)]
72
- }
73
-
74
- function compileFile(slimFile, isEntry = false, mainEntry = null) {
75
- const hasSilmExtension = slimFile.endsWith(".slim")
76
- const file = hasSilmExtension ? slimFile : slimFile + ".slim"
77
- const abs = path.resolve(file)
97
+ function compileFile(slimFile) {
98
+ const abs = path.resolve(slimFile.endsWith(".slim") ? slimFile : slimFile + ".slim")
78
99
 
79
100
  if (compiled.has(abs)) return
80
101
  compiled.add(abs)
81
102
 
82
- if (!fs.existsSync(abs)) {
83
- console.error(`\nError: File not found: ${abs}\n`)
84
- process.exit(1)
85
- }
103
+ if (!fs.existsSync(abs)) fail(new Error(`File not found: ${abs}`))
86
104
 
87
105
  const code = fs.readFileSync(abs, "utf8")
88
-
89
- const uses = extractUses(code)
106
+ const uses = [...new Set(findUses(code).map(use => use.source))]
90
107
 
91
108
  if (!usePackages && uses.length > 0) {
92
- const err = new UseError(
109
+ fail(new UseError(
93
110
  `The "use" feature is disabled. Set "uses": { "enabled": true } in slimconfig.json to enable imports (in ${abs}, found: use ${uses[0]})`
94
- )
95
- console.error(err)
96
- process.exit(1)
111
+ ))
97
112
  }
98
113
 
114
+ const deps = []
115
+ let inCycle = false
116
+
117
+ compiling.push(abs)
99
118
  for (const raw of uses) {
100
119
  const depPath = resolveSlimSource(raw, abs)
101
-
102
120
  if (depPath === null) continue
103
121
 
104
- if (!fs.existsSync(depPath)) {
105
- const err = new UseError(`No lib/file founded by the path: ${raw}`)
106
- console.error(err)
107
- process.exit(1)
108
- }
122
+ if (!fs.existsSync(depPath)) fail(new UseError(`Nothing found for "use ${raw}" (looked for ${depPath})`))
123
+ if (depPath === abs) fail(new UseError(`${abs} uses itself`))
109
124
 
110
- if (depPath === abs) {
111
- console.error(`\nError: Circular dependency detected in ${abs}\n`)
112
- process.exit(1)
125
+ const cycleStart = compiling.indexOf(depPath)
126
+ if (cycleStart !== -1) {
127
+ const chain = [...compiling.slice(cycleStart), depPath].map(file => path.relative(".", file))
128
+ console.warn(`[SLIM] Warning: circular use ${chain.join(" -> ")}`)
129
+ inCycle = true
130
+ continue
113
131
  }
114
- compileFile(depPath, false, mainEntry)
115
- }
116
132
 
117
- const { code: output, declarations: dts } = transform(code, abs, { jsdoc, declarations, check, uses: useStyle })
133
+ compileFile(depPath)
134
+ deps.push(depPath)
135
+ }
136
+ compiling.pop()
137
+
138
+ const outputPath = getDistPath(abs)
139
+ const sourceHash = hash(code)
140
+ const saved = cache.files[abs]
141
+ const upToDate = saved && !inCycle && saved.hash === sourceHash && saved.output === outputPath &&
142
+ fs.existsSync(outputPath) && deps.every(dep => !changedTypes.has(dep))
143
+
144
+ if (upToDate) {
145
+ importModuleTypes(abs, saved.types)
146
+ nextCache.files[abs] = saved
147
+ reused++
148
+ return
149
+ }
118
150
 
119
- const outputPath = isEntry
120
- ? path.resolve(distDirName(), `${mainEntry}.js`)
121
- : getDistPath(abs)
151
+ const { code: output, declarations: dts } = transform(code, abs, {
152
+ jsdoc, declarations, check, release, uses: useStyle, outFile: outputPath
153
+ })
122
154
 
123
155
  fs.mkdirSync(path.dirname(outputPath), { recursive: true })
124
156
  fs.writeFileSync(outputPath, output)
125
157
 
126
158
  if (dts) fs.writeFileSync(outputPath.replace(/\.js$/, ".d.ts"), dts)
159
+
160
+ const types = exportModuleTypes(abs)
161
+ const reactive = JSON.stringify([...reactiveExports(abs)])
162
+ if (!saved || saved.types !== types || saved.reactive !== reactive) changedTypes.add(abs)
163
+ nextCache.files[abs] = { hash: sourceHash, output: outputPath, types, reactive }
127
164
  }
128
165
 
129
- function cleanDist(slimFileClear) {
130
- const keep = new Set([
131
- path.resolve(distDirName(), `${slimFileClear}.js`),
132
- path.resolve(distDirName(), "mappings.json"),
133
- ])
166
+ function cleanDist() {
167
+ const keep = new Set([path.resolve(distDirName(), "mappings.json"), CACHE_FILE()])
134
168
 
135
- function addDirToKeep(dir) {
169
+ function keepDir(dir) {
136
170
  if (!fs.existsSync(dir)) return
137
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
138
- const full = path.resolve(dir, entry.name)
171
+ for (const item of fs.readdirSync(dir, { withFileTypes: true })) {
172
+ const full = path.resolve(dir, item.name)
139
173
  keep.add(full)
140
- if (entry.isDirectory()) addDirToKeep(full)
174
+ if (item.isDirectory()) keepDir(full)
141
175
  }
142
176
  }
143
- addDirToKeep(path.resolve(distDirName(), "external"))
177
+ keepDir(path.resolve(distDirName(), "external"))
144
178
 
145
- for (const slimFile of compiled) {
146
- if (slimFile === path.resolve(`${slimFileClear}.slim`)) {
147
- keep.add(path.resolve(distDirName(), `${slimFileClear}.js`))
148
- } else {
149
- keep.add(getDistPath(slimFile))
150
- }
151
- }
179
+ for (const slimFile of compiled) keep.add(getDistPath(slimFile))
152
180
 
153
181
  for (const kept of [...keep]) {
154
182
  if (kept.endsWith(".js")) keep.add(kept.replace(/\.js$/, ".d.ts"))
155
183
  }
156
184
 
157
- function walkAndClean(dir) {
185
+ function clean(dir) {
158
186
  if (!fs.existsSync(dir)) return
159
187
 
160
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
161
- const full = path.resolve(dir, entry.name)
188
+ for (const item of fs.readdirSync(dir, { withFileTypes: true })) {
189
+ const full = path.resolve(dir, item.name)
162
190
 
163
- if (entry.isDirectory()) {
164
- walkAndClean(full)
165
-
166
- if (fs.readdirSync(full).length === 0) {
167
- fs.rmdirSync(full)
168
- }
191
+ if (item.isDirectory()) {
192
+ clean(full)
193
+ if (fs.readdirSync(full).length === 0) fs.rmdirSync(full)
169
194
  } else if (!keep.has(full)) {
170
195
  fs.rmSync(full)
171
- Debug.log(`Cleaned: ${path.relative(".", full)}`)
196
+ debug(`Cleaned: ${path.relative(".", full)}`)
172
197
  }
173
198
  }
174
199
  }
175
200
 
176
- walkAndClean(path.resolve(distDirName()))
201
+ clean(path.resolve(distDirName()))
177
202
  }
178
203
 
179
204
  function writeJsConfig() {
@@ -195,17 +220,16 @@ function writeJsConfig() {
195
220
  }
196
221
 
197
222
  fs.writeFileSync(configPath, JSON.stringify(config, null, 4) + "\n")
198
- Debug.log("Created jsconfig.json for TypeScript checking")
223
+ debug("Created jsconfig.json for TypeScript checking")
199
224
  }
200
225
 
201
- async function main() {
226
+ function main() {
202
227
  let data
203
228
 
204
229
  try {
205
- const contents = await readFile("slimconfig.json", "utf8")
206
- data = JSON.parse(contents)
230
+ data = readJson("slimconfig.json")
207
231
  } catch (error) {
208
- console.error("Error reading or parsing slimconfig.json:", error)
232
+ console.error("Error reading or parsing slimconfig.json:", error.message)
209
233
  process.exit(1)
210
234
  }
211
235
 
@@ -217,17 +241,24 @@ async function main() {
217
241
  jsdoc = data.jsdoc === true
218
242
  declarations = data.declarations === true
219
243
  check = data.check !== false && process.env.SLIM_NO_CHECK !== "1"
244
+ release = data.release === true || process.env.SLIM_RELEASE === "1"
220
245
 
221
246
  if (!("main" in data)) return
222
247
 
223
248
  try {
224
249
  syncExternal()
225
- compileFile(data.main, true, data.main)
226
- cleanDist(data.main)
250
+ loadCache(compilerFingerprint(data))
251
+ const entry = data.main.endsWith(".slim") ? data.main : `${data.main}.slim`
252
+ setOutputPath(entry, path.resolve(distDirName(), `${data.main}.js`))
253
+ compileFile(entry)
254
+ cleanDist()
255
+ saveCache()
256
+ if (reused > 0) debug(`Up to date: ${reused} of ${compiled.size} files`)
227
257
  if (jsdoc) writeJsConfig()
228
258
  } catch (error) {
229
259
  console.error(`\n${error.slimTypeErrors || error.slimSyntaxError ? error.message : error}\n`)
230
260
  process.exit(1)
231
261
  }
232
262
  }
263
+
233
264
  main()
@@ -1,4 +1,3 @@
1
- // Source frames require a host-provided file reader.
2
1
  let readSource = null
3
2
 
4
3
  export function setSourceReader(reader) {
@@ -41,7 +40,7 @@ function isInternalFrame(normalized) {
41
40
  normalized.includes("/src/handlers/") ||
42
41
  normalized.includes("/src/compile.js") ||
43
42
  normalized.includes("/src/transform.js") ||
44
- normalized.includes("/src/parser.js")
43
+ normalized.includes("/src/syntax/")
45
44
  )
46
45
  }
47
46
 
@@ -166,7 +165,7 @@ export class LangError extends Error {
166
165
  }
167
166
 
168
167
  export class StructError extends LangError {
169
- constructor(m, meta) { super(m, "StructError", meta) }
168
+ constructor(m, meta, tag = "StructError") { super(m, tag, meta) }
170
169
  }
171
170
  export class StructPassedError extends LangError {
172
171
  constructor(m, meta) { super(m, "StructPassedError", meta) }
@@ -175,7 +174,7 @@ export class StructExpectError extends LangError {
175
174
  constructor(m, meta) { super(m, "StructExpectError", meta) }
176
175
  }
177
176
  export class StructResultError extends StructError {
178
- constructor(m, meta) { super(m, "StructResultError", meta) }
177
+ constructor(m, meta) { super(m, meta, "StructResultError") }
179
178
  }
180
179
 
181
180
  export class TypeError_ extends LangError {
@@ -193,10 +192,10 @@ export class EnumError extends LangError {
193
192
  constructor(m, meta) { super(m, "EnumError", meta) }
194
193
  }
195
194
 
196
- export class UseError extends Error {
195
+ export class UseError extends LangError {
197
196
  constructor(m, meta) { super(m, "UseError", meta) }
198
197
  }
199
198
 
200
- export class TypeDefError extends TypeError {
199
+ export class TypeDefError extends LangError {
201
200
  constructor(m, meta) { super(m, "TypeDefError", meta) }
202
201
  }
@@ -1,4 +1,3 @@
1
- // Browser-safe runtime subset serialized into client handlers.
2
1
 
3
2
  export function log(...args) { console.log(...args) }
4
3
  export function warn(...args) { console.warn(...args) }
@@ -34,5 +33,4 @@ export function type(obj) {
34
33
  return undefined
35
34
  }
36
35
 
37
- // Client declarations are emitted before the handler table.
38
36
  export const CLIENT_RUNTIME = [log, warn, error, info, debug, type]