@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.
- 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 -316
- 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
|
@@ -6,7 +6,7 @@ declare function error(...args: any[]): void
|
|
|
6
6
|
declare function info(...args: any[]): void
|
|
7
7
|
declare function debug(...args: any[]): void
|
|
8
8
|
|
|
9
|
-
declare function type(value: any
|
|
9
|
+
declare function type(value: any): string
|
|
10
10
|
|
|
11
11
|
type email = string
|
|
12
12
|
type url = string
|
|
@@ -16,24 +16,39 @@ type negative = number
|
|
|
16
16
|
type natural = number
|
|
17
17
|
type nonempty = string | any[]
|
|
18
18
|
declare function onError(handler: (error: any) => void): void
|
|
19
|
-
declare function test(name: string, fn: () => void): void
|
|
19
|
+
declare function test(name: string, fn: () => void | Promise<void>): void
|
|
20
|
+
declare function describe(name: string, fn: () => void): void
|
|
20
21
|
declare function assert(condition: any, message?: string): void
|
|
21
22
|
declare function assertEqual(actual: any, expected: any, message?: string): void
|
|
22
23
|
|
|
23
24
|
declare const PI: number
|
|
24
25
|
|
|
26
|
+
declare function effect(fn: () => void | (() => void)): () => void
|
|
27
|
+
declare function watch<T>(source: () => T, fn: (value: T, previous: T | undefined) => void | (() => void)): () => void
|
|
28
|
+
declare function untrack<T>(fn: () => T): T
|
|
29
|
+
declare function batch<T>(fn: () => T): T
|
|
30
|
+
declare function onCleanup(fn: () => void): void
|
|
31
|
+
declare function resource<T>(loader: () => T | Promise<T>, initial?: T): { value: T | undefined, readonly loading: boolean, readonly error: any, reload(): void }
|
|
32
|
+
declare function renderToString(render: () => any): string
|
|
33
|
+
declare function createContext<T>(fallback?: T): { fallback: T | undefined }
|
|
34
|
+
declare function provide<T>(context: { fallback: T | undefined }, value: T): void
|
|
35
|
+
declare function inject<T>(context: { fallback: T | undefined }): T
|
|
36
|
+
declare function catchError(render: () => any, fallback: (error: any, reset: () => void) => any): () => any
|
|
37
|
+
declare function hydrate<T>(render: () => T, container: any): T
|
|
38
|
+
|
|
25
39
|
declare class Struct {}
|
|
26
40
|
declare class Component {}
|
|
27
41
|
declare class Type {}
|
|
28
42
|
declare class HTMLElement {}
|
|
29
43
|
|
|
30
|
-
declare function __def_struct__(name: string, schema: any, specifications?: any, defaults?: any, extendsName?: any, methods?: any): any
|
|
44
|
+
declare function __def_struct__(name: string, schema: any, specifications?: any, defaults?: any, extendsName?: any, methods?: any, typeParams?: string[] | null, readonly?: string[] | null): any
|
|
31
45
|
declare function __def_enum__(name: string, schema: any): any
|
|
32
|
-
declare function __typed__(value: any, structName: any, returnMethod?: string): any
|
|
46
|
+
declare function __typed__(value: any, structName: any, returnMethod?: string, typeArgs?: string[] | null): any
|
|
33
47
|
|
|
34
48
|
declare function __typed_variable__<T>(value: T, specification?: any, bindingId?: any, varName?: any): T
|
|
35
49
|
declare function __typed_variable_check__<T>(bindingId: any, value: T, varName?: any): T
|
|
36
50
|
declare function __typed_pattern__<T>(value: T, typeLabel: any): T
|
|
51
|
+
declare function __as_struct__<T>(value: T, typeLabel: string): T
|
|
37
52
|
declare function __typed_parameter__(value: any, specification: any, bindingId: any, varName?: any, optional?: any, message?: any): any
|
|
38
53
|
declare function __typed_static_field__<T>(owner: any, field: any, value: T, specification?: any, displayName?: any): T
|
|
39
54
|
declare function __typed_static_field_check__<T>(owner: any, field: any, value: T, displayName?: any): T
|
|
@@ -51,13 +66,37 @@ declare function __is_empty__(value: any): boolean
|
|
|
51
66
|
declare function __copyof__(value: any): any
|
|
52
67
|
declare function __intdiv__(a: number, b: number): number
|
|
53
68
|
declare function __match_eq__(subject: any, pattern: any): boolean
|
|
69
|
+
declare function __match_type__(subject: any, typeLabel: string): boolean
|
|
54
70
|
declare function __lock_object__<T>(value: T): T
|
|
55
71
|
|
|
56
72
|
declare function __handle_sync_error__(error: any): void
|
|
57
73
|
declare function __handle_async_error__(error: any): void
|
|
58
74
|
declare function __html__(...args: any[]): any
|
|
75
|
+
declare function __component__<T>(fn: () => T): T
|
|
76
|
+
declare function __signal__<T>(value: T): { v: T, notify(): void }
|
|
77
|
+
declare function __computed__<T>(fn: () => T): { readonly v: T }
|
|
78
|
+
declare function __mut__<T>(source: any, result: T): T
|
|
79
|
+
declare function __mut_deep__<T>(source: any, change: (object: any) => T, object: any): T
|
|
80
|
+
declare function __is__(source: any, key: any, strict: boolean): boolean
|
|
81
|
+
declare function __defaults__(props: any, defaults: any): any
|
|
82
|
+
declare function __template__(roots: any[], svg?: boolean): () => any
|
|
83
|
+
declare function __root__(fn: () => any): any
|
|
84
|
+
declare function __insert__(anchor: any, fn: () => any): void
|
|
85
|
+
declare function __branch__(test: () => any, yes: () => any, no: (() => any) | null): () => any
|
|
86
|
+
declare function __attr__(el: any, name: string, fn: () => any): void
|
|
87
|
+
declare function __spread__(el: any, fn: () => any): void
|
|
88
|
+
declare function __on__(el: any, event: string, handler: any): void
|
|
89
|
+
declare function __list__(anchor: any, source: () => any, render: (item: any, index: any) => any, key?: (item: any, index: number) => any): void
|
|
90
|
+
declare function __s__(value: any): string
|
|
91
|
+
declare function __once__(anchor: any, value: any): void
|
|
92
|
+
declare function __set__(el: any, name: string, value: any): void
|
|
93
|
+
declare function __enter__(el: any, className: string): void
|
|
94
|
+
declare function __leave__(el: any, className: string): void
|
|
95
|
+
declare function __live__(read: () => any): any
|
|
59
96
|
declare function __flush_events__(...args: any[]): any
|
|
60
97
|
declare function htmlToVdom(...args: any[]): any
|
|
98
|
+
declare function raw(html: string): any
|
|
99
|
+
declare function escapeHtml(value: any): string
|
|
61
100
|
|
|
62
101
|
declare class StructError extends Error {}
|
|
63
102
|
declare class StructPassedError extends Error {}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
|
|
2
|
+
export const ANY = Object.freeze({ kind: "any" })
|
|
3
|
+
|
|
4
|
+
export const named = name => name === "any" ? ANY : { kind: "named", name }
|
|
5
|
+
export const literal = value => ({ kind: "literal", value })
|
|
6
|
+
export const arrayOf = element => ({ kind: "array", element })
|
|
7
|
+
export const tupleOf = items => ({ kind: "tuple", items })
|
|
8
|
+
export const genericOf = (name, args) => name === "Array" && args.length === 1 ? arrayOf(args[0]) : { kind: "generic", name, args }
|
|
9
|
+
export const enumMember = (name, member) => ({ kind: "enumMember", name, member })
|
|
10
|
+
export const functionOf = (params, returns) => ({ kind: "function", params, returns })
|
|
11
|
+
|
|
12
|
+
function flatten(kind, types) {
|
|
13
|
+
return types.flatMap(type => type.kind === kind ? type.types : [type])
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function unionOf(types) {
|
|
17
|
+
const unique = []
|
|
18
|
+
const seen = new Set()
|
|
19
|
+
for (const type of flatten("union", types)) {
|
|
20
|
+
if (type.kind === "any") return ANY
|
|
21
|
+
const key = formatType(type)
|
|
22
|
+
if (!seen.has(key)) { seen.add(key); unique.push(type) }
|
|
23
|
+
}
|
|
24
|
+
return unique.length === 1 ? unique[0] : { kind: "union", types: unique }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function intersectionOf(types) {
|
|
28
|
+
const all = flatten("intersection", types)
|
|
29
|
+
return all.length === 1 ? all[0] : { kind: "intersection", types: all }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const nullable = type => unionOf([type, named("null"), named("undefined")])
|
|
33
|
+
|
|
34
|
+
function tokenize(text) {
|
|
35
|
+
const tokens = []
|
|
36
|
+
let i = 0
|
|
37
|
+
|
|
38
|
+
while (i < text.length) {
|
|
39
|
+
const c = text[i]
|
|
40
|
+
if (/\s/.test(c)) { i++; continue }
|
|
41
|
+
|
|
42
|
+
if (c === '"' || c === "'") {
|
|
43
|
+
let j = i + 1
|
|
44
|
+
while (j < text.length && text[j] !== c) j += text[j] === "\\" ? 2 : 1
|
|
45
|
+
if (j >= text.length) throw new SyntaxError(`unterminated string in type "${text}"`)
|
|
46
|
+
tokens.push({ kind: "string", value: text.slice(i, j + 1) })
|
|
47
|
+
i = j + 1
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const number = /^-?\d+(?:\.\d+)?/.exec(text.slice(i))
|
|
52
|
+
if (number && (c !== "-" || /\d/.test(text[i + 1]))) {
|
|
53
|
+
tokens.push({ kind: "number", value: number[0] })
|
|
54
|
+
i += number[0].length
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const word = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*/.exec(text.slice(i))
|
|
59
|
+
if (word) {
|
|
60
|
+
tokens.push({ kind: "name", value: word[0] })
|
|
61
|
+
i += word[0].length
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const pair = text.slice(i, i + 2)
|
|
66
|
+
if (pair === "::" || pair === "[]" || pair === "=>") { tokens.push({ kind: pair }); i += 2; continue }
|
|
67
|
+
if ("|&[]()<>,?:".includes(c)) { tokens.push({ kind: c }); i++; continue }
|
|
68
|
+
|
|
69
|
+
throw new SyntaxError(`unexpected "${c}" in type "${text}"`)
|
|
70
|
+
}
|
|
71
|
+
return tokens
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function readString(raw) {
|
|
75
|
+
if (raw[0] === '"') {
|
|
76
|
+
try { return JSON.parse(raw) } catch { }
|
|
77
|
+
}
|
|
78
|
+
return raw.slice(1, -1).replace(/\\(.)/g, "$1")
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class TypeParser {
|
|
82
|
+
constructor(text) {
|
|
83
|
+
this.text = text
|
|
84
|
+
this.tokens = tokenize(text)
|
|
85
|
+
this.pos = 0
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
peek(kind) { return this.tokens[this.pos]?.kind === kind }
|
|
89
|
+
|
|
90
|
+
eat(kind) {
|
|
91
|
+
if (!this.peek(kind)) return false
|
|
92
|
+
this.pos++
|
|
93
|
+
return true
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
expect(kind) {
|
|
97
|
+
if (!this.eat(kind)) throw new SyntaxError(`expected "${kind}" in type "${this.text}"`)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
parse() {
|
|
101
|
+
const type = this.union()
|
|
102
|
+
if (this.pos < this.tokens.length) throw new SyntaxError(`unexpected "${this.tokens[this.pos].value ?? this.tokens[this.pos].kind}" in type "${this.text}"`)
|
|
103
|
+
return type
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
union() {
|
|
107
|
+
this.eat("|")
|
|
108
|
+
const types = [this.intersection()]
|
|
109
|
+
while (this.eat("|")) types.push(this.intersection())
|
|
110
|
+
return types.length === 1 ? types[0] : unionOf(types)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
intersection() {
|
|
114
|
+
const types = [this.postfix()]
|
|
115
|
+
while (this.eat("&")) types.push(this.postfix())
|
|
116
|
+
return intersectionOf(types)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
postfix() {
|
|
120
|
+
let type = this.primary()
|
|
121
|
+
for (;;) {
|
|
122
|
+
if (this.eat("[]")) type = arrayOf(type)
|
|
123
|
+
else if (this.eat("?")) type = nullable(type)
|
|
124
|
+
else return type
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
list(close) {
|
|
129
|
+
const items = []
|
|
130
|
+
if (this.eat(close)) return items
|
|
131
|
+
do items.push(this.union())
|
|
132
|
+
while (this.eat(","))
|
|
133
|
+
this.expect(close)
|
|
134
|
+
return items
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
functionType() {
|
|
138
|
+
const params = []
|
|
139
|
+
if (!this.eat(")")) {
|
|
140
|
+
do {
|
|
141
|
+
let name = null
|
|
142
|
+
if (this.tokens[this.pos]?.kind === "name" && this.tokens[this.pos + 1]?.kind === ":") {
|
|
143
|
+
name = this.tokens[this.pos].value
|
|
144
|
+
this.pos += 2
|
|
145
|
+
}
|
|
146
|
+
params.push({ name, type: this.union() })
|
|
147
|
+
} while (this.eat(","))
|
|
148
|
+
this.expect(")")
|
|
149
|
+
}
|
|
150
|
+
this.expect("=>")
|
|
151
|
+
return functionOf(params, this.union())
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
primary() {
|
|
155
|
+
const token = this.tokens[this.pos++]
|
|
156
|
+
if (!token) throw new SyntaxError(`type "${this.text}" ends unexpectedly`)
|
|
157
|
+
|
|
158
|
+
switch (token.kind) {
|
|
159
|
+
case "(": {
|
|
160
|
+
const start = this.pos
|
|
161
|
+
try {
|
|
162
|
+
return this.functionType()
|
|
163
|
+
} catch {
|
|
164
|
+
this.pos = start
|
|
165
|
+
}
|
|
166
|
+
const inner = this.union()
|
|
167
|
+
this.expect(")")
|
|
168
|
+
return inner
|
|
169
|
+
}
|
|
170
|
+
case "[": return tupleOf(this.list("]"))
|
|
171
|
+
case "string": return literal(readString(token.value))
|
|
172
|
+
case "number": return literal(Number(token.value))
|
|
173
|
+
case "name":
|
|
174
|
+
if (token.value === "true" || token.value === "false") return literal(token.value === "true")
|
|
175
|
+
if (this.eat("::")) {
|
|
176
|
+
const member = this.tokens[this.pos++]
|
|
177
|
+
if (member?.kind !== "name") throw new SyntaxError(`expected an enum member in type "${this.text}"`)
|
|
178
|
+
return enumMember(token.value, member.value)
|
|
179
|
+
}
|
|
180
|
+
if (this.eat("<")) return genericOf(token.value, this.list(">"))
|
|
181
|
+
return named(token.value)
|
|
182
|
+
}
|
|
183
|
+
throw new SyntaxError(`unexpected "${token.kind}" in type "${this.text}"`)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const parsed = new Map()
|
|
188
|
+
|
|
189
|
+
export function parseType(label) {
|
|
190
|
+
let type = parsed.get(label)
|
|
191
|
+
if (!type) {
|
|
192
|
+
type = label.trim() === "" ? ANY : new TypeParser(label).parse()
|
|
193
|
+
parsed.set(label, type)
|
|
194
|
+
}
|
|
195
|
+
return type
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function readType(label) {
|
|
199
|
+
try {
|
|
200
|
+
return parseType(label)
|
|
201
|
+
} catch {
|
|
202
|
+
return named(label.trim())
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function isNullish(type) {
|
|
207
|
+
return type.kind === "named" && (type.name === "null" || type.name === "undefined")
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function wrap(type) {
|
|
211
|
+
const text = formatType(type)
|
|
212
|
+
return type.kind === "union" || type.kind === "intersection" || type.kind === "function" || text.endsWith("?") ? `(${text})` : text
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function formatType(type) {
|
|
216
|
+
switch (type.kind) {
|
|
217
|
+
case "any": return "any"
|
|
218
|
+
case "named": return type.name
|
|
219
|
+
case "literal": return typeof type.value === "string" ? JSON.stringify(type.value) : String(type.value)
|
|
220
|
+
case "array": return `${wrap(type.element)}[]`
|
|
221
|
+
case "tuple": return `[${type.items.map(formatType).join(", ")}]`
|
|
222
|
+
case "generic": return `${type.name}<${type.args.map(formatType).join(", ")}>`
|
|
223
|
+
case "enumMember": return `${type.name}::${type.member}`
|
|
224
|
+
case "intersection": return type.types.map(wrap).join(" & ")
|
|
225
|
+
case "function": {
|
|
226
|
+
const params = type.params.map(param => param.name ? `${param.name}: ${formatType(param.type)}` : formatType(param.type))
|
|
227
|
+
return `(${params.join(", ")}) => ${formatType(type.returns)}`
|
|
228
|
+
}
|
|
229
|
+
case "union": {
|
|
230
|
+
const rest = type.types.filter(part => !isNullish(part))
|
|
231
|
+
const optional = rest.length === 1 && type.types.length === 3 &&
|
|
232
|
+
type.types.some(part => part.name === "null") && type.types.some(part => part.name === "undefined")
|
|
233
|
+
if (optional) return `${wrap(rest[0])}?`
|
|
234
|
+
return type.types.map(part => part.kind === "intersection" || part.kind === "function" ? `(${formatType(part)})` : formatType(part)).join(" | ")
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return "any"
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function substituteType(type, bound) {
|
|
241
|
+
switch (type.kind) {
|
|
242
|
+
case "named": return bound.get(type.name) ?? type
|
|
243
|
+
case "array": return arrayOf(substituteType(type.element, bound))
|
|
244
|
+
case "tuple": return tupleOf(type.items.map(item => substituteType(item, bound)))
|
|
245
|
+
case "generic": return genericOf(type.name, type.args.map(arg => substituteType(arg, bound)))
|
|
246
|
+
case "union": return unionOf(type.types.map(part => substituteType(part, bound)))
|
|
247
|
+
case "intersection": return intersectionOf(type.types.map(part => substituteType(part, bound)))
|
|
248
|
+
case "function":
|
|
249
|
+
return functionOf(type.params.map(param => ({ ...param, type: substituteType(param.type, bound) })), substituteType(type.returns, bound))
|
|
250
|
+
}
|
|
251
|
+
return type
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function referenceName(type) {
|
|
255
|
+
if (type.kind === "named" || type.kind === "generic" || type.kind === "enumMember") return type.name
|
|
256
|
+
if (type.kind === "array") return referenceName(type.element)
|
|
257
|
+
return null
|
|
258
|
+
}
|
package/src/external/types.js
CHANGED
package/src/format.js
CHANGED
|
@@ -1,6 +1,170 @@
|
|
|
1
1
|
import fs from "node:fs"
|
|
2
|
+
import { babel } from "./syntax/plugin.js"
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
const OPENERS = new Set(["{", "(", "[", "${", "#{", "#["])
|
|
5
|
+
const CLOSERS = new Set(["}", ")", "]"])
|
|
6
|
+
const VERBATIM = new Set(["string", "template", "CommentBlock", "CommentLine", "regexp"])
|
|
7
|
+
|
|
8
|
+
const VOID_ELEMENTS = new Set([
|
|
9
|
+
"area", "base", "br", "col", "embed", "hr", "img", "input",
|
|
10
|
+
"link", "meta", "param", "source", "track", "wbr"
|
|
11
|
+
])
|
|
12
|
+
|
|
13
|
+
const label = token => typeof token.type === "string" ? token.type : token.type.label
|
|
14
|
+
|
|
15
|
+
function collectMarkup(node, out) {
|
|
16
|
+
if (!node || typeof node !== "object") return
|
|
17
|
+
if (Array.isArray(node)) return node.forEach(child => collectMarkup(child, out))
|
|
18
|
+
if (node.type === "SlimMarkup") out.push(node)
|
|
19
|
+
for (const [key, value] of Object.entries(node)) {
|
|
20
|
+
if (key !== "loc" && key !== "extra" && value && typeof value === "object") collectMarkup(value, out)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function describeMarkup(code, node) {
|
|
25
|
+
const holes = node.expressions.map(expression => {
|
|
26
|
+
const open = code.lastIndexOf("${", expression.start)
|
|
27
|
+
let close = expression.end
|
|
28
|
+
while (close < node.end && code[close] !== "}") close++
|
|
29
|
+
return { start: open, end: close + 1, depth: 0 }
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const changes = []
|
|
33
|
+
let depth = 0
|
|
34
|
+
let inTag = false
|
|
35
|
+
let closing = false
|
|
36
|
+
let tag = ""
|
|
37
|
+
let quote = null
|
|
38
|
+
let hole = 0
|
|
39
|
+
|
|
40
|
+
for (let i = node.start; i < node.end; i++) {
|
|
41
|
+
if (hole < holes.length && i === holes[hole].start) {
|
|
42
|
+
holes[hole].depth = depth
|
|
43
|
+
i = holes[hole++].end - 1
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
const c = code[i]
|
|
47
|
+
if (inTag) {
|
|
48
|
+
if (quote) {
|
|
49
|
+
if (c === quote) quote = null
|
|
50
|
+
} else if (c === '"' || c === "'") {
|
|
51
|
+
quote = c
|
|
52
|
+
} else if (c === ">") {
|
|
53
|
+
inTag = false
|
|
54
|
+
if (closing) depth--
|
|
55
|
+
else if (code[i - 1] !== "/" && !VOID_ELEMENTS.has(tag)) depth++
|
|
56
|
+
changes.push({ at: i + 1, depth })
|
|
57
|
+
}
|
|
58
|
+
} else if (c === "<" && code[i + 1] === "/") {
|
|
59
|
+
inTag = true
|
|
60
|
+
closing = true
|
|
61
|
+
} else if (c === "<" && /[A-Za-z]/.test(code[i + 1] ?? "")) {
|
|
62
|
+
inTag = true
|
|
63
|
+
closing = false
|
|
64
|
+
const name = /^[\w:.-]+/.exec(code.slice(i + 1))
|
|
65
|
+
tag = name ? name[0].toLowerCase() : ""
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const depthAt = offset => {
|
|
70
|
+
let current = 0
|
|
71
|
+
for (const change of changes) {
|
|
72
|
+
if (change.at > offset) break
|
|
73
|
+
current = change.depth
|
|
74
|
+
}
|
|
75
|
+
return current
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return { start: node.start, end: node.end, holes, depthAt }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function depthsFromSyntax(code) {
|
|
82
|
+
const ast = babel.parse(code, {
|
|
83
|
+
sourceType: "module",
|
|
84
|
+
tokens: true,
|
|
85
|
+
errorRecovery: false,
|
|
86
|
+
plugins: ["typescript", "slim"]
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
const markupNodes = []
|
|
90
|
+
collectMarkup(ast.program, markupNodes)
|
|
91
|
+
const markup = markupNodes.map(node => describeMarkup(code, node))
|
|
92
|
+
|
|
93
|
+
const inText = (region, offset) =>
|
|
94
|
+
offset >= region.start && offset < region.end && !region.holes.some(hole => offset >= hole.start && offset < hole.end)
|
|
95
|
+
|
|
96
|
+
const brackets = ast.tokens.filter(token => {
|
|
97
|
+
const kind = label(token)
|
|
98
|
+
if (!OPENERS.has(kind) && !CLOSERS.has(kind)) return false
|
|
99
|
+
return !markup.some(region => inText(region, token.start) ||
|
|
100
|
+
region.holes.some(hole => token.start === hole.end - 1))
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
const templateText = []
|
|
104
|
+
const stack = []
|
|
105
|
+
for (const token of ast.tokens) {
|
|
106
|
+
const kind = label(token)
|
|
107
|
+
if (kind === "`") {
|
|
108
|
+
if (stack.at(-1)?.kind === "template") templateText.push({ start: stack.pop().start, end: token.start })
|
|
109
|
+
else stack.push({ kind: "template", start: token.end })
|
|
110
|
+
} else if (kind === "${" && stack.at(-1)?.kind === "template") {
|
|
111
|
+
templateText.push({ start: stack.at(-1).start, end: token.start + 1 })
|
|
112
|
+
stack.push({ kind: "hole" })
|
|
113
|
+
} else if (kind === "}" && stack.at(-1)?.kind === "hole") {
|
|
114
|
+
stack.pop()
|
|
115
|
+
stack.at(-1).start = token.end
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const inTemplateText = offset => templateText.some(range => offset > range.start && offset < range.end)
|
|
119
|
+
|
|
120
|
+
const result = []
|
|
121
|
+
let bracket = 0
|
|
122
|
+
let depth = 0
|
|
123
|
+
let tokenIndex = 0
|
|
124
|
+
let lineStart = 0
|
|
125
|
+
|
|
126
|
+
const lines = code.split("\n")
|
|
127
|
+
for (const line of lines) {
|
|
128
|
+
const first = lineStart + (line.length - line.trimStart().length)
|
|
129
|
+
|
|
130
|
+
while (bracket < brackets.length && brackets[bracket].start < first) {
|
|
131
|
+
depth += OPENERS.has(label(brackets[bracket])) ? 1 : -1
|
|
132
|
+
bracket++
|
|
133
|
+
}
|
|
134
|
+
while (tokenIndex < ast.tokens.length && ast.tokens[tokenIndex].end <= first) tokenIndex++
|
|
135
|
+
|
|
136
|
+
const token = ast.tokens[tokenIndex]
|
|
137
|
+
const insideToken = (token && token.start < first && VERBATIM.has(label(token))) || inTemplateText(lineStart)
|
|
138
|
+
const textRegion = markup.filter(region => inText(region, first)).at(-1)
|
|
139
|
+
|
|
140
|
+
if (insideToken || line.trim() === "") {
|
|
141
|
+
result.push(insideToken ? null : 0)
|
|
142
|
+
} else {
|
|
143
|
+
let lineDepth = Math.max(0, depth)
|
|
144
|
+
for (const region of markup) {
|
|
145
|
+
for (const hole of region.holes) {
|
|
146
|
+
if (first >= hole.start && first < hole.end) lineDepth += hole.depth
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const trimmed = line.trimStart()
|
|
151
|
+
if (textRegion) {
|
|
152
|
+
lineDepth += textRegion.depthAt(first) - (trimmed.startsWith("</") ? 1 : 0)
|
|
153
|
+
} else if (token && token.start === first && CLOSERS.has(label(token))) {
|
|
154
|
+
lineDepth--
|
|
155
|
+
} else if (/^\|(?!\|)|^\.[A-Za-z_$]/.test(trimmed)) {
|
|
156
|
+
lineDepth++
|
|
157
|
+
}
|
|
158
|
+
result.push(Math.max(0, lineDepth))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
lineStart += line.length + 1
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return result
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function depthsFromText(code) {
|
|
4
168
|
const info = [{ reindentable: true, depth: 0 }]
|
|
5
169
|
let state = "code"
|
|
6
170
|
let depth = 0
|
|
@@ -50,24 +214,31 @@ function analyze(code) {
|
|
|
50
214
|
}
|
|
51
215
|
}
|
|
52
216
|
|
|
53
|
-
|
|
217
|
+
const lines = code.split("\n")
|
|
218
|
+
return lines.map((line, index) => {
|
|
219
|
+
const entry = info[index]
|
|
220
|
+
if (!entry?.reindentable) return null
|
|
221
|
+
const trimmed = line.trim()
|
|
222
|
+
if (trimmed === "") return 0
|
|
223
|
+
return /^[}\])]/.test(trimmed) ? Math.max(0, entry.depth - 1) : entry.depth
|
|
224
|
+
})
|
|
54
225
|
}
|
|
55
226
|
|
|
56
227
|
export function formatSlim(code) {
|
|
57
228
|
const normalized = code.replace(/\r\n/g, "\n")
|
|
58
|
-
const lines = normalized.split("\n")
|
|
59
|
-
const perLine = analyze(normalized)
|
|
60
229
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
230
|
+
let depths
|
|
231
|
+
try {
|
|
232
|
+
depths = depthsFromSyntax(normalized)
|
|
233
|
+
} catch {
|
|
234
|
+
depths = depthsFromText(normalized)
|
|
235
|
+
}
|
|
64
236
|
|
|
237
|
+
const out = normalized.split("\n").map((line, index) => {
|
|
238
|
+
const depth = depths[index]
|
|
239
|
+
if (depth === null || depth === undefined) return line
|
|
65
240
|
const trimmed = line.trim()
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
let depth = info.depth
|
|
69
|
-
if (/^[}\])]/.test(trimmed)) depth = Math.max(0, depth - 1)
|
|
70
|
-
return " ".repeat(depth) + trimmed
|
|
241
|
+
return trimmed === "" ? "" : " ".repeat(depth) + trimmed
|
|
71
242
|
})
|
|
72
243
|
|
|
73
244
|
return out.join("\n").replace(/\n+$/, "") + "\n"
|
|
@@ -1,43 +1,16 @@
|
|
|
1
1
|
import { formatError } from "../external/classErrors.js"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
if (err?.tag) {
|
|
5
|
-
console.error(formatError(
|
|
6
|
-
err.tag,
|
|
7
|
-
err.message,
|
|
8
|
-
err.file ?? null,
|
|
9
|
-
err.line ?? null,
|
|
10
|
-
err.col ?? null,
|
|
11
|
-
err.sourceLine ?? null,
|
|
12
|
-
))
|
|
13
|
-
process.exit(1)
|
|
14
|
-
}
|
|
15
|
-
|
|
3
|
+
function report(err) {
|
|
16
4
|
console.error(formatError(
|
|
17
|
-
"Error",
|
|
5
|
+
err?.tag ?? "Error",
|
|
18
6
|
err?.message ?? String(err),
|
|
19
|
-
|
|
7
|
+
err?.tag ? err.file ?? null : null,
|
|
8
|
+
err?.tag ? err.line ?? null : null,
|
|
9
|
+
err?.tag ? err.col ?? null : null,
|
|
10
|
+
err?.tag ? err.sourceLine ?? null : null
|
|
20
11
|
))
|
|
21
12
|
process.exit(1)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
process.on("uncaughtException", (err) => {
|
|
25
|
-
if (err?.tag) {
|
|
26
|
-
console.error(formatError(
|
|
27
|
-
err.tag,
|
|
28
|
-
err.message,
|
|
29
|
-
err.file ?? null,
|
|
30
|
-
err.line ?? null,
|
|
31
|
-
err.col ?? null,
|
|
32
|
-
err.sourceLine ?? null,
|
|
33
|
-
))
|
|
34
|
-
process.exit(1)
|
|
35
|
-
}
|
|
13
|
+
}
|
|
36
14
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
err?.message ?? String(err),
|
|
40
|
-
null, null, null, null
|
|
41
|
-
))
|
|
42
|
-
process.exit(1)
|
|
43
|
-
})
|
|
15
|
+
process.on("unhandledRejection", report)
|
|
16
|
+
process.on("uncaughtException", report)
|