@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
package/src/transform.js
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
|
-
import { parse } from "@babel/parser"
|
|
2
1
|
import _traverse from "@babel/traverse"
|
|
3
2
|
import _generate from "@babel/generator"
|
|
4
|
-
import
|
|
5
|
-
import { preprocess } from "./parser.js"
|
|
3
|
+
import { parseProgram } from "./syntax/index.js"
|
|
6
4
|
import { emitJsDoc, emitDeclarations, jsdocComment } from "./jsdoc.js"
|
|
7
5
|
import { checkTypes, formatDiagnostics } from "./checker.js"
|
|
6
|
+
import { compileReactivity, reactiveExports } from "./reactivity.js"
|
|
7
|
+
import { readType, formatType, referenceName } from "./external/typeSyntax.js"
|
|
8
8
|
import * as t from "@babel/types"
|
|
9
9
|
import path from "node:path"
|
|
10
10
|
import { getDistPath, resolveSlimImport, resolveSlimSource, distDirName } from "./modulePaths.js"
|
|
11
|
-
import {
|
|
12
|
-
PRE_SOURCE,
|
|
13
|
-
buildPreMap,
|
|
14
|
-
computeLineStarts,
|
|
15
|
-
mapPreToOriginal
|
|
16
|
-
} from "./sourcemap.js"
|
|
17
11
|
|
|
18
12
|
const traverse = _traverse.default ?? _traverse
|
|
19
13
|
const generate = _generate.default ?? _generate
|
|
@@ -22,39 +16,28 @@ function isRuntimeCall(node, name) {
|
|
|
22
16
|
return t.isCallExpression(node) && t.isIdentifier(node.callee, { name })
|
|
23
17
|
}
|
|
24
18
|
|
|
25
|
-
function getTypeReferenceName(typeText) {
|
|
26
|
-
const withoutArray = typeText.replace(/(?:\[\])+$/, "")
|
|
27
|
-
const withoutGeneric = withoutArray.replace(/<.*>$/, "")
|
|
28
|
-
return withoutGeneric.split("::")[0]
|
|
29
|
-
}
|
|
30
|
-
|
|
31
19
|
const BUILTIN_TYPE_NAMES = new Set([
|
|
32
20
|
"int", "float", "number", "string", "bool", "null", "undefined",
|
|
33
21
|
"object", "array", "function", "any", "element"
|
|
34
22
|
])
|
|
35
23
|
|
|
36
24
|
function buildTypeSpec(typeText, path_, importedNames = new Set()) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
typeText = typeText.slice(0, -1).trim() + " | null | undefined"
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const isIntersection = typeText.includes("&") && !typeText.includes("|")
|
|
43
|
-
const separator = isIntersection ? "&" : "|"
|
|
25
|
+
const type = readType(typeText)
|
|
26
|
+
const parts = type.kind === "union" || type.kind === "intersection" ? type.types : [type]
|
|
44
27
|
|
|
45
|
-
const refs =
|
|
46
|
-
const
|
|
47
|
-
const binding =
|
|
48
|
-
const args = [t.stringLiteral(
|
|
28
|
+
const refs = parts.map(part => {
|
|
29
|
+
const name = referenceName(part)
|
|
30
|
+
const binding = name ? path_.scope.getBinding(name) : null
|
|
31
|
+
const args = [t.stringLiteral(formatType(part))]
|
|
49
32
|
|
|
50
|
-
if ((binding || importedNames.has(
|
|
51
|
-
args.push(t.arrowFunctionExpression([], t.identifier(
|
|
33
|
+
if ((binding || importedNames.has(name)) && !BUILTIN_TYPE_NAMES.has(name)) {
|
|
34
|
+
args.push(t.arrowFunctionExpression([], t.identifier(name)))
|
|
52
35
|
}
|
|
53
36
|
|
|
54
37
|
return t.callExpression(t.identifier("__type_ref__"), args)
|
|
55
38
|
})
|
|
56
39
|
|
|
57
|
-
const combinator =
|
|
40
|
+
const combinator = type.kind === "intersection" ? "__type_spec_all__" : "__type_spec__"
|
|
58
41
|
return t.callExpression(t.identifier(combinator), refs)
|
|
59
42
|
}
|
|
60
43
|
|
|
@@ -77,6 +60,26 @@ function getMemberName(member) {
|
|
|
77
60
|
return null
|
|
78
61
|
}
|
|
79
62
|
|
|
63
|
+
const UNWRAPPED_IN_RELEASE = new Set(["__typed_variable__", "__typed_pattern__"])
|
|
64
|
+
const REMOVED_IN_RELEASE = new Set(["__typed_parameter__", "__declare_return__"])
|
|
65
|
+
|
|
66
|
+
function stripRuntimeChecks(ast) {
|
|
67
|
+
traverse(ast, {
|
|
68
|
+
ExpressionStatement(path_) {
|
|
69
|
+
const call = path_.node.expression
|
|
70
|
+
if (t.isCallExpression(call) && t.isIdentifier(call.callee) && REMOVED_IN_RELEASE.has(call.callee.name)) {
|
|
71
|
+
path_.remove()
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
CallExpression(path_) {
|
|
75
|
+
const { callee, arguments: args } = path_.node
|
|
76
|
+
if (t.isIdentifier(callee) && UNWRAPPED_IN_RELEASE.has(callee.name) && args[0]) {
|
|
77
|
+
path_.replaceWith(args[0])
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
80
83
|
function instrumentRuntimeTypes(ast, sourceFile, importedNames) {
|
|
81
84
|
const typedBindings = new Map()
|
|
82
85
|
const staticFields = new Map()
|
|
@@ -245,6 +248,8 @@ function instrumentRuntimeTypes(ast, sourceFile, importedNames) {
|
|
|
245
248
|
|
|
246
249
|
if (oneLine && t.isIdentifier(node.arguments[1])) {
|
|
247
250
|
node.arguments[1] = buildTypeSpec(node.arguments[1].name, path_, importedNames)
|
|
251
|
+
} else if (oneLine && t.isStringLiteral(node.arguments[1])) {
|
|
252
|
+
node.arguments[1] = buildTypeSpec(node.arguments[1].value, path_, importedNames)
|
|
248
253
|
}
|
|
249
254
|
|
|
250
255
|
for (const property of properties.properties) {
|
|
@@ -282,6 +287,7 @@ function instrumentRuntimeTypes(ast, sourceFile, importedNames) {
|
|
|
282
287
|
} else if (scopePath.isBlockStatement()) {
|
|
283
288
|
scopePath.unshiftContainer("body", declaration)
|
|
284
289
|
} else if (scopePath.isFunction()) {
|
|
290
|
+
scopePath.ensureBlock()
|
|
285
291
|
scopePath.get("body").unshiftContainer("body", declaration)
|
|
286
292
|
} else if (scopePath.isForStatement() && t.isVariableDeclaration(scopePath.node.init)) {
|
|
287
293
|
scopePath.node.init.declarations.unshift(...declaration.declarations)
|
|
@@ -290,113 +296,52 @@ function instrumentRuntimeTypes(ast, sourceFile, importedNames) {
|
|
|
290
296
|
}
|
|
291
297
|
}
|
|
292
298
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
const
|
|
296
|
-
if (
|
|
297
|
-
const staticField = getStaticField(path_, node.left)
|
|
298
|
-
const fieldName = staticField?.name
|
|
299
|
-
const field = staticField?.definition
|
|
300
|
-
if (!field) return
|
|
301
|
-
|
|
302
|
-
const check = value => t.callExpression(t.identifier("__typed_static_field_check__"), [
|
|
303
|
-
t.cloneNode(node.left.object),
|
|
304
|
-
t.stringLiteral(fieldName),
|
|
305
|
-
value,
|
|
306
|
-
t.stringLiteral(field.displayName)
|
|
307
|
-
])
|
|
308
|
-
|
|
309
|
-
if (node.operator === "=") {
|
|
310
|
-
node.right = check(node.right)
|
|
311
|
-
return
|
|
312
|
-
}
|
|
299
|
+
function checkerFor(path_, target) {
|
|
300
|
+
if (t.isMemberExpression(target)) {
|
|
301
|
+
const field = getStaticField(path_, target)
|
|
302
|
+
if (!field?.definition) return null
|
|
313
303
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
path_.replaceWith(t.logicalExpression(operator, t.cloneNode(node.left), assignment))
|
|
322
|
-
path_.skip()
|
|
323
|
-
return
|
|
324
|
-
}
|
|
304
|
+
return value => t.callExpression(t.identifier("__typed_static_field_check__"), [
|
|
305
|
+
t.cloneNode(target.object),
|
|
306
|
+
t.stringLiteral(field.name),
|
|
307
|
+
value,
|
|
308
|
+
t.stringLiteral(field.definition.displayName)
|
|
309
|
+
])
|
|
310
|
+
}
|
|
325
311
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
312
|
+
if (!t.isIdentifier(target)) return null
|
|
313
|
+
const binding = typedBindings.get(path_.scope.getBinding(target.name))
|
|
314
|
+
return binding ? value => buildTypedCheck(binding, value) : null
|
|
315
|
+
}
|
|
331
316
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
317
|
+
traverse(ast, {
|
|
318
|
+
AssignmentExpression(path_) {
|
|
319
|
+
const { node } = path_
|
|
320
|
+
const check = checkerFor(path_, node.left)
|
|
321
|
+
if (!check) return
|
|
335
322
|
|
|
336
323
|
if (node.operator === "=") {
|
|
337
|
-
node.right =
|
|
324
|
+
node.right = check(node.right)
|
|
338
325
|
return
|
|
339
326
|
}
|
|
340
327
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
t.cloneNode(node.left),
|
|
346
|
-
buildTypedCheck(binding, node.right)
|
|
347
|
-
)
|
|
328
|
+
const operator = node.operator.slice(0, -1)
|
|
329
|
+
|
|
330
|
+
if (["&&", "||", "??"].includes(operator)) {
|
|
331
|
+
const assignment = t.assignmentExpression("=", t.cloneNode(node.left), check(node.right))
|
|
348
332
|
path_.replaceWith(t.logicalExpression(operator, t.cloneNode(node.left), assignment))
|
|
349
333
|
path_.skip()
|
|
350
334
|
return
|
|
351
335
|
}
|
|
352
336
|
|
|
353
|
-
const operator = node.operator.slice(0, -1)
|
|
354
337
|
node.operator = "="
|
|
355
|
-
node.right =
|
|
356
|
-
binding,
|
|
357
|
-
t.binaryExpression(operator, t.cloneNode(node.left), node.right)
|
|
358
|
-
)
|
|
338
|
+
node.right = check(t.binaryExpression(operator, t.cloneNode(node.left), node.right))
|
|
359
339
|
},
|
|
360
340
|
|
|
361
341
|
UpdateExpression(path_) {
|
|
362
342
|
const { node } = path_
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const fieldName = staticField?.name
|
|
366
|
-
const field = staticField?.definition
|
|
367
|
-
if (!field) return
|
|
368
|
-
|
|
369
|
-
const previous = path_.scope.generateUidIdentifier("typedPrevious")
|
|
370
|
-
const nextValue = t.binaryExpression(
|
|
371
|
-
node.operator === "++" ? "+" : "-",
|
|
372
|
-
t.cloneNode(node.argument),
|
|
373
|
-
t.numericLiteral(1)
|
|
374
|
-
)
|
|
375
|
-
const check = t.callExpression(t.identifier("__typed_static_field_check__"), [
|
|
376
|
-
t.cloneNode(node.argument.object),
|
|
377
|
-
t.stringLiteral(fieldName),
|
|
378
|
-
nextValue,
|
|
379
|
-
t.stringLiteral(field.displayName)
|
|
380
|
-
])
|
|
381
|
-
const assignment = t.assignmentExpression("=", t.cloneNode(node.argument), check)
|
|
382
|
-
const result = node.prefix ? t.cloneNode(node.argument) : t.cloneNode(previous)
|
|
383
|
-
|
|
384
|
-
path_.replaceWith(t.callExpression(
|
|
385
|
-
t.arrowFunctionExpression([], t.blockStatement([
|
|
386
|
-
t.variableDeclaration("const", [t.variableDeclarator(previous, t.cloneNode(node.argument))]),
|
|
387
|
-
t.expressionStatement(assignment),
|
|
388
|
-
t.returnStatement(result)
|
|
389
|
-
])),
|
|
390
|
-
[]
|
|
391
|
-
))
|
|
392
|
-
path_.skip()
|
|
393
|
-
return
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
if (!t.isIdentifier(node.argument)) return
|
|
397
|
-
|
|
398
|
-
const binding = typedBindings.get(path_.scope.getBinding(node.argument.name))
|
|
399
|
-
if (!binding) return
|
|
343
|
+
const check = checkerFor(path_, node.argument)
|
|
344
|
+
if (!check) return
|
|
400
345
|
|
|
401
346
|
const previous = path_.scope.generateUidIdentifier("typedPrevious")
|
|
402
347
|
const nextValue = t.binaryExpression(
|
|
@@ -404,11 +349,7 @@ function instrumentRuntimeTypes(ast, sourceFile, importedNames) {
|
|
|
404
349
|
t.cloneNode(node.argument),
|
|
405
350
|
t.numericLiteral(1)
|
|
406
351
|
)
|
|
407
|
-
const assignment = t.assignmentExpression(
|
|
408
|
-
"=",
|
|
409
|
-
t.cloneNode(node.argument),
|
|
410
|
-
buildTypedCheck(binding, nextValue)
|
|
411
|
-
)
|
|
352
|
+
const assignment = t.assignmentExpression("=", t.cloneNode(node.argument), check(nextValue))
|
|
412
353
|
const result = node.prefix ? t.cloneNode(node.argument) : t.cloneNode(previous)
|
|
413
354
|
|
|
414
355
|
path_.replaceWith(t.callExpression(
|
|
@@ -424,12 +365,7 @@ function instrumentRuntimeTypes(ast, sourceFile, importedNames) {
|
|
|
424
365
|
})
|
|
425
366
|
}
|
|
426
367
|
|
|
427
|
-
function
|
|
428
|
-
return resolveSlimImport(raw, fromFile)
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
function getRuntimePath(sourceFile, entry) {
|
|
432
|
-
const distFile = getDistPath(sourceFile)
|
|
368
|
+
function getRuntimePath(distFile, entry) {
|
|
433
369
|
const runtimeAbs = path.resolve(distDirName(), "external", entry)
|
|
434
370
|
const rel = path.relative(path.dirname(distFile), runtimeAbs)
|
|
435
371
|
const relFixed = rel.replace(/\\/g, "/")
|
|
@@ -439,7 +375,8 @@ function getRuntimePath(sourceFile, entry) {
|
|
|
439
375
|
const DOM_RUNTIME = new Set([
|
|
440
376
|
"htmlToVdom", "HTMLElement", "__html__",
|
|
441
377
|
"__flush_events__", "__bind_events__", "__lifecycle__",
|
|
442
|
-
"__create_host__", "__adopt_into__", "__define_element__"
|
|
378
|
+
"__create_host__", "__adopt_into__", "__define_element__",
|
|
379
|
+
"__template__", "__insert__", "__once__", "__root__", "__list__", "__attr__", "__set__", "__on__", "__spread__", "__branch__", "__enter__", "__leave__", "__child__", "__next__", "__move__", "Router", "Link", "Portal", "navigate", "route"
|
|
443
380
|
])
|
|
444
381
|
|
|
445
382
|
function usesDom(ast) {
|
|
@@ -466,7 +403,7 @@ function parseSpecifiers(name, bareIsDefault = true) {
|
|
|
466
403
|
|
|
467
404
|
if (trimmed.startsWith("{")) {
|
|
468
405
|
const inner = trimmed.replace(/[{}]/g, "").trim()
|
|
469
|
-
return inner.split(",").map(part => {
|
|
406
|
+
return inner.split(",").filter(part => part.trim()).map(part => {
|
|
470
407
|
const aliasParts = part.trim().split(/\s+as\s+/)
|
|
471
408
|
const imported = aliasParts[0].trim()
|
|
472
409
|
const local = (aliasParts[1] ?? aliasParts[0]).trim()
|
|
@@ -486,28 +423,31 @@ function parseSpecifiers(name, bareIsDefault = true) {
|
|
|
486
423
|
return [t.importDefaultSpecifier(t.identifier(trimmed))]
|
|
487
424
|
}
|
|
488
425
|
|
|
489
|
-
function formatSyntaxError(err, originalCode, sourceFile
|
|
426
|
+
function formatSyntaxError(err, originalCode, sourceFile) {
|
|
490
427
|
const loc = err.loc
|
|
428
|
+
const atEnd = typeof err.pos === "number" && err.pos >= originalCode.trimEnd().length
|
|
429
|
+
let message = err.slimMessage ??
|
|
430
|
+
(atEnd ? "Unexpected end of file: a bracket or block is not closed" : err.message.replace(/\s*\(\d+:\d+\)$/, ""))
|
|
431
|
+
|
|
432
|
+
if (!err.slimMessage && !atEnd && message.startsWith("Unexpected token") && typeof err.pos === "number") {
|
|
433
|
+
const token = /^(?:[\w$]+|\S)/.exec(originalCode.slice(err.pos))?.[0]
|
|
434
|
+
if (token) message = message.replace("Unexpected token", `Unexpected "${token}"`)
|
|
435
|
+
}
|
|
491
436
|
|
|
492
437
|
if (!loc) {
|
|
493
|
-
const e = new Error(`SyntaxError: ${
|
|
438
|
+
const e = new Error(`SyntaxError: ${message}`)
|
|
494
439
|
e.slimSyntaxError = true
|
|
495
440
|
throw e
|
|
496
441
|
}
|
|
497
442
|
|
|
498
|
-
const
|
|
499
|
-
const
|
|
500
|
-
const
|
|
501
|
-
mapped, preLineStarts, origLineStarts, loc.line - 1, loc.column
|
|
502
|
-
)
|
|
503
|
-
|
|
504
|
-
const lines = originalCode.split("\n")
|
|
505
|
-
const sourceLine = lines[line - 1] ?? ""
|
|
443
|
+
const line = loc.line
|
|
444
|
+
const column = loc.column + 1
|
|
445
|
+
const sourceLine = (originalCode.split("\n")[line - 1] ?? "").replace(/\r$/, "")
|
|
506
446
|
const indent = sourceLine.length - sourceLine.trimStart().length
|
|
507
447
|
const pointer = " ".repeat(Math.max(0, column - 1 - indent)) + "^"
|
|
508
448
|
|
|
509
449
|
const e = new Error([
|
|
510
|
-
`SyntaxError: ${
|
|
450
|
+
`SyntaxError: ${message}`,
|
|
511
451
|
` at ${sourceFile}:${line}:${column}`,
|
|
512
452
|
"",
|
|
513
453
|
` ${sourceLine.trim()}`,
|
|
@@ -517,41 +457,113 @@ function formatSyntaxError(err, originalCode, sourceFile, mapped) {
|
|
|
517
457
|
throw e
|
|
518
458
|
}
|
|
519
459
|
|
|
520
|
-
|
|
460
|
+
function isAsyncFunctionBinding(binding) {
|
|
461
|
+
const node = binding?.path.node
|
|
462
|
+
if (t.isFunctionDeclaration(node)) return node.async
|
|
463
|
+
return t.isVariableDeclarator(node) && t.isFunction(node.init) && node.init.async
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function callsAsyncFunction(path_, call) {
|
|
467
|
+
return t.isCallExpression(call) && t.isIdentifier(call.callee) &&
|
|
468
|
+
isAsyncFunctionBinding(path_.scope.getBinding(call.callee.name))
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function isExport(node) {
|
|
472
|
+
return t.isExportNamedDeclaration(node) || t.isExportDefaultDeclaration(node) || t.isExportAllDeclaration(node)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function catchTopLevel(statements) {
|
|
476
|
+
return t.tryStatement(
|
|
477
|
+
t.blockStatement(statements),
|
|
478
|
+
t.catchClause(t.identifier("__err__"), t.blockStatement([
|
|
479
|
+
t.expressionStatement(t.callExpression(t.identifier("__handle_sync_error__"), [t.identifier("__err__")]))
|
|
480
|
+
]))
|
|
481
|
+
)
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function layoutTopLevel(body) {
|
|
485
|
+
const head = []
|
|
486
|
+
const declared = []
|
|
487
|
+
const ordered = []
|
|
488
|
+
const hasExports = body.some(isExport)
|
|
489
|
+
|
|
490
|
+
const assign = (target, value) =>
|
|
491
|
+
ordered.push(t.expressionStatement(t.assignmentExpression("=", target, value)))
|
|
492
|
+
|
|
493
|
+
const declare = (kind, names, exported) => {
|
|
494
|
+
const declaration = t.variableDeclaration(kind, names.map(name => t.variableDeclarator(t.identifier(name))))
|
|
495
|
+
declared.push(exported ? t.exportNamedDeclaration(declaration, []) : declaration)
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const hoistVariables = (node, exported) => {
|
|
499
|
+
const names = node.declarations.flatMap(declarator => Object.keys(t.getBindingIdentifiers(declarator.id)))
|
|
500
|
+
declare(node.kind === "var" ? "var" : "let", names, exported)
|
|
501
|
+
for (const declarator of node.declarations) {
|
|
502
|
+
if (declarator.init) assign(declarator.id, declarator.init)
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const hoistClass = (node, exported) => {
|
|
507
|
+
declare("let", [node.id.name], exported)
|
|
508
|
+
assign(t.identifier(node.id.name), t.classExpression(node.id, node.superClass, node.body, node.decorators))
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const hoistDefault = node => {
|
|
512
|
+
const declaration = node.declaration
|
|
513
|
+
if (t.isFunctionDeclaration(declaration)) return declared.push(node)
|
|
514
|
+
|
|
515
|
+
let local = "__slim_default__"
|
|
516
|
+
if (t.isClassDeclaration(declaration) && declaration.id) {
|
|
517
|
+
local = declaration.id.name
|
|
518
|
+
hoistClass(declaration, false)
|
|
519
|
+
} else {
|
|
520
|
+
declare("let", [local], false)
|
|
521
|
+
assign(t.identifier(local), t.isClassDeclaration(declaration)
|
|
522
|
+
? t.classExpression(null, declaration.superClass, declaration.body, declaration.decorators)
|
|
523
|
+
: declaration)
|
|
524
|
+
}
|
|
525
|
+
declared.push(t.exportNamedDeclaration(null, [t.exportSpecifier(t.identifier(local), t.identifier("default"))]))
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
for (const node of body) {
|
|
529
|
+
if (t.isImportDeclaration(node) || node._slimTypeSlots) head.push(node)
|
|
530
|
+
else if (!hasExports) ordered.push(node)
|
|
531
|
+
else if (t.isFunctionDeclaration(node)) declared.push(node)
|
|
532
|
+
else if (t.isVariableDeclaration(node)) hoistVariables(node, false)
|
|
533
|
+
else if (t.isClassDeclaration(node) && node.id) hoistClass(node, false)
|
|
534
|
+
else if (t.isExportDefaultDeclaration(node)) hoistDefault(node)
|
|
535
|
+
else if (t.isExportNamedDeclaration(node) && t.isVariableDeclaration(node.declaration)) hoistVariables(node.declaration, true)
|
|
536
|
+
else if (t.isExportNamedDeclaration(node) && t.isClassDeclaration(node.declaration)) hoistClass(node.declaration, true)
|
|
537
|
+
else if (isExport(node)) declared.push(node)
|
|
538
|
+
else ordered.push(node)
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return [...head, ...declared, catchTopLevel(ordered)]
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
export function transform(code, sourceFile = "input.slim", options = {}) {
|
|
545
|
+
const outFile = options.outFile ?? getDistPath(sourceFile)
|
|
521
546
|
const bareIsDefault = options.uses !== "named" && options.uses !== "legacy"
|
|
522
|
-
const asyncFunctions = new Set()
|
|
523
547
|
const imports = new Map()
|
|
524
548
|
const wildcards = []
|
|
525
549
|
const specifierSources = []
|
|
526
550
|
|
|
527
|
-
const { code: pre, mapped } = preprocess(code, sourceFile)
|
|
528
|
-
|
|
529
551
|
let ast
|
|
530
552
|
try {
|
|
531
|
-
ast =
|
|
532
|
-
sourceType: "module",
|
|
533
|
-
plugins: ["jsx"]
|
|
534
|
-
})
|
|
553
|
+
ast = parseProgram(code, sourceFile)
|
|
535
554
|
} catch (err) {
|
|
536
|
-
if (err.code === "BABEL_PARSER_SYNTAX_ERROR")
|
|
537
|
-
formatSyntaxError(err, code, sourceFile, mapped)
|
|
538
|
-
}
|
|
555
|
+
if (err.code === "BABEL_PARSER_SYNTAX_ERROR") formatSyntaxError(err, code, sourceFile)
|
|
539
556
|
throw err
|
|
540
557
|
}
|
|
541
558
|
|
|
542
559
|
traverse(ast, {
|
|
543
|
-
FunctionDeclaration(path_) {
|
|
544
|
-
if (path_.node.async) {
|
|
545
|
-
asyncFunctions.add(path_.node.id?.name)
|
|
546
|
-
}
|
|
547
|
-
},
|
|
548
560
|
CallExpression(path_) {
|
|
549
561
|
const callee = path_.node.callee
|
|
550
562
|
|
|
551
563
|
if (t.isIdentifier(callee) && callee.name === "__use_all__") {
|
|
552
564
|
const [sourceNode] = path_.node.arguments
|
|
553
565
|
if (!t.isStringLiteral(sourceNode)) return
|
|
554
|
-
wildcards.push(
|
|
566
|
+
wildcards.push(resolveSlimImport(sourceNode.value, sourceFile, outFile))
|
|
555
567
|
specifierSources.push({ spec: null, raw: sourceNode.value })
|
|
556
568
|
path_.remove()
|
|
557
569
|
return
|
|
@@ -560,7 +572,7 @@ export function transform(code, sourceFile = "input.ps", options = {}) {
|
|
|
560
572
|
if (t.isIdentifier(callee) && callee.name === "__use__") {
|
|
561
573
|
const [nameNode, sourceNode] = path_.node.arguments
|
|
562
574
|
if (!t.isStringLiteral(nameNode) || !t.isStringLiteral(sourceNode)) return
|
|
563
|
-
imports.set(nameNode.value,
|
|
575
|
+
imports.set(nameNode.value, resolveSlimImport(sourceNode.value, sourceFile, outFile))
|
|
564
576
|
specifierSources.push({ spec: nameNode.value, raw: sourceNode.value })
|
|
565
577
|
path_.remove()
|
|
566
578
|
}
|
|
@@ -574,16 +586,13 @@ export function transform(code, sourceFile = "input.ps", options = {}) {
|
|
|
574
586
|
|
|
575
587
|
ExpressionStatement(path_) {
|
|
576
588
|
const expr = path_.node.expression
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
)
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
[t.identifier("__handle_async_error__")]
|
|
585
|
-
)
|
|
586
|
-
}
|
|
589
|
+
const isVoid = t.isUnaryExpression(expr, { operator: "void" })
|
|
590
|
+
const call = isVoid ? expr.argument : expr
|
|
591
|
+
if (!callsAsyncFunction(path_, call)) return
|
|
592
|
+
|
|
593
|
+
const handled = t.callExpression(t.memberExpression(call, t.identifier("catch")), [t.identifier("__handle_async_error__")])
|
|
594
|
+
if (isVoid) expr.argument = handled
|
|
595
|
+
else path_.node.expression = handled
|
|
587
596
|
}
|
|
588
597
|
})
|
|
589
598
|
|
|
@@ -613,11 +622,12 @@ export function transform(code, sourceFile = "input.ps", options = {}) {
|
|
|
613
622
|
}
|
|
614
623
|
|
|
615
624
|
const diagnostics = checkTypes(ast, {
|
|
616
|
-
mapped,
|
|
617
625
|
originalCode: code,
|
|
618
626
|
sourceFile,
|
|
619
627
|
imports: moduleImports,
|
|
620
|
-
wildcards: moduleWildcards
|
|
628
|
+
wildcards: moduleWildcards,
|
|
629
|
+
locals: [...imports.keys()].flatMap(name => parseSpecifiers(name, bareIsDefault).map(specifier => specifier.local.name)),
|
|
630
|
+
openGlobals: wildcards.length > 0
|
|
621
631
|
})
|
|
622
632
|
if (diagnostics.length > 0) {
|
|
623
633
|
const error = new Error(formatDiagnostics(diagnostics))
|
|
@@ -635,9 +645,32 @@ export function transform(code, sourceFile = "input.ps", options = {}) {
|
|
|
635
645
|
const declarations = options.declarations ? emitDeclarations(ast) : null
|
|
636
646
|
const typedefs = options.jsdoc ? emitJsDoc(ast) : []
|
|
637
647
|
|
|
648
|
+
if (options.release) stripRuntimeChecks(ast)
|
|
638
649
|
instrumentRuntimeTypes(ast, sourceFile, importedNames)
|
|
650
|
+
const importedReactive = new Map()
|
|
651
|
+
for (const { spec, raw } of specifierSources) {
|
|
652
|
+
const slimSource = resolveSlimSource(raw, sourceFile)
|
|
653
|
+
const exported = slimSource ? reactiveExports(slimSource) : null
|
|
654
|
+
if (!exported?.size) continue
|
|
655
|
+
if (spec === null) {
|
|
656
|
+
for (const [name, kind] of exported) importedReactive.set(name, kind)
|
|
657
|
+
continue
|
|
658
|
+
}
|
|
659
|
+
for (const specifier of parseSpecifiers(spec, bareIsDefault)) {
|
|
660
|
+
if (!t.isImportSpecifier(specifier)) continue
|
|
661
|
+
const imported = t.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value
|
|
662
|
+
if (exported.has(imported)) importedReactive.set(specifier.local.name, exported.get(imported))
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
try {
|
|
667
|
+
compileReactivity(ast, code, { sourceFile, importedReactive })
|
|
668
|
+
} catch (err) {
|
|
669
|
+
if (err.code === "BABEL_PARSER_SYNTAX_ERROR") formatSyntaxError(err, code, sourceFile)
|
|
670
|
+
throw err
|
|
671
|
+
}
|
|
639
672
|
|
|
640
|
-
const runtimePath = getRuntimePath(
|
|
673
|
+
const runtimePath = getRuntimePath(outFile, usesDom(ast) ? "defaults.js" : "core.js")
|
|
641
674
|
const defaultImport = t.importDeclaration([], t.stringLiteral(runtimePath))
|
|
642
675
|
|
|
643
676
|
if (options.jsdoc) {
|
|
@@ -668,83 +701,15 @@ export function transform(code, sourceFile = "input.ps", options = {}) {
|
|
|
668
701
|
t.importDeclaration(parseSpecifiers(name, bareIsDefault), t.stringLiteral(source))
|
|
669
702
|
)
|
|
670
703
|
|
|
671
|
-
const existingImports = ast.program.body.filter(n => t.isImportDeclaration(n))
|
|
672
|
-
|
|
673
|
-
const exportDeclarations = ast.program.body.filter(n =>
|
|
674
|
-
t.isExportNamedDeclaration(n) ||
|
|
675
|
-
t.isExportDefaultDeclaration(n) ||
|
|
676
|
-
t.isExportAllDeclaration(n)
|
|
677
|
-
)
|
|
678
|
-
|
|
679
|
-
const exportedNames = new Set()
|
|
680
|
-
for (const node of exportDeclarations) {
|
|
681
|
-
if (t.isExportNamedDeclaration(node) && !node.declaration && node.specifiers) {
|
|
682
|
-
for (const spec of node.specifiers) {
|
|
683
|
-
exportedNames.add(spec.local.name)
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
const slotDecls = []
|
|
689
|
-
const topLevel = []
|
|
690
|
-
const rest = []
|
|
691
|
-
|
|
692
|
-
for (const node of ast.program.body) {
|
|
693
|
-
if (t.isImportDeclaration(node)) continue
|
|
694
|
-
if (t.isExportNamedDeclaration(node) || t.isExportDefaultDeclaration(node) || t.isExportAllDeclaration(node)) continue
|
|
695
|
-
|
|
696
|
-
if (node._slimTypeSlots) {
|
|
697
|
-
slotDecls.push(node)
|
|
698
|
-
} else if (
|
|
699
|
-
(t.isFunctionDeclaration(node) || t.isClassDeclaration(node)) &&
|
|
700
|
-
node.id && exportedNames.has(node.id.name)
|
|
701
|
-
) {
|
|
702
|
-
topLevel.push(node)
|
|
703
|
-
} else {
|
|
704
|
-
rest.push(node)
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
const tryBlock = t.tryStatement(
|
|
709
|
-
t.blockStatement(rest),
|
|
710
|
-
t.catchClause(
|
|
711
|
-
t.identifier("__err__"),
|
|
712
|
-
t.blockStatement([
|
|
713
|
-
t.expressionStatement(
|
|
714
|
-
t.callExpression(
|
|
715
|
-
t.identifier("__handle_sync_error__"),
|
|
716
|
-
[t.identifier("__err__")]
|
|
717
|
-
)
|
|
718
|
-
)
|
|
719
|
-
])
|
|
720
|
-
)
|
|
721
|
-
)
|
|
722
|
-
|
|
723
704
|
ast.program.body = [
|
|
724
705
|
defaultImport,
|
|
725
706
|
...wildcardNodes,
|
|
726
707
|
...importNodes,
|
|
727
|
-
...
|
|
728
|
-
...slotDecls,
|
|
729
|
-
...topLevel,
|
|
730
|
-
...exportDeclarations,
|
|
731
|
-
tryBlock
|
|
708
|
+
...layoutTopLevel(ast.program.body)
|
|
732
709
|
]
|
|
733
710
|
|
|
734
|
-
const { code: output, map
|
|
735
|
-
|
|
736
|
-
{ sourceMaps: true, sourceFileName: PRE_SOURCE },
|
|
737
|
-
pre
|
|
738
|
-
)
|
|
739
|
-
|
|
740
|
-
const preMap = buildPreMap(mapped, code, sourceFile)
|
|
741
|
-
const finalMap = remapping(
|
|
742
|
-
generatedMap,
|
|
743
|
-
file => (file === PRE_SOURCE ? preMap : null)
|
|
744
|
-
)
|
|
745
|
-
|
|
746
|
-
const mapComment = `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(JSON.stringify(finalMap)).toString("base64")
|
|
747
|
-
}`
|
|
711
|
+
const { code: output, map } = generate(ast, { sourceMaps: true, sourceFileName: sourceFile }, code)
|
|
712
|
+
const mapComment = `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(JSON.stringify(map)).toString("base64")}`
|
|
748
713
|
|
|
749
714
|
return { code: output + mapComment, declarations }
|
|
750
715
|
}
|