@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.
- package/README.md +522 -14
- package/package.json +2 -6
- package/packages/slim/server/main.slim +433 -422
- package/run-dev-slim.js +10 -5
- package/run-slim.js +10 -20
- package/src/bin/api/github_get.js +7 -12
- package/src/bin/api/github_req.js +54 -241
- package/src/bin/api/lock.js +2 -1
- package/src/bin/api/spm.js +20 -91
- package/src/bin/cli.js +73 -102
- package/src/bin/config.default.json +11 -11
- package/src/bin/helpers.js +2 -7
- package/src/bin/spm.js +3 -7
- package/src/checker.js +1705 -388
- package/src/compile.js +131 -100
- package/src/external/classErrors.js +5 -6
- package/src/external/client.js +0 -2
- package/src/external/core.js +355 -237
- package/src/external/defaults.js +12 -4
- package/src/external/dom.js +663 -0
- package/src/external/helpers.js +87 -58
- package/src/external/reactive.js +601 -0
- package/src/external/router.js +117 -0
- package/src/external/slim-globals.d.ts +43 -4
- package/src/external/typeSyntax.js +258 -0
- package/src/external/types.js +1 -1
- package/src/format.js +183 -12
- package/src/handlers/errorHandler.js +9 -36
- package/src/jsdoc.js +36 -16
- package/src/modulePaths.js +15 -5
- package/src/reactivity.js +774 -0
- package/src/repl.js +1 -1
- package/src/suggest.js +60 -0
- package/src/syntax/babel.js +46 -0
- package/src/syntax/index.js +39 -0
- package/src/syntax/lower.js +666 -0
- package/src/syntax/markup.js +195 -0
- package/src/syntax/plugin.js +903 -0
- package/src/syntax/types.js +106 -0
- package/src/test-runner.js +5 -5
- package/src/transform.js +209 -244
- package/packages/slim/helpers/string.js +0 -43
- package/packages/slim/types/hash.js +0 -11
- package/src/handlers/parser/components.js +0 -295
- package/src/handlers/parserHandler.js +0 -789
- package/src/lexer.js +0 -174
- package/src/parser.js +0 -803
- package/src/sourcemap.js +0 -0
package/src/jsdoc.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import _traverse from "@babel/traverse"
|
|
2
2
|
import * as t from "@babel/types"
|
|
3
|
+
import { readType } from "./external/typeSyntax.js"
|
|
3
4
|
|
|
4
5
|
const traverse = _traverse.default ?? _traverse
|
|
5
6
|
|
|
@@ -10,18 +11,26 @@ const PRIMITIVES = {
|
|
|
10
11
|
function: "Function", element: "HTMLElement"
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
function toTS(type) {
|
|
15
|
+
switch (type.kind) {
|
|
16
|
+
case "named": return PRIMITIVES[type.name] ?? type.name
|
|
17
|
+
case "literal": return typeof type.value === "string" ? JSON.stringify(type.value) : String(type.value)
|
|
18
|
+
case "array": {
|
|
19
|
+
const element = toTS(type.element)
|
|
20
|
+
return type.element.kind === "union" || type.element.kind === "intersection" ? `(${element})[]` : `${element}[]`
|
|
21
|
+
}
|
|
22
|
+
case "tuple": return `[${type.items.map(toTS).join(", ")}]`
|
|
23
|
+
case "generic": return `${type.name}<${type.args.map(toTS).join(", ")}>`
|
|
24
|
+
case "enumMember": return "number | string"
|
|
25
|
+
case "function": return `((${type.params.map((param, index) => `${param.name ?? `arg${index}`}: ${toTS(param.type)}`).join(", ")}) => ${toTS(type.returns)})`
|
|
26
|
+
case "union": return [...new Set(type.types.map(toTS))].join(" | ")
|
|
27
|
+
case "intersection": return [...new Set(type.types.map(toTS))].join(" & ")
|
|
18
28
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return PRIMITIVES[label] ?? label
|
|
29
|
+
return "any"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function mapType(label) {
|
|
33
|
+
return toTS(readType(String(label)))
|
|
25
34
|
}
|
|
26
35
|
|
|
27
36
|
export function jsdocComment(lines) {
|
|
@@ -41,6 +50,12 @@ function enumTypedef(name, schema) {
|
|
|
41
50
|
return `@typedef {${type}} ${name}`
|
|
42
51
|
}
|
|
43
52
|
|
|
53
|
+
function stringList(node) {
|
|
54
|
+
return t.isArrayExpression(node)
|
|
55
|
+
? node.elements.filter(element => t.isStringLiteral(element)).map(element => element.value)
|
|
56
|
+
: []
|
|
57
|
+
}
|
|
58
|
+
|
|
44
59
|
function structTypedef(name, schema) {
|
|
45
60
|
const fields = []
|
|
46
61
|
|
|
@@ -98,7 +113,7 @@ function paramLines(fn) {
|
|
|
98
113
|
return lines
|
|
99
114
|
}
|
|
100
115
|
|
|
101
|
-
function structInterface(schema) {
|
|
116
|
+
function structInterface(schema, readonly = []) {
|
|
102
117
|
const fields = []
|
|
103
118
|
|
|
104
119
|
for (const property of schema.properties) {
|
|
@@ -113,7 +128,8 @@ function structInterface(schema) {
|
|
|
113
128
|
|
|
114
129
|
const optional = rawKey.startsWith("*")
|
|
115
130
|
const field = optional ? rawKey.slice(1).trim() : rawKey
|
|
116
|
-
|
|
131
|
+
const modifier = readonly.includes(field) ? "readonly " : ""
|
|
132
|
+
fields.push(`${modifier}${field}${optional ? "?" : ""}: ${mapType(property.value.value)}`)
|
|
117
133
|
}
|
|
118
134
|
|
|
119
135
|
return `{ ${fields.join("; ")} }`
|
|
@@ -199,8 +215,11 @@ export function emitDeclarations(ast) {
|
|
|
199
215
|
|
|
200
216
|
if (t.isIdentifier(init.callee, { name: "__def_struct__" }) && t.isObjectExpression(init.arguments[1])) {
|
|
201
217
|
usesStruct = true
|
|
202
|
-
|
|
203
|
-
|
|
218
|
+
const params = stringList(init.arguments[6])
|
|
219
|
+
const generic = params.length ? `<${params.join(", ")}>` : ""
|
|
220
|
+
const anyArgs = params.length ? `<${params.map(() => "any").join(", ")}>` : ""
|
|
221
|
+
lines.push(`export interface ${name}${generic} ${structInterface(init.arguments[1], stringList(init.arguments[7]))}`)
|
|
222
|
+
lines.push(`export declare const ${name}: SlimStruct<${name}${anyArgs}>`)
|
|
204
223
|
} else if (t.isIdentifier(init.callee, { name: "__def_enum__" }) && t.isObjectExpression(init.arguments[1])) {
|
|
205
224
|
lines.push(`export type ${name} = ${enumValueType(init.arguments[1])}`)
|
|
206
225
|
lines.push(`export declare const ${name}: Record<string, any>`)
|
|
@@ -215,7 +234,7 @@ export function emitDeclarations(ast) {
|
|
|
215
234
|
if (lines.length === 0) return null
|
|
216
235
|
|
|
217
236
|
const header = usesStruct
|
|
218
|
-
? "type SlimStruct<T> = { verify(value: unknown): T; verifySafe(value: unknown): { success: boolean; result: T } }\n\n"
|
|
237
|
+
? "type SlimStruct<T> = { verify(value: unknown): T; verifySafe(value: unknown): { success: boolean; result: T }; new(values?: Partial<T>): T }\n\n"
|
|
219
238
|
: ""
|
|
220
239
|
|
|
221
240
|
return header + lines.join("\n") + "\n"
|
|
@@ -233,6 +252,7 @@ export function emitJsDoc(ast) {
|
|
|
233
252
|
if (t.isIdentifier(init.callee, { name: "__def_struct__" }) &&
|
|
234
253
|
t.isStringLiteral(init.arguments[0]) &&
|
|
235
254
|
t.isObjectExpression(init.arguments[1])) {
|
|
255
|
+
for (const param of stringList(init.arguments[6])) typedefs.push(`@template ${param}`)
|
|
236
256
|
typedefs.push(structTypedef(init.arguments[0].value, init.arguments[1]))
|
|
237
257
|
}
|
|
238
258
|
|
package/src/modulePaths.js
CHANGED
|
@@ -7,9 +7,13 @@ const slimExtension = ".slim"
|
|
|
7
7
|
|
|
8
8
|
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")
|
|
9
9
|
|
|
10
|
-
function
|
|
10
|
+
export function readJson(file) {
|
|
11
|
+
return JSON.parse(fs.readFileSync(file, "utf8").replace(/^\uFEFF/, ""))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function readConfig() {
|
|
11
15
|
try {
|
|
12
|
-
return
|
|
16
|
+
return readJson(path.join(process.cwd(), "slimconfig.json"))
|
|
13
17
|
} catch {
|
|
14
18
|
return {}
|
|
15
19
|
}
|
|
@@ -62,8 +66,16 @@ function isNodeModule(raw) {
|
|
|
62
66
|
return fs.existsSync(path.resolve("node_modules", bareName(raw)))
|
|
63
67
|
}
|
|
64
68
|
|
|
69
|
+
const outputOverrides = new Map()
|
|
70
|
+
|
|
71
|
+
export function setOutputPath(slimFile, outFile) {
|
|
72
|
+
outputOverrides.set(path.resolve(slimFile), path.resolve(outFile))
|
|
73
|
+
}
|
|
74
|
+
|
|
65
75
|
export function getDistPath(slimFile) {
|
|
66
76
|
const abs = path.resolve(slimFile)
|
|
77
|
+
if (outputOverrides.has(abs)) return outputOverrides.get(abs)
|
|
78
|
+
|
|
67
79
|
const srcRoot = path.resolve("src")
|
|
68
80
|
const projectRoot = path.resolve(".")
|
|
69
81
|
const shippedPackages = path.join(PACKAGE_ROOT, "packages")
|
|
@@ -80,7 +92,6 @@ export function getDistPath(slimFile) {
|
|
|
80
92
|
} else if (isWithin(projectRoot, abs)) {
|
|
81
93
|
relative = path.relative(projectRoot, abs)
|
|
82
94
|
} else {
|
|
83
|
-
// Drop leading "../" so out-of-project sources can't escape the dist dir.
|
|
84
95
|
const stripped = path.relative(projectRoot, abs).split(path.sep).filter(seg => seg !== "..")
|
|
85
96
|
relative = stripped.length ? path.join(...stripped) : path.basename(abs)
|
|
86
97
|
}
|
|
@@ -118,14 +129,13 @@ export function resolveSlimSource(raw, fromFile) {
|
|
|
118
129
|
return localSource
|
|
119
130
|
}
|
|
120
131
|
|
|
121
|
-
export function resolveSlimImport(raw, fromFile) {
|
|
132
|
+
export function resolveSlimImport(raw, fromFile, distFrom = getDistPath(fromFile)) {
|
|
122
133
|
if (raw.endsWith(".js")) return raw
|
|
123
134
|
|
|
124
135
|
const slimSource = resolveSlimSource(raw, fromFile)
|
|
125
136
|
if (slimSource === null) return raw
|
|
126
137
|
|
|
127
138
|
const distTarget = getDistPath(slimSource)
|
|
128
|
-
const distFrom = getDistPath(fromFile)
|
|
129
139
|
const relative = path.relative(path.dirname(distFrom), distTarget).replace(/\\/g, "/")
|
|
130
140
|
|
|
131
141
|
return relative.startsWith(".") ? relative : "./" + relative
|