@slim-lang/core 1.2.12 → 1.2.15

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
package/src/repl.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import repl from "node:repl"
2
2
  import vm from "node:vm"
3
- import { preprocess } from "./parser.js"
3
+ import { preprocess } from "./syntax/index.js"
4
4
  import "./external/defaults.js"
5
5
 
6
6
  function slimEval(cmd, context, filename, callback) {
package/src/suggest.js ADDED
@@ -0,0 +1,60 @@
1
+ function distance(a, b) {
2
+ const rows = a.length + 1
3
+ const cols = b.length + 1
4
+ const d = Array.from({ length: rows }, (_, i) => [i, ...Array(cols - 1).fill(0)])
5
+ for (let j = 1; j < cols; j++) d[0][j] = j
6
+
7
+ for (let i = 1; i < rows; i++) {
8
+ for (let j = 1; j < cols; j++) {
9
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1
10
+ d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost)
11
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
12
+ d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1)
13
+ }
14
+ }
15
+ }
16
+ return d[a.length][b.length]
17
+ }
18
+
19
+ export function closest(name, candidates) {
20
+ const limit = name.length <= 2 ? 0 : name.length <= 5 ? 1 : 2
21
+ const lower = name.toLowerCase()
22
+ let best = null
23
+ let bestDistance = Infinity
24
+
25
+ for (const candidate of candidates) {
26
+ if (candidate === name || Math.abs(candidate.length - name.length) > limit + 1) continue
27
+ const score = candidate.toLowerCase() === lower ? 0.5 : distance(lower, candidate.toLowerCase())
28
+ if (score <= Math.max(limit, 0.5) && score < bestDistance) {
29
+ best = candidate
30
+ bestDistance = score
31
+ }
32
+ }
33
+ return best
34
+ }
35
+
36
+ export function didYouMean(name, candidates) {
37
+ const match = closest(name, candidates)
38
+ return match ? ` — did you mean "${match}"?` : ""
39
+ }
40
+
41
+ export const KNOWN_GLOBALS = new Set([
42
+ "log", "warn", "error", "info", "debug", "type", "Component", "Type", "Struct", "onError",
43
+ "test", "describe", "assert", "assertEqual", "PI", "raw", "escapeHtml", "htmlToVdom",
44
+ "StructError", "StructPassedError", "StructExpectError", "ArgumentDeclarationTypeError",
45
+ "onMount", "onConnect", "onUnmount", "effect", "watch", "untrack", "batch", "onCleanup", "resource", "renderToString", "renderToStringAsync", "hydrate", "createContext", "provide", "inject", "catchError", "createForm", "Router", "Link", "Portal", "navigate", "route", "matchRoute",
46
+ "window", "document", "navigator", "location", "history", "screen", "localStorage", "sessionStorage",
47
+ "indexedDB", "alert", "confirm", "prompt", "event", "self", "top", "parent", "frames", "customElements",
48
+ "requestAnimationFrame", "cancelAnimationFrame", "requestIdleCallback", "cancelIdleCallback",
49
+ "getComputedStyle", "matchMedia", "scrollTo", "scrollBy", "open", "close", "innerWidth", "innerHeight",
50
+ "devicePixelRatio", "caches", "Image", "Audio", "Option", "FileReader", "DOMParser", "XMLHttpRequest",
51
+ "CSS", "Node", "Element", "HTMLElement", "SVGElement", "ShadowRoot", "DocumentFragment",
52
+ "MutationObserver", "IntersectionObserver", "ResizeObserver", "Worker", "SharedWorker", "Notification",
53
+ "process", "Buffer", "require", "module", "exports", "__dirname", "__filename", "global"
54
+ ])
55
+
56
+ const GLOBAL_PATTERN = /^(?:HTML|SVG|DOM|CSS|RTC|WebGL|Web|IDB)|(?:Event|Element|Observer|Error|Array)$/
57
+
58
+ export function isKnownGlobal(name) {
59
+ return KNOWN_GLOBALS.has(name) || name in globalThis || GLOBAL_PATTERN.test(name)
60
+ }
@@ -0,0 +1,46 @@
1
+ import fs from "node:fs"
2
+ import path from "node:path"
3
+ import vm from "node:vm"
4
+ import { createRequire } from "node:module"
5
+
6
+ const require = createRequire(import.meta.url)
7
+
8
+ const INTERNALS = [
9
+ "tt", "tokenIsIdentifier", "tokenIsKeywordOrIdentifier", "tokenCanStartExpression",
10
+ "tokenOperatorPrecedence", "ExpressionErrors", "functionFlags"
11
+ ]
12
+ const PLUGIN_TABLES = ["mixinPlugins", "mixinPluginNames", "parserClassCache"]
13
+
14
+ const EXTRA_EXPORTS = `
15
+ exports.internals = { ${INTERNALS.join(", ")} };
16
+ exports.registerPlugin = function registerPlugin(name, mixin) {
17
+ if (!Object.prototype.hasOwnProperty.call(mixinPlugins, name)) mixinPluginNames.push(name);
18
+ mixinPlugins[name] = mixin;
19
+ parserClassCache.clear();
20
+ };
21
+ `
22
+
23
+ function declares(source, name) {
24
+ return new RegExp(`^(?:const|let|var|function|class) ${name}\\b`, "m").test(source)
25
+ }
26
+
27
+ function loadParser() {
28
+ const file = require.resolve("@babel/parser")
29
+ const { version } = require("@babel/parser/package.json")
30
+ const source = fs.readFileSync(file, "utf8")
31
+
32
+ const missing = [...INTERNALS, ...PLUGIN_TABLES].filter(name => !declares(source, name))
33
+ if (missing.length > 0) {
34
+ throw new Error(`Slim can't hook into @babel/parser ${version} (no ${missing.join(", ")}): ` +
35
+ `install the version pinned in Slim's package.json`)
36
+ }
37
+
38
+ const run = vm.compileFunction(source + EXTRA_EXPORTS, ["exports", "require", "module", "__filename", "__dirname"], {
39
+ filename: file
40
+ })
41
+ const module = { exports: {} }
42
+ run(module.exports, createRequire(file), module, file, path.dirname(file))
43
+ return module.exports
44
+ }
45
+
46
+ export const babel = loadParser()
@@ -0,0 +1,39 @@
1
+ import _generate from "@babel/generator"
2
+ import { parseSlim } from "./plugin.js"
3
+ import { lowerSlim } from "./lower.js"
4
+
5
+ const generate = _generate.default ?? _generate
6
+
7
+ export function parseProgram(code, sourceFile = "input.slim") {
8
+ return lowerSlim(parseSlim(code, sourceFile), code, sourceFile)
9
+ }
10
+
11
+ export function preprocess(code, sourceFile = "input.slim") {
12
+ return { code: generate(parseProgram(code, sourceFile)).code }
13
+ }
14
+
15
+ function collectUses(node, uses) {
16
+ if (!node || typeof node !== "object") return
17
+ if (Array.isArray(node)) return node.forEach(child => collectUses(child, uses))
18
+ if (node.type === "SlimUseDeclaration") {
19
+ uses.push({ spec: node.specifier, source: node.source, start: node.start, end: node.end })
20
+ return
21
+ }
22
+ for (const [key, value] of Object.entries(node)) {
23
+ if (key !== "loc" && key !== "extra" && value && typeof value === "object") collectUses(value, uses)
24
+ }
25
+ }
26
+
27
+ export function findUses(code) {
28
+ let ast
29
+ try {
30
+ ast = parseSlim(code)
31
+ } catch {
32
+ return []
33
+ }
34
+ const uses = []
35
+ collectUses(ast.program, uses)
36
+ return uses
37
+ }
38
+
39
+ export { parseSlim }