@tylix/compiler 0.5.2 → 0.7.0
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/LICENSE +21 -0
- package/package.json +30 -2
- package/src/ast/nodes.js +219 -16
- package/src/ast/substituteCapabilityRefs.js +185 -61
- package/src/codegen/emitStatementLines.js +98 -54
- package/src/codegen/generateExpression.js +339 -36
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +169 -20
- package/src/codegen/generateTemplate.js +183 -101
- package/src/codegen/generateTemplateExpression.js +102 -59
- package/src/codegen/jsGlobals.js +211 -0
- package/src/compileComponent.js +39 -8
- package/src/imports/extractImports.js +112 -26
- package/src/imports/resolveImports.js +186 -0
- package/src/index.js +3 -2
- package/src/lexer/Lexer.js +430 -235
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +2176 -325
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +23 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +350 -62
- package/src/renderPageDocument.js +338 -56
- package/src/runtime/capability.js +39 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +362 -64
- package/src/runtime/useApi.js +74 -13
- package/src/runtime/useChannel.js +27 -6
- package/src/typecheck/buildVirtualTs.js +23 -12
- package/src/typecheck/tylix-globals.d.ts +23 -4
- package/src/typecheck/typedTemplateExpr.js +195 -53
- package/src/typecheckPage.js +28 -4
- package/src/codegen/generateExpression.array.test.js +0 -13
- package/src/codegen/generateExpression.callexpr.test.js +0 -19
- package/src/codegen/generateExpression.test.js +0 -59
- package/src/codegen/generateMethod.asyncawait.test.js +0 -37
- package/src/codegen/generateMethod.ifstatement.test.js +0 -20
- package/src/codegen/generateMethod.test.js +0 -81
- package/src/codegen/generatePage.onmount.test.js +0 -46
- package/src/codegen/generatePage.test.js +0 -138
- package/src/codegen/generateStatement.test.js +0 -31
- package/src/codegen/generateTemplate.ifeach.test.js +0 -138
- package/src/codegen/generateTemplate.test.js +0 -149
- package/src/codegen/generateTemplateExpression.test.js +0 -34
- package/src/compileComponent.test.js +0 -121
- package/src/imports/resolvePackageComponents.js +0 -41
- package/src/lexer/Lexer.test.js +0 -151
- package/src/parser/Parser.array.test.js +0 -37
- package/src/parser/Parser.asyncawait.test.js +0 -33
- package/src/parser/Parser.ifstatement.test.js +0 -20
- package/src/parser/Parser.onmount.test.js +0 -18
- package/src/parser/Parser.test.js +0 -315
- package/src/parser/parseComponent.test.js +0 -53
- package/src/parser/parsePageFile.test.js +0 -101
- package/src/parser/parseTemplate.ifeach.test.js +0 -33
- package/src/parser/parseTemplate.pre.test.js +0 -39
- package/src/parser/parseTemplate.test.js +0 -112
- package/src/renderPageDocument.test.js +0 -125
- package/src/runtime/reactive.test.js +0 -106
- package/test/dashboard-syntax.test.js +0 -116
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { extractImports } from './extractImports.js'
|
|
4
|
+
|
|
5
|
+
// A component package ships a manifest at its root mapping exported
|
|
6
|
+
// names to the .tyx file that defines them:
|
|
7
|
+
// { "IconCheck": "icons/IconCheck.tyx" }
|
|
8
|
+
const MANIFEST_FILE = 'tylix-components.json'
|
|
9
|
+
const PACKAGE_NAME_RE = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/
|
|
10
|
+
|
|
11
|
+
const fileCache = new Map() // absolute path -> { mtimeMs, text }
|
|
12
|
+
|
|
13
|
+
async function readCached(file) {
|
|
14
|
+
const stat = await fs.stat(file)
|
|
15
|
+
const hit = fileCache.get(file)
|
|
16
|
+
if (hit && hit.mtimeMs === stat.mtimeMs) return hit.text
|
|
17
|
+
const text = await fs.readFile(file, 'utf-8')
|
|
18
|
+
fileCache.set(file, { mtimeMs: stat.mtimeMs, text })
|
|
19
|
+
return text
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Walks up from startDir so packages hoisted into a workspace root
|
|
23
|
+
// resolve too. fs.stat follows symlinks (workspace-linked packages).
|
|
24
|
+
async function findPackageDir(pkgName, startDir) {
|
|
25
|
+
let dir = path.resolve(startDir)
|
|
26
|
+
while (true) {
|
|
27
|
+
const candidate = path.join(dir, 'node_modules', pkgName)
|
|
28
|
+
try {
|
|
29
|
+
if ((await fs.stat(candidate)).isDirectory()) return candidate
|
|
30
|
+
} catch {
|
|
31
|
+
// keep walking up
|
|
32
|
+
}
|
|
33
|
+
const parent = path.dirname(dir)
|
|
34
|
+
if (parent === dir) return null
|
|
35
|
+
dir = parent
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// baseDir: project root (local imports may not escape it).
|
|
40
|
+
// fromFile: absolute path of the importing .tyx file (relative
|
|
41
|
+
// specifiers resolve against its folder).
|
|
42
|
+
// stack: files currently being resolved, used to catch circular
|
|
43
|
+
// imports. Internal: callers leave it out.
|
|
44
|
+
//
|
|
45
|
+
// Returns { [localTagName]: sourceText }. Each source has its own
|
|
46
|
+
// import lines already blanked out (line numbers kept), and whatever
|
|
47
|
+
// those imports pulled in (icons, other components) is included in the
|
|
48
|
+
// result under its own tag name, so a component can import other
|
|
49
|
+
// components and the page does not need to know about them.
|
|
50
|
+
export async function resolveImportedComponents(
|
|
51
|
+
imports,
|
|
52
|
+
{ baseDir, fromFile = null, stack = [] },
|
|
53
|
+
) {
|
|
54
|
+
const root = path.resolve(baseDir)
|
|
55
|
+
const components = {}
|
|
56
|
+
const owner = {}
|
|
57
|
+
|
|
58
|
+
// The same name backed by the same text (two components both importing
|
|
59
|
+
// IconArrowRight) is fine. Different text under one name is an error.
|
|
60
|
+
const add = (local, text, from) => {
|
|
61
|
+
if (Object.hasOwn(components, local)) {
|
|
62
|
+
if (components[local] === text) return
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Component "${local}" is provided by both ${owner[local]} and ${from}`,
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
components[local] = text
|
|
68
|
+
owner[local] = from
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Strips a component file's own imports, resolves them relative to that
|
|
72
|
+
// file, adds what they provide to the result, and returns the cleaned
|
|
73
|
+
// source.
|
|
74
|
+
const expand = async (text, file, label) => {
|
|
75
|
+
let extracted
|
|
76
|
+
try {
|
|
77
|
+
extracted = extractImports(text)
|
|
78
|
+
} catch (err) {
|
|
79
|
+
err.message = `In ${label}: ${err.message}`
|
|
80
|
+
throw err
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (extracted.imports.length > 0) {
|
|
84
|
+
const nested = await resolveImportedComponents(extracted.imports, {
|
|
85
|
+
baseDir,
|
|
86
|
+
fromFile: file,
|
|
87
|
+
stack: [...stack, file],
|
|
88
|
+
})
|
|
89
|
+
for (const [name, nestedText] of Object.entries(nested)) {
|
|
90
|
+
add(name, nestedText, label)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return extracted.source
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const imp of imports) {
|
|
98
|
+
if (imp.kind === 'local') {
|
|
99
|
+
if (!fromFile) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
`Cannot resolve "${imp.specifier}" (line ${imp.line}): the importing file's path was not provided`,
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
let file = path.resolve(path.dirname(fromFile), imp.specifier)
|
|
105
|
+
if (!path.extname(file)) file += '.tyx'
|
|
106
|
+
if (!file.startsWith(root + path.sep)) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Import "${imp.specifier}" (line ${imp.line}) points outside the project`,
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
if (stack.includes(file)) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
`Circular import: ${[...stack, file].map((f) => path.relative(root, f)).join(' -> ')}`,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let text
|
|
118
|
+
try {
|
|
119
|
+
text = await readCached(file)
|
|
120
|
+
} catch {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`Cannot find component file for "${imp.specifier}" imported at line ${imp.line} (looked for ${file})`,
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const source = await expand(text, file, path.relative(root, file))
|
|
127
|
+
add(imp.names[0].local, source, file)
|
|
128
|
+
continue
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// package import
|
|
132
|
+
if (!PACKAGE_NAME_RE.test(imp.specifier)) {
|
|
133
|
+
throw new Error(`Invalid package name "${imp.specifier}" in import`)
|
|
134
|
+
}
|
|
135
|
+
const pkgDir = await findPackageDir(imp.specifier, root)
|
|
136
|
+
if (!pkgDir) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Cannot resolve components from "${imp.specifier}": not found in node_modules (searched from ${root} upwards). Is it installed?`,
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let manifest
|
|
143
|
+
try {
|
|
144
|
+
manifest = JSON.parse(await readCached(path.join(pkgDir, MANIFEST_FILE)))
|
|
145
|
+
} catch {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`Cannot resolve components from "${imp.specifier}": missing or invalid ${MANIFEST_FILE} in ${pkgDir}`,
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
for (const { imported, local } of imp.names) {
|
|
152
|
+
if (
|
|
153
|
+
!Object.hasOwn(manifest, imported) ||
|
|
154
|
+
typeof manifest[imported] !== 'string'
|
|
155
|
+
) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
`"${imp.specifier}" does not export a component named "${imported}" (checked ${MANIFEST_FILE})`,
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
const file = path.resolve(pkgDir, manifest[imported])
|
|
161
|
+
if (!file.startsWith(pkgDir + path.sep)) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`"${imp.specifier}" maps "${imported}" outside its own directory (${manifest[imported]})`,
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
if (stack.includes(file)) {
|
|
167
|
+
throw new Error(`Circular import: ${[...stack, file].join(' -> ')}`)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const label = `${imp.specifier}:${imported}`
|
|
171
|
+
const source = await expand(await readCached(file), file, label)
|
|
172
|
+
add(local, source, label)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return components
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// One call for the whole job: strip the import lines and load what
|
|
180
|
+
// they point at.
|
|
181
|
+
export async function collectImports(source, options) {
|
|
182
|
+
const { imports, source: cleaned } = extractImports(source)
|
|
183
|
+
const components =
|
|
184
|
+
imports.length > 0 ? await resolveImportedComponents(imports, options) : {}
|
|
185
|
+
return { source: cleaned, components }
|
|
186
|
+
}
|
package/src/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './parser/parseComponent.js'
|
|
|
2
2
|
export * from './parser/Parser.js'
|
|
3
3
|
export * from './parser/parseTemplate.js'
|
|
4
4
|
export * from './parser/parseExpressionString.js'
|
|
5
|
+
export * from './parser/languageOptions.js'
|
|
5
6
|
export * from './lexer/Lexer.js'
|
|
6
7
|
export * from './lexer/tokenTypes.js'
|
|
7
8
|
export * from './ast/nodes.js'
|
|
@@ -18,6 +19,6 @@ export * from './typecheck/typecheckVirtualFile.js'
|
|
|
18
19
|
export * from './typecheckPage.js'
|
|
19
20
|
export * from './typecheck/transpileHelperSource.js'
|
|
20
21
|
export * from './imports/extractImports.js'
|
|
21
|
-
export * from './imports/
|
|
22
|
+
export * from './imports/resolveImports.js'
|
|
22
23
|
export * from './runtime/capability.js'
|
|
23
|
-
export * from './codegen/generateCapability.js'
|
|
24
|
+
export * from './codegen/generateCapability.js'
|