@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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +30 -2
  3. package/src/ast/nodes.js +219 -16
  4. package/src/ast/substituteCapabilityRefs.js +185 -61
  5. package/src/codegen/emitStatementLines.js +98 -54
  6. package/src/codegen/generateExpression.js +339 -36
  7. package/src/codegen/generateMethod.js +5 -3
  8. package/src/codegen/generatePage.js +74 -44
  9. package/src/codegen/generateStatement.js +169 -20
  10. package/src/codegen/generateTemplate.js +183 -101
  11. package/src/codegen/generateTemplateExpression.js +102 -59
  12. package/src/codegen/jsGlobals.js +211 -0
  13. package/src/compileComponent.js +39 -8
  14. package/src/imports/extractImports.js +112 -26
  15. package/src/imports/resolveImports.js +186 -0
  16. package/src/index.js +3 -2
  17. package/src/lexer/Lexer.js +430 -235
  18. package/src/lexer/tokenTypes.js +32 -7
  19. package/src/parser/Parser.js +2176 -325
  20. package/src/parser/languageOptions.js +16 -0
  21. package/src/parser/parseExpressionString.js +23 -7
  22. package/src/parser/parsePageFile.js +139 -58
  23. package/src/parser/parseTemplate.js +350 -62
  24. package/src/renderPageDocument.js +338 -56
  25. package/src/runtime/capability.js +39 -17
  26. package/src/runtime/loadingIndicator.js +91 -29
  27. package/src/runtime/reactive.js +362 -64
  28. package/src/runtime/useApi.js +74 -13
  29. package/src/runtime/useChannel.js +27 -6
  30. package/src/typecheck/buildVirtualTs.js +23 -12
  31. package/src/typecheck/tylix-globals.d.ts +23 -4
  32. package/src/typecheck/typedTemplateExpr.js +195 -53
  33. package/src/typecheckPage.js +28 -4
  34. package/src/codegen/generateExpression.array.test.js +0 -13
  35. package/src/codegen/generateExpression.callexpr.test.js +0 -19
  36. package/src/codegen/generateExpression.test.js +0 -59
  37. package/src/codegen/generateMethod.asyncawait.test.js +0 -37
  38. package/src/codegen/generateMethod.ifstatement.test.js +0 -20
  39. package/src/codegen/generateMethod.test.js +0 -81
  40. package/src/codegen/generatePage.onmount.test.js +0 -46
  41. package/src/codegen/generatePage.test.js +0 -138
  42. package/src/codegen/generateStatement.test.js +0 -31
  43. package/src/codegen/generateTemplate.ifeach.test.js +0 -138
  44. package/src/codegen/generateTemplate.test.js +0 -149
  45. package/src/codegen/generateTemplateExpression.test.js +0 -34
  46. package/src/compileComponent.test.js +0 -121
  47. package/src/imports/resolvePackageComponents.js +0 -41
  48. package/src/lexer/Lexer.test.js +0 -151
  49. package/src/parser/Parser.array.test.js +0 -37
  50. package/src/parser/Parser.asyncawait.test.js +0 -33
  51. package/src/parser/Parser.ifstatement.test.js +0 -20
  52. package/src/parser/Parser.onmount.test.js +0 -18
  53. package/src/parser/Parser.test.js +0 -315
  54. package/src/parser/parseComponent.test.js +0 -53
  55. package/src/parser/parsePageFile.test.js +0 -101
  56. package/src/parser/parseTemplate.ifeach.test.js +0 -33
  57. package/src/parser/parseTemplate.pre.test.js +0 -39
  58. package/src/parser/parseTemplate.test.js +0 -112
  59. package/src/renderPageDocument.test.js +0 -125
  60. package/src/runtime/reactive.test.js +0 -106
  61. package/test/dashboard-syntax.test.js +0 -116
@@ -0,0 +1,16 @@
1
+ // Per-project language switches. Defaults keep legacy behaviour.
2
+ // not: 'tight' -- `not` binds like `!` (legacy, default)
3
+ // not: 'loose' -- the WORD `not` binds looser than is / has / exists
4
+ // (`not items has x`, `not user exists`). The symbol
5
+ // `!` stays tight in both modes.
6
+ export const DEFAULT_LANGUAGE_OPTIONS = Object.freeze({ not: 'tight' })
7
+
8
+ export function normalizeLanguageOptions(options = {}) {
9
+ const not = options?.not ?? DEFAULT_LANGUAGE_OPTIONS.not
10
+ if (not !== 'tight' && not !== 'loose') {
11
+ throw new Error(
12
+ `Invalid language option not: "${not}" (expected "tight" or "loose")`,
13
+ )
14
+ }
15
+ return { ...DEFAULT_LANGUAGE_OPTIONS, ...options, not }
16
+ }
@@ -1,13 +1,29 @@
1
- import { Lexer } from "../lexer/Lexer.js";
2
- import { Parser } from "./Parser.js";
1
+ import { Lexer } from '../lexer/Lexer.js'
2
+ import { Parser } from './Parser.js'
3
+ import { TokenType } from '../lexer/tokenTypes.js'
3
4
 
4
5
  /**
5
6
  * Parses a single expression (no statements) from a raw string, e.g.
6
7
  * the inside of a template interpolation: "count * 2" or "increment".
7
8
  * Reuses Parser's expression grammar directly rather than duplicating it.
9
+ *
10
+ * Anything left over after the expression is an error rather than
11
+ * being silently dropped (`{{ a b }}`, `{{ a(); b() }}`).
8
12
  */
9
- export function parseExpressionString(source) {
10
- const tokens = new Lexer(source).tokenize();
11
- const parser = new Parser(tokens);
12
- return parser.parseExpression();
13
- }
13
+ export function parseExpressionString(source, options = {}) {
14
+ const tokens = new Lexer(source).tokenize()
15
+ const parser = new Parser(tokens, options)
16
+ const expression = parser.parseExpression()
17
+ if (!parser.check(TokenType.EOF)) {
18
+ const next = parser.peek()
19
+ if (next.type === TokenType.SEMICOLON) {
20
+ throw new Error(
21
+ `Only one expression is allowed in a template binding ("${source.trim()}"); to run several statements, put them in an action and call it`,
22
+ )
23
+ }
24
+ throw new Error(
25
+ `Unexpected "${next.value ?? next.type}" in expression "${source.trim()}"`,
26
+ )
27
+ }
28
+ return expression
29
+ }
@@ -1,4 +1,102 @@
1
- const SECTION_KEYWORDS = ['state', 'computed', 'action', 'template', 'style']
1
+ // Locates the top-level `uses | state | computed | action | template |
2
+ // style` section keywords. A keyword only counts when:
3
+ // - it is the first thing on its line, and
4
+ // - it is not followed by `:` `(` `=` or `,` (that makes it a field
5
+ // or attribute name, e.g. `style: "dark"` or `style="..."`), and
6
+ // - before the template: it sits at brace/paren depth 0, so keys
7
+ // inside action bodies and object literals are ignored. Strings,
8
+ // template literals and comments are skipped while tracking depth.
9
+ // - after the template starts: only `style` is looked for (the
10
+ // template body is HTML, where quotes and braces mean nothing).
11
+ const SECTION_RE = /^(uses|state|computed|action|template|style)(?![\w$])/
12
+
13
+ export function findSectionBoundaries(source) {
14
+ const boundaries = []
15
+ const n = source.length
16
+ let i = 0
17
+ let depth = 0
18
+ let lineStart = true
19
+ let inTemplate = false
20
+
21
+ while (i < n) {
22
+ const c = source[i]
23
+
24
+ if (c === '\n') {
25
+ lineStart = true
26
+ i++
27
+ continue
28
+ }
29
+ if (c === ' ' || c === '\t' || c === '\r') {
30
+ i++
31
+ continue
32
+ }
33
+
34
+ if (lineStart) {
35
+ lineStart = false
36
+ const m = SECTION_RE.exec(source.slice(i, i + 16))
37
+ if (m) {
38
+ const keyword = m[1]
39
+ let j = i + keyword.length
40
+ while (source[j] === ' ' || source[j] === '\t') j++
41
+ const next = source[j]
42
+ const isFieldName =
43
+ next === ':' || next === '(' || next === '=' || next === ','
44
+ const allowed = inTemplate ? keyword === 'style' : depth === 0
45
+ if (!isFieldName && allowed) {
46
+ boundaries.push({
47
+ keyword,
48
+ start: i,
49
+ contentStart: i + keyword.length,
50
+ })
51
+ if (keyword === 'template') inTemplate = true
52
+ i += keyword.length
53
+ continue
54
+ }
55
+ }
56
+ }
57
+
58
+ if (inTemplate) {
59
+ i++
60
+ continue
61
+ }
62
+
63
+ // --- script-mode scanning ---
64
+ if (c === '/' && source[i + 1] === '/') {
65
+ while (i < n && source[i] !== '\n') i++
66
+ continue
67
+ }
68
+ if (c === '/' && source[i + 1] === '*') {
69
+ const close = source.indexOf('*/', i + 2)
70
+ i = close === -1 ? n : close + 2
71
+ continue
72
+ }
73
+ if (c === '"' || c === "'") {
74
+ i++
75
+ // A quote never spans lines, so a stray one (inside a regex
76
+ // literal, say) can't swallow the rest of the file.
77
+ while (i < n && source[i] !== c && source[i] !== '\n') {
78
+ if (source[i] === '\\') i++
79
+ i++
80
+ }
81
+ if (source[i] === c) i++
82
+ continue
83
+ }
84
+ if (c === '`') {
85
+ i++
86
+ while (i < n && source[i] !== '`') {
87
+ if (source[i] === '\\') i++
88
+ i++
89
+ }
90
+ i++
91
+ continue
92
+ }
93
+ if (c === '{' || c === '(' || c === '[') depth++
94
+ else if (c === '}' || c === ')' || c === ']') depth = Math.max(0, depth - 1)
95
+ i++
96
+ }
97
+
98
+ return boundaries
99
+ }
2
100
 
3
101
  export function parsePageFile(source) {
4
102
  const pageMatch = /^\s*page\s+([A-Za-z_$][A-Za-z0-9_$]*)/.exec(source)
@@ -12,10 +110,24 @@ export function parsePageFile(source) {
12
110
  )
13
111
 
14
112
  const boundaries = findSectionBoundaries(afterPageDeclaration)
113
+ const templateBoundary = boundaries.find((b) => b.keyword === 'template')
114
+ const styleBoundary = boundaries.find((b) => b.keyword === 'style')
115
+
116
+ if (!templateBoundary) {
117
+ throw new Error('.tyx file is missing a required "template" section.')
118
+ }
15
119
 
16
- const scriptEnd =
17
- boundaries.find((b) => b.keyword === 'template')?.start ??
18
- afterPageDeclaration.length
120
+ // A section's content runs until the next template/style keyword,
121
+ // or the end of the file -- so `style` written before `template`
122
+ // no longer swallows the template.
123
+ const contentBoundaries = [templateBoundary, styleBoundary].filter(Boolean)
124
+ const endOf = (b) =>
125
+ contentBoundaries
126
+ .filter((x) => x.start > b.start)
127
+ .map((x) => x.start)
128
+ .sort((a, c) => a - c)[0] ?? afterPageDeclaration.length
129
+
130
+ const scriptEnd = Math.min(...contentBoundaries.map((b) => b.start))
19
131
  const rawScriptSource = afterPageDeclaration.slice(0, scriptEnd)
20
132
  const scriptSource = rawScriptSource.trim()
21
133
 
@@ -27,44 +139,30 @@ export function parsePageFile(source) {
27
139
  1
28
140
  const scriptStartLine = linesBeforeScript + trimmedPrefix.split('\n').length
29
141
 
30
- const templateBoundary = boundaries.find((b) => b.keyword === 'template')
31
- const styleBoundary = boundaries.find((b) => b.keyword === 'style')
32
-
33
- let template = ''
34
- let templateStartLine = null
35
- if (templateBoundary) {
36
- const templateEnd =
37
- styleBoundary && styleBoundary.start > templateBoundary.start
38
- ? styleBoundary.start
39
- : afterPageDeclaration.length
40
- const rawTemplateSource = afterPageDeclaration.slice(
41
- templateBoundary.contentStart,
42
- templateEnd,
43
- )
44
- template = rawTemplateSource.trim()
45
-
46
- const templateLeadingWhitespaceLength =
47
- rawTemplateSource.length - rawTemplateSource.trimStart().length
48
- const templateTrimmedPrefix = rawTemplateSource.slice(
49
- 0,
50
- templateLeadingWhitespaceLength,
51
- )
52
- const absoluteContentStart =
53
- pageMatch.index + pageMatch[0].length + templateBoundary.contentStart
54
- const linesBeforeTemplate =
55
- source.slice(0, absoluteContentStart).split('\n').length - 1
56
- templateStartLine =
57
- linesBeforeTemplate + templateTrimmedPrefix.split('\n').length
58
- }
142
+ const rawTemplateSource = afterPageDeclaration.slice(
143
+ templateBoundary.contentStart,
144
+ endOf(templateBoundary),
145
+ )
146
+ const template = rawTemplateSource.trim()
59
147
 
60
- let style = ''
61
- if (styleBoundary) {
62
- style = afterPageDeclaration.slice(styleBoundary.contentStart).trim()
63
- }
148
+ const templateLeadingWhitespaceLength =
149
+ rawTemplateSource.length - rawTemplateSource.trimStart().length
150
+ const templateTrimmedPrefix = rawTemplateSource.slice(
151
+ 0,
152
+ templateLeadingWhitespaceLength,
153
+ )
154
+ const absoluteContentStart =
155
+ pageMatch.index + pageMatch[0].length + templateBoundary.contentStart
156
+ const linesBeforeTemplate =
157
+ source.slice(0, absoluteContentStart).split('\n').length - 1
158
+ const templateStartLine =
159
+ linesBeforeTemplate + templateTrimmedPrefix.split('\n').length
64
160
 
65
- if (!templateBoundary) {
66
- throw new Error('.tyx file is missing a required "template" section.')
67
- }
161
+ const style = styleBoundary
162
+ ? afterPageDeclaration
163
+ .slice(styleBoundary.contentStart, endOf(styleBoundary))
164
+ .trim()
165
+ : ''
68
166
 
69
167
  return {
70
168
  pageName,
@@ -74,21 +172,4 @@ export function parsePageFile(source) {
74
172
  scriptStartLine,
75
173
  templateStartLine,
76
174
  }
77
- }
78
-
79
- export function findSectionBoundaries(source) {
80
- const boundaries = []
81
- const lineStartPattern =
82
- /(^|\n)[ \t]*(uses|state|computed|action|template|style)\b/g
83
- let match
84
- while ((match = lineStartPattern.exec(source)) !== null) {
85
- const keyword = match[2]
86
- const keywordStart = match.index + match[0].indexOf(keyword)
87
- boundaries.push({
88
- keyword,
89
- start: keywordStart,
90
- contentStart: keywordStart + keyword.length,
91
- })
92
- }
93
- return boundaries
94
- }
175
+ }