@syncular/typegen 0.58.0 → 0.60.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 (78) hide show
  1. package/README.md +96 -43
  2. package/dist/cli.js +8 -3
  3. package/dist/emit-dart.d.ts +4 -3
  4. package/dist/emit-dart.js +40 -100
  5. package/dist/emit-kotlin.d.ts +4 -4
  6. package/dist/emit-kotlin.js +41 -99
  7. package/dist/emit-queries-dart.d.ts +1 -1
  8. package/dist/emit-queries-dart.js +17 -26
  9. package/dist/emit-queries-kotlin.d.ts +1 -1
  10. package/dist/emit-queries-kotlin.js +15 -24
  11. package/dist/emit-queries-rust.d.ts +1 -1
  12. package/dist/emit-queries-rust.js +18 -19
  13. package/dist/emit-queries-swift.d.ts +1 -1
  14. package/dist/emit-queries-swift.js +22 -26
  15. package/dist/emit-queries.d.ts +1 -1
  16. package/dist/emit-queries.js +18 -27
  17. package/dist/emit-swift.d.ts +4 -4
  18. package/dist/emit-swift.js +43 -102
  19. package/dist/emit.d.ts +3 -3
  20. package/dist/emit.js +37 -113
  21. package/dist/errors.d.ts +6 -2
  22. package/dist/errors.js +10 -2
  23. package/dist/fmt.js +11 -2
  24. package/dist/generate.d.ts +4 -3
  25. package/dist/generate.js +61 -73
  26. package/dist/init.js +5 -6
  27. package/dist/ir.d.ts +14 -2
  28. package/dist/ir.js +67 -49
  29. package/dist/lower.d.ts +0 -7
  30. package/dist/lower.js +0 -62
  31. package/dist/lsp.js +6 -1
  32. package/dist/manifest.d.ts +1 -2
  33. package/dist/manifest.js +13 -12
  34. package/dist/migration-lock.d.ts +32 -23
  35. package/dist/migration-lock.js +115 -92
  36. package/dist/naming.d.ts +25 -0
  37. package/dist/naming.js +92 -0
  38. package/dist/query.d.ts +94 -14
  39. package/dist/query.js +642 -461
  40. package/dist/sql.js +18 -3
  41. package/dist/syql-ast.d.ts +2 -0
  42. package/dist/syql-ast.js +7 -1
  43. package/dist/syql-browser.js +7 -6
  44. package/dist/syql-diagnostics.d.ts +1 -0
  45. package/dist/syql-diagnostics.js +1 -0
  46. package/dist/syql-lowering.js +11 -1
  47. package/dist/syql-parser.d.ts +4 -1
  48. package/dist/syql-parser.js +42 -5
  49. package/dist/syql-validator.js +13 -220
  50. package/package.json +3 -3
  51. package/src/cli.ts +9 -3
  52. package/src/emit-dart.ts +44 -116
  53. package/src/emit-kotlin.ts +41 -118
  54. package/src/emit-queries-dart.ts +23 -32
  55. package/src/emit-queries-kotlin.ts +20 -30
  56. package/src/emit-queries-rust.ts +29 -31
  57. package/src/emit-queries-swift.ts +26 -32
  58. package/src/emit-queries.ts +26 -47
  59. package/src/emit-swift.ts +49 -120
  60. package/src/emit.ts +45 -135
  61. package/src/errors.ts +13 -2
  62. package/src/fmt.ts +10 -2
  63. package/src/generate.ts +113 -107
  64. package/src/init.ts +8 -6
  65. package/src/ir.ts +91 -54
  66. package/src/lower.ts +0 -53
  67. package/src/lsp.ts +11 -4
  68. package/src/manifest.ts +22 -18
  69. package/src/migration-lock.ts +192 -137
  70. package/src/naming.ts +154 -0
  71. package/src/query.ts +864 -502
  72. package/src/sql.ts +30 -3
  73. package/src/syql-ast.ts +9 -0
  74. package/src/syql-browser.ts +8 -7
  75. package/src/syql-diagnostics.ts +2 -0
  76. package/src/syql-lowering.ts +16 -1
  77. package/src/syql-parser.ts +73 -8
  78. package/src/syql-validator.ts +21 -279
package/dist/sql.js CHANGED
@@ -169,7 +169,9 @@ function tokenizeStatements(sql, source) {
169
169
  }
170
170
  i = end;
171
171
  }
172
- else if (DIGIT.test(ch)) {
172
+ else if (DIGIT.test(ch) ||
173
+ ((ch === '-' || ch === '+') && DIGIT.test(sql[i + 1] ?? ''))) {
174
+ // A leading sign belongs to the number: `DEFAULT -1` is a literal.
173
175
  let end = i + 1;
174
176
  while (end < sql.length && /[0-9.]/.test(sql[end]))
175
177
  end += 1;
@@ -241,8 +243,8 @@ class Cursor {
241
243
  throw new TypegenError(this.source, `unsupported trailing SQL ${JSON.stringify(token.text)}`);
242
244
  }
243
245
  }
244
- fail(message) {
245
- throw new TypegenError(this.source, message);
246
+ fail(message, code, details) {
247
+ throw new TypegenError(this.source, message, code, details);
246
248
  }
247
249
  }
248
250
  function parseColumnType(cursor, columnName) {
@@ -290,6 +292,19 @@ function parseColumnDef(cursor, allowPrimaryKey) {
290
292
  if (value.kind === 'punct') {
291
293
  cursor.fail(`column ${name}: DEFAULT expressions are unsupported (only literals)`);
292
294
  }
295
+ // A word is a literal only as one of SQLite's keyword literals, so
296
+ // `DEFAULT NOT NULL` cannot swallow the NOT of a constraint.
297
+ if (value.kind === 'word' &&
298
+ ![
299
+ 'NULL',
300
+ 'TRUE',
301
+ 'FALSE',
302
+ 'CURRENT_TIME',
303
+ 'CURRENT_DATE',
304
+ 'CURRENT_TIMESTAMP',
305
+ ].includes(value.text.toUpperCase())) {
306
+ cursor.fail('DEFAULT needs a literal value: a number, a string, NULL, TRUE, FALSE, or CURRENT_TIME/DATE/TIMESTAMP', 'typegen.invalid_default', { column: name, found: value.text });
307
+ }
293
308
  // Literal defaults are accepted and ignored: typegen extracts the
294
309
  // schema shape; running migrations is the host's job.
295
310
  }
@@ -1,4 +1,5 @@
1
1
  /** Stable, span-free semantic AST used by revision-1 conformance fixtures. */
2
+ import type { QueryAcknowledgement } from './query.js';
2
3
  import { type SyqlToken } from './syql-lexer.js';
3
4
  import type { SyqlSyntaxFile, SyqlTemplate, SyqlValueType } from './syql-parser.js';
4
5
  export interface SyqlSemanticToken {
@@ -77,6 +78,7 @@ export type SyqlSemanticDeclaration = {
77
78
  readonly defaultSize: number;
78
79
  readonly maxSize: number;
79
80
  };
81
+ readonly acknowledgements?: readonly QueryAcknowledgement[];
80
82
  };
81
83
  export interface SyqlSemanticFile {
82
84
  readonly kind: 'syql-file';
package/dist/syql-ast.js CHANGED
@@ -1,4 +1,3 @@
1
- /** Stable, span-free semantic AST used by revision-1 conformance fixtures. */
2
1
  import { isSyqlTrivia } from './syql-lexer.js';
3
2
  function semanticType(type) {
4
3
  return type === undefined
@@ -125,6 +124,13 @@ function semanticDeclaration(declaration) {
125
124
  maxSize: declaration.limit.maxSize,
126
125
  },
127
126
  }),
127
+ ...(declaration.acknowledgements.length === 0
128
+ ? {}
129
+ : {
130
+ acknowledgements: declaration.acknowledgements.map((ack) => ({
131
+ ...ack,
132
+ })),
133
+ }),
128
134
  };
129
135
  }
130
136
  /** Return the stable syntax/semantic AST pinned by `spec/syql` fixtures. */
@@ -7,7 +7,7 @@
7
7
  * keep using the normal project generator.
8
8
  */
9
9
  import { formatSyql } from './fmt.js';
10
- import { synthesizeDdl, } from './query.js';
10
+ import { checkQueryPlan, synthesizeDdl, } from './query.js';
11
11
  import { SyqlFrontendError } from './syql-lexer.js';
12
12
  import { lowerSyqlQuery } from './syql-lowering.js';
13
13
  import { parseSyqlSyntaxFile } from './syql-parser.js';
@@ -34,11 +34,12 @@ export function compileSyqlSource(source, ir, db, options = {}) {
34
34
  const file = options.file ?? SYQL_BROWSER_DEFAULT_FILE;
35
35
  const semantic = analyzeSyqlSemantics(singleFileGraph(file, source));
36
36
  const validated = validateSyqlProgram(semantic, ir, db, options.naming);
37
- return {
38
- queries: validated.queries.map((query) => lowerSyqlQuery(query, ir, db, options.naming, {
39
- ...(options.backend === undefined ? {} : { backend: options.backend }),
40
- })),
41
- };
37
+ const queries = validated.queries.map((query) => lowerSyqlQuery(query, ir, db, options.naming, {
38
+ ...(options.backend === undefined ? {} : { backend: options.backend }),
39
+ }));
40
+ for (const query of queries)
41
+ checkQueryPlan(query.analysis, ir, db);
42
+ return { queries };
42
43
  }
43
44
  /** Format one virtual source file with the canonical SYQL formatter. */
44
45
  export function formatSyqlSource(source, file = SYQL_BROWSER_DEFAULT_FILE) {
@@ -25,6 +25,7 @@ export declare const SYQL_DIAGNOSTIC_REMEDIES: {
25
25
  readonly SYQL2010_INVALID_PAGE_RANGE: 'Choose default and maximum page sizes that satisfy the declared range.';
26
26
  readonly SYQL2011_INVALID_PARAMETER: 'Rewrite the parameter using a supported value, optional, range, group, sort, or limit form.';
27
27
  readonly SYQL2012_INVALID_QUERY_BODY: 'Rewrite the query body using the clause order and forms defined by the SYQL grammar.';
28
+ readonly SYQL2013_INVALID_ACKNOWLEDGEMENT: "Write the acknowledgement as allow <finding> <table> '<reason>' directly above a query, once per finding.";
28
29
  readonly SYQL3001_EXPECTED_EMBEDDED_TOKEN: 'Insert the required token in the embedded SQL construct.';
29
30
  readonly SYQL3002_INVALID_BIND: 'Bind a declared input with the :name form in a supported SQL position.';
30
31
  readonly SYQL3003_INVALID_PREDICATE_CALL: 'Call the predicate with declared bind arguments and matching parentheses.';
@@ -16,6 +16,7 @@ export const SYQL_DIAGNOSTIC_REMEDIES = {
16
16
  SYQL2010_INVALID_PAGE_RANGE: 'Choose default and maximum page sizes that satisfy the declared range.',
17
17
  SYQL2011_INVALID_PARAMETER: 'Rewrite the parameter using a supported value, optional, range, group, sort, or limit form.',
18
18
  SYQL2012_INVALID_QUERY_BODY: 'Rewrite the query body using the clause order and forms defined by the SYQL grammar.',
19
+ SYQL2013_INVALID_ACKNOWLEDGEMENT: "Write the acknowledgement as allow <finding> <table> '<reason>' directly above a query, once per finding.",
19
20
  SYQL3001_EXPECTED_EMBEDDED_TOKEN: 'Insert the required token in the embedded SQL construct.',
20
21
  SYQL3002_INVALID_BIND: 'Bind a declared input with the :name form in a supported SQL position.',
21
22
  SYQL3003_INVALID_PREDICATE_CALL: 'Call the predicate with declared bind arguments and matching parentheses.',
@@ -1,6 +1,6 @@
1
1
  /** Revision-1 SYQL logical-to-physical lowering (§§15–17). */
2
2
  import { TypegenError } from './errors.js';
3
- import { buildNamingMap } from './naming.js';
3
+ import { buildNamingMap, validateQueryIdentifier } from './naming.js';
4
4
  import { analyzeStatement, } from './query.js';
5
5
  import { lexSyqlSqlSource } from './syql-lexer.js';
6
6
  import { syqlRangeEndBind, syqlRangeStartBind } from './syql-semantics.js';
@@ -232,6 +232,15 @@ function publicInputs(validated, naming) {
232
232
  maxSize: validated.limit.maxSize,
233
233
  });
234
234
  }
235
+ for (const input of result) {
236
+ for (const langName of [
237
+ input.langName,
238
+ ...(input.kind === 'group' ? input.members : []).map((m) => m.langName),
239
+ ...(input.kind === 'sort' ? input.profiles : []).map((p) => p.langName),
240
+ ]) {
241
+ validateQueryIdentifier(langName, naming.targets, query.module.file, `query ${declaration.name} input`);
242
+ }
243
+ }
235
244
  return result;
236
245
  }
237
246
  function valueOwners(validated) {
@@ -424,6 +433,7 @@ export function lowerSyqlQuery(validated, ir, db, naming = DEFAULT_NAMING, optio
424
433
  file: logical.module.file,
425
434
  reactive: validated.reactive,
426
435
  syql: metadata,
436
+ acknowledgements: logical.declaration.acknowledgements,
427
437
  };
428
438
  return {
429
439
  validated,
@@ -1,5 +1,6 @@
1
1
  /** Revision-1 SYQL container parser (`docs/SYQL.md` §3). */
2
2
  import { type SyqlSourceSpan, type SyqlToken } from './syql-lexer.js';
3
+ import { type QueryAcknowledgement } from './query.js';
3
4
  import { type SyqlEmbeddedTemplate } from './syql-template-parser.js';
4
5
  export type SyqlBaseType = 'string' | 'integer' | 'float' | 'boolean' | 'json' | 'bytes' | 'blob_ref' | 'crdt';
5
6
  export interface SyqlValueType {
@@ -113,6 +114,8 @@ export interface SyqlQueryDeclaration {
113
114
  readonly statement: SyqlTemplate;
114
115
  readonly sort?: SyqlSortSection;
115
116
  readonly limit?: SyqlLimitDeclaration;
117
+ /** Query plan findings acknowledged before the declaration (§14.5). */
118
+ readonly acknowledgements: readonly QueryAcknowledgement[];
116
119
  readonly span: SyqlSourceSpan;
117
120
  readonly nameSpan: SyqlSourceSpan;
118
121
  }
@@ -128,6 +131,6 @@ export interface SyqlSyntaxFile {
128
131
  readonly queries: readonly SyqlQueryDeclaration[];
129
132
  readonly span: SyqlSourceSpan;
130
133
  }
131
- export type SyqlParseErrorCode = 'SYQL2001_EXPECTED_TOKEN' | 'SYQL2002_INVALID_NAME' | 'SYQL2003_RESERVED_NAME' | 'SYQL2004_DUPLICATE_NAME' | 'SYQL2005_INVALID_IMPORT' | 'SYQL2006_EMPTY_TEMPLATE' | 'SYQL2007_FORBIDDEN_SEMICOLON' | 'SYQL2008_INVALID_MEMBER' | 'SYQL2009_INVALID_INTEGER' | 'SYQL2010_INVALID_PAGE_RANGE' | 'SYQL2011_INVALID_PARAMETER' | 'SYQL2012_INVALID_QUERY_BODY';
134
+ export type SyqlParseErrorCode = 'SYQL2001_EXPECTED_TOKEN' | 'SYQL2002_INVALID_NAME' | 'SYQL2003_RESERVED_NAME' | 'SYQL2004_DUPLICATE_NAME' | 'SYQL2005_INVALID_IMPORT' | 'SYQL2006_EMPTY_TEMPLATE' | 'SYQL2007_FORBIDDEN_SEMICOLON' | 'SYQL2008_INVALID_MEMBER' | 'SYQL2009_INVALID_INTEGER' | 'SYQL2010_INVALID_PAGE_RANGE' | 'SYQL2011_INVALID_PARAMETER' | 'SYQL2012_INVALID_QUERY_BODY' | 'SYQL2013_INVALID_ACKNOWLEDGEMENT';
132
135
  /** Parse one `.syql` file using the destructive revision-1 grammar. */
133
136
  export declare function parseSyqlSyntaxFile(file: string, source: string): SyqlSyntaxFile;
@@ -1,5 +1,6 @@
1
1
  /** Revision-1 SYQL container parser (`docs/SYQL.md` §3). */
2
2
  import { isSyqlTrivia, lexSyqlSource, SyqlFrontendError, } from './syql-lexer.js';
3
+ import { QUERY_PLAN_FINDINGS } from './query.js';
3
4
  import { parseSyqlEmbeddedTemplate, } from './syql-template-parser.js';
4
5
  const CAMEL_IDENT_RE = /^[a-z][A-Za-z0-9]*$/;
5
6
  const INTEGER_LITERAL_RE = /^(?:0|[1-9][0-9]*)$/;
@@ -82,20 +83,26 @@ class Parser {
82
83
  continue;
83
84
  }
84
85
  sawDeclaration = true;
85
- if (keyword.text === 'predicate') {
86
+ const acknowledgements = this.#parseAcknowledgements();
87
+ const next = this.#peek();
88
+ if (acknowledgements.length === 0 && next.text === 'predicate') {
86
89
  const declaration = this.#parsePredicate();
87
90
  this.#claimName(fileNames, declaration.name, declaration.nameSpan, 'declaration');
88
91
  declarations.push(declaration);
89
92
  predicates.push(declaration);
90
93
  }
91
- else if (keyword.text === 'query' || keyword.text === 'sync') {
92
- const declaration = this.#parseQuery(keyword.text === 'sync');
94
+ else if (next.text === 'query' || next.text === 'sync') {
95
+ const declaration = this.#parseQuery(next.text === 'sync', acknowledgements);
93
96
  this.#claimName(fileNames, declaration.name, declaration.nameSpan, 'declaration');
94
97
  declarations.push(declaration);
95
98
  queries.push(declaration);
96
99
  }
97
100
  else {
98
- this.#fail('SYQL2008_INVALID_MEMBER', keyword, `expected import, predicate, query, or sync query; found ${JSON.stringify(keyword.text)}`);
101
+ this.#fail(acknowledgements.length > 0
102
+ ? 'SYQL2013_INVALID_ACKNOWLEDGEMENT'
103
+ : 'SYQL2008_INVALID_MEMBER', next, acknowledgements.length > 0
104
+ ? `a query plan acknowledgement precedes a query declaration; found ${JSON.stringify(next.text || 'end of file')}`
105
+ : `expected import, predicate, query, or sync query; found ${JSON.stringify(next.text)}`);
99
106
  }
100
107
  }
101
108
  const first = this.#tokens[0];
@@ -200,7 +207,36 @@ class Parser {
200
207
  nameSpan: name.span,
201
208
  };
202
209
  }
203
- #parseQuery(sync) {
210
+ #parseAcknowledgements() {
211
+ const out = [];
212
+ while (this.#peek().text === 'allow') {
213
+ const start = this.#take();
214
+ const kind = this.#take();
215
+ const finding = QUERY_PLAN_FINDINGS.find((name) => name === kind.text);
216
+ if (finding === undefined) {
217
+ this.#fail('SYQL2013_INVALID_ACKNOWLEDGEMENT', kind, `expected a query plan finding (${QUERY_PLAN_FINDINGS.join(', ')}), found ${JSON.stringify(kind.text || 'end of file')}`);
218
+ }
219
+ const table = finding === 'many_tables' ? undefined : this.#take();
220
+ if (table !== undefined && table.kind !== 'identifier') {
221
+ this.#fail('SYQL2013_INVALID_ACKNOWLEDGEMENT', table, `expected the table the finding names, found ${JSON.stringify(table.text || 'end of file')}`);
222
+ }
223
+ const reason = this.#take();
224
+ const text = reason.text.slice(1, -1).split("''").join("'");
225
+ if (reason.kind !== 'string' || text.trim() === '') {
226
+ this.#fail('SYQL2013_INVALID_ACKNOWLEDGEMENT', reason, 'a query plan acknowledgement ends with a non-blank reason string');
227
+ }
228
+ if (out.some((ack) => ack.finding === finding && ack.table === table?.text)) {
229
+ this.#fail('SYQL2013_INVALID_ACKNOWLEDGEMENT', start, 'the query already acknowledges this finding');
230
+ }
231
+ out.push({
232
+ finding,
233
+ ...(table === undefined ? {} : { table: table.text }),
234
+ reason: text,
235
+ });
236
+ }
237
+ return out;
238
+ }
239
+ #parseQuery(sync, acknowledgements) {
204
240
  const start = sync ? this.#expectText('sync') : this.#peek();
205
241
  this.#expectText('query');
206
242
  const name = this.#parseCamelName('query name');
@@ -257,6 +293,7 @@ class Parser {
257
293
  statement: body.statement,
258
294
  ...(body.sort === undefined ? {} : { sort: body.sort }),
259
295
  ...(body.limit === undefined ? {} : { limit: body.limit }),
296
+ acknowledgements,
260
297
  span: spanBetween(start, body.close),
261
298
  nameSpan: name.span,
262
299
  };
@@ -1,7 +1,6 @@
1
- import { analyzeStatement, inferParamTypeEvidence, PLAIN_REF_RE, scanRelations, splitSelectList, scanTableRefs, stripCommentsAndStrings, } from './query.js';
1
+ import { AGGREGATE_FUNCTIONS, analyzeStatement, inferParamTypeEvidence, PLAIN_REF_RE, scanRelations, scopeProofs, sqlIdentifier, splitSelectList, scanTableRefs, stripCommentsAndStrings, } from './query.js';
2
2
  import { isSyqlTrivia, lexSyqlSqlSource, SyqlFrontendError, } from './syql-lexer.js';
3
3
  import { syqlRangeEndBind, syqlRangeStartBind } from './syql-semantics.js';
4
- const IDENT_SOURCE = '[A-Za-z_][A-Za-z0-9_]*';
5
4
  const IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
6
5
  const SQLITE_SUBJECT_RE = '[A-Za-z_][A-Za-z0-9_$]*';
7
6
  const NONDETERMINISTIC_FUNCTIONS = new Set([
@@ -59,26 +58,6 @@ function suggestion(authored, candidates) {
59
58
  const nearest = nearestName(authored, candidates);
60
59
  return nearest === undefined ? '' : `; did you mean \`${nearest}\`?`;
61
60
  }
62
- function identifierText(token) {
63
- if (token?.kind === 'identifier')
64
- return token.text;
65
- if (token?.kind !== 'quoted-identifier')
66
- return undefined;
67
- if (token.text.startsWith('[') && token.text.endsWith(']')) {
68
- return token.text.slice(1, -1).replaceAll(']]', ']');
69
- }
70
- const quote = token.text[0];
71
- return token.text.slice(1, -1).replaceAll(`${quote}${quote}`, quote ?? '');
72
- }
73
- const AGGREGATE_FUNCTIONS = new Set([
74
- 'avg',
75
- 'count',
76
- 'group_concat',
77
- 'max',
78
- 'min',
79
- 'sum',
80
- 'total',
81
- ]);
82
61
  /**
83
62
  * The function surface of the standard SQLite 3.46.0 amalgamation.
84
63
  *
@@ -733,7 +712,7 @@ class Validator {
733
712
  #sqlSubjectSpan(logical, subject, focus) {
734
713
  const tokens = significant(logical.declaration.statement.tokens);
735
714
  const [qualifier, column] = subject.split('.', 2);
736
- const matches = (token, expected) => identifierText(token)?.toLowerCase() === expected.toLowerCase();
715
+ const matches = (token, expected) => sqlIdentifier(token)?.toLowerCase() === expected.toLowerCase();
737
716
  if (focus === 'table') {
738
717
  for (let index = 0; index < tokens.length - 1; index += 1) {
739
718
  const keyword = tokenLower(tokens[index]);
@@ -834,115 +813,9 @@ class Validator {
834
813
  // bodies are identical because `when` is confined to the outer clauses.
835
814
  const refs = relations.tables;
836
815
  const refScope = (ref) => relations.scopeAt(ref.start);
837
- const cleaned = stripCommentsAndStrings(markerSql);
838
- const structure = this.#inspect(cleaned, logical.declaration.statement.span.file);
839
- const markerScopes = scanRelations(markerSql, this.#ir);
840
- const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
841
- const requiredAt = (index) => {
842
- const scope = markerScopes.scopeAt(index);
843
- if (scope === 'nested')
844
- return false;
845
- const cte = markerScopes.cteScopes.find((candidate) => `cte:${candidate.name}` === scope);
846
- const scopeItems = cte === undefined
847
- ? structure.outer
848
- : structure.tokens.filter((item) => item.depth === 1 &&
849
- item.token.span.start.offset >= cte.start &&
850
- item.token.span.end.offset <= cte.end);
851
- let whereStart;
852
- for (const item of scopeItems) {
853
- if (item.token.span.start.offset >= index)
854
- break;
855
- const lower = tokenLower(item.token);
856
- if (lower === 'where')
857
- whereStart = item.token.span.end.offset;
858
- else if (OUTER_CLAUSE_ENDERS.has(lower ?? ''))
859
- whereStart = undefined;
860
- }
861
- if (whereStart === undefined)
862
- return false;
863
- const clauseEnd = scopeItems.find((item) => item.token.span.start.offset > index &&
864
- OUTER_CLAUSE_ENDERS.has(tokenLower(item.token) ?? ''))?.token.span.start.offset ??
865
- cte?.end ??
866
- cleaned.length;
867
- const matchStack = [];
868
- for (let cursor = whereStart; cursor < index; cursor += 1) {
869
- if (cleaned[cursor] === '(')
870
- matchStack.push(cursor);
871
- else if (cleaned[cursor] === ')')
872
- matchStack.pop();
873
- }
874
- // Parenthesized boolean expressions are valid, but predicates inside a
875
- // CTE or subquery are not outer proof obligations for sync coverage.
876
- if (matchStack.some((open) => /\b(?:SELECT|WITH)\b/i.test(cleaned.slice(open + 1, index)))) {
877
- return false;
878
- }
879
- // Negated equality does not constrain the result to that scope.
880
- const prefix = cleaned.slice(whereStart, index);
881
- const boundaries = [...prefix.matchAll(/\b(?:AND|OR)\b/gi)].map((match) => (match.index ?? -1) + match[0].length);
882
- const lastBoundary = Math.max(prefix.lastIndexOf('('), ...boundaries);
883
- if (/\bNOT\b/i.test(prefix.slice(lastBoundary + 1)))
884
- return false;
885
- if (matchStack.some((open) => /\bNOT\s*$/i.test(cleaned.slice(whereStart, open)))) {
886
- return false;
887
- }
888
- const stack = [];
889
- for (let cursor = whereStart; cursor < clauseEnd; cursor += 1) {
890
- const char = cleaned[cursor];
891
- if (char === '(')
892
- stack.push(cursor);
893
- else if (char === ')')
894
- stack.pop();
895
- if ((cursor === 0 || !/[A-Za-z0-9_:]/.test(cleaned[cursor - 1] ?? '')) &&
896
- /^OR\b/i.test(cleaned.slice(cursor)) &&
897
- stack.length <= matchStack.length &&
898
- stack.every((open, stackIndex) => matchStack[stackIndex] === open)) {
899
- return false;
900
- }
901
- }
902
- return true;
903
- };
904
- const joinEqualityContextAt = (index) => {
905
- const prefix = cleaned.slice(0, index);
906
- const clauses = [
907
- ...prefix.matchAll(/\b(ON|JOIN|WHERE|GROUP\s+BY|ORDER\s+BY|HAVING|LIMIT|UNION|EXCEPT|INTERSECT)\b/gi),
908
- ];
909
- const last = clauses.at(-1);
910
- if (last?.[1]?.toUpperCase() !== 'ON')
911
- return undefined;
912
- const start = (last.index ?? 0) + last[0].length;
913
- const next = /\b(?:JOIN|WHERE|GROUP\s+BY|ORDER\s+BY|HAVING|LIMIT|UNION|EXCEPT|INTERSECT)\b/i.exec(cleaned.slice(index));
914
- const end = next === null ? cleaned.length : index + next.index;
915
- const clause = cleaned.slice(start, end);
916
- if (/\b(?:OR|NOT|SELECT|WITH)\b/i.test(clause))
917
- return undefined;
918
- const join = clauses.at(-2);
919
- if (join?.[1]?.toUpperCase() !== 'JOIN')
920
- return undefined;
921
- const joinIndex = join.index ?? 0;
922
- const outerKind = /\b(LEFT|RIGHT|FULL)(?:\s+OUTER)?\s*$/i
923
- .exec(prefix.slice(0, joinIndex))?.[1]
924
- ?.toUpperCase();
925
- if (outerKind === undefined)
926
- return { kind: 'inner' };
927
- // A FULL join preserves both operands, so its ON constrains neither.
928
- if (outerKind === 'FULL')
929
- return undefined;
930
- const introducedAlias = (prefix
931
- .slice(joinIndex + join[0].length, last.index)
932
- .match(new RegExp(IDENT_SOURCE, 'g')) ?? [])
933
- .filter((word) => word.toUpperCase() !== 'AS')
934
- .at(-1)
935
- ?.toLowerCase();
936
- if (introducedAlias === undefined)
937
- return undefined;
938
- return {
939
- kind: 'outer',
940
- introducedAlias,
941
- nullExtended: outerKind === 'LEFT' ? 'introduced' : 'prior',
942
- };
943
- };
944
- const refByAlias = new Map(refs.map((ref) => [`${refScope(ref)}\0${ref.alias.toLowerCase()}`, ref]));
816
+ const proofs = scopeProofs(markerSql, this.#ir);
945
817
  const scopeNode = (scope, alias, column) => `${scope}\0${alias.toLowerCase()}\0${column.toLowerCase()}`;
818
+ const nodeOf = (column) => scopeNode(column.scope, column.alias, column.column);
946
819
  const parents = new Map();
947
820
  const find = (node) => {
948
821
  const parent = parents.get(node);
@@ -1000,55 +873,12 @@ class Validator {
1000
873
  }
1001
874
  return seen;
1002
875
  };
1003
- const scopeColumn = (scope, alias, column) => {
1004
- const ref = refByAlias.get(`${scope}\0${alias.toLowerCase()}`);
1005
- const table = this.#ir.tables.find((item) => item.name === ref?.table);
1006
- return (table?.scopes.some((scope) => scope.column.toLowerCase() === column.toLowerCase()) === true);
1007
- };
1008
- // Scope inheritance across a join relies on a satisfied predicate proving
1009
- // both operands non-NULL. `IS` is null-safe (`NULL IS NULL` holds), so it
1010
- // would let a null-extended row on an outer join's preserved side carry a
1011
- // scope proof it never satisfies; only `=`/`==` qualify here.
1012
- const qualifiedEquality = new RegExp(`(${IDENT_SOURCE})\\.(${IDENT_SOURCE})\\s*(?:=|==)\\s*(${IDENT_SOURCE})\\.(${IDENT_SOURCE})`, 'gi');
1013
- for (const match of cleaned.matchAll(qualifiedEquality)) {
1014
- const leftAlias = match[1];
1015
- const leftColumn = match[2];
1016
- const rightAlias = match[3];
1017
- const rightColumn = match[4];
1018
- const index = match.index ?? 0;
1019
- const scope = markerScopes.scopeAt(index);
1020
- if (!scopeColumn(scope, leftAlias, leftColumn) ||
1021
- !scopeColumn(scope, rightAlias, rightColumn)) {
1022
- continue;
1023
- }
1024
- const leftNode = scopeNode(scope, leftAlias, leftColumn);
1025
- const rightNode = scopeNode(scope, rightAlias, rightColumn);
1026
- if (requiredAt(index)) {
1027
- union(leftNode, rightNode);
1028
- continue;
1029
- }
1030
- const context = joinEqualityContextAt(index);
1031
- if (context === undefined)
1032
- continue;
1033
- if (context.kind === 'inner') {
1034
- // An inner join's ON filters every result row, so the equality
1035
- // constrains both operands symmetrically.
1036
- union(leftNode, rightNode);
1037
- continue;
1038
- }
1039
- const leftIntroduced = leftAlias.toLowerCase() === context.introducedAlias;
1040
- const rightIntroduced = rightAlias.toLowerCase() === context.introducedAlias;
1041
- // An equality with both operands on one side of the outer join proves
1042
- // nothing across it; preserved-side rows appear regardless of the ON.
1043
- if (leftIntroduced === rightIntroduced)
1044
- continue;
1045
- const introducedNode = leftIntroduced ? leftNode : rightNode;
1046
- const priorNode = leftIntroduced ? rightNode : leftNode;
1047
- if (context.nullExtended === 'introduced') {
1048
- addInheritEdge(introducedNode, priorNode);
876
+ for (const equality of proofs.equalities) {
877
+ if (equality.symmetric) {
878
+ union(nodeOf(equality.target), nodeOf(equality.source));
1049
879
  }
1050
880
  else {
1051
- addInheritEdge(priorNode, introducedNode);
881
+ addInheritEdge(nodeOf(equality.target), nodeOf(equality.source));
1052
882
  }
1053
883
  }
1054
884
  const required = new Set([...symbols.values()].flatMap((symbol) => symbol.parameter.kind === 'value' &&
@@ -1064,48 +894,11 @@ class Validator {
1064
894
  continue;
1065
895
  const inferred = [];
1066
896
  for (const scope of table.scopes) {
1067
- const alias = escapeRegex(ref.alias);
1068
- const column = escapeRegex(scope.column);
1069
- const unqualifiedMatches = refs.filter((candidate) => {
1070
- if (refScope(candidate) !== refScope(ref))
1071
- return false;
1072
- const other = this.#ir.tables.find((item) => item.name === candidate.table);
1073
- return other?.columns.some((item) => item.name === scope.column);
1074
- }).length;
1075
- const subject = unqualifiedMatches === 1
1076
- ? `(?<![A-Za-z0-9_.:])(?:${alias}\\.)?${column}\\b`
1077
- : `(?<![A-Za-z0-9_.:])${alias}\\.${column}\\b`;
1078
- const found = [];
1079
- const equality = new RegExp(`${subject}\\s*(?:=|==|\\bIS\\b)\\s*:([A-Za-z_][A-Za-z0-9_]*)\\b|:([A-Za-z_][A-Za-z0-9_]*)\\b\\s*(?:=|==)\\s*${subject}`, 'gi');
1080
- const inScope = (index) => markerScopes.scopeAt(index) === refScope(ref) && requiredAt(index);
1081
- for (const match of cleaned.matchAll(equality)) {
1082
- if (!inScope(match.index))
1083
- continue;
1084
- const name = match[1] ?? match[2];
1085
- if (name !== undefined && required.has(name)) {
1086
- found.push({ name, operator: 'equal' });
1087
- }
1088
- }
1089
- const inList = new RegExp(`${subject}\\s+IN\\s*\\(([^)]*)\\)`, 'gi');
1090
- for (const match of cleaned.matchAll(inList)) {
1091
- if (!inScope(match.index))
1092
- continue;
1093
- const body = match[1] ?? '';
1094
- const bodyStart = match.index + match[0].length - body.length - 1;
1095
- const bodyTokens = significant(lexSyqlSqlSource(logical.declaration.statement.span.file, markerSql.slice(bodyStart, bodyStart + body.length)));
1096
- const validBindList = bodyTokens.every((token, index) => index % 2 === 0
1097
- ? token.kind === 'bind'
1098
- : token.kind === 'punctuation' && token.text === ',');
1099
- const params = bodyTokens
1100
- .filter((_, index) => index % 2 === 0)
1101
- .map((token) => token.text.slice(1));
1102
- if (validBindList &&
1103
- params.length > 0 &&
1104
- params.every((name) => required.has(name))) {
1105
- for (const name of params)
1106
- found.push({ name, operator: 'in' });
1107
- }
1108
- }
897
+ const node = scopeNode(refScope(ref), ref.alias, scope.column);
898
+ const found = proofs.binds
899
+ .filter((proof) => nodeOf(proof) === node &&
900
+ proof.params.every((name) => required.has(name)))
901
+ .flatMap((proof) => proof.params.map((name) => ({ name, operator: proof.operator })));
1109
902
  const params = [...new Set(found.map((item) => item.name))];
1110
903
  if (params.length > 0) {
1111
904
  const evidence = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/typegen",
3
- "version": "0.58.0",
3
+ "version": "0.60.0",
4
4
  "description": "Syncular schema-to-TypeScript type generator and the `syncular` CLI",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -56,9 +56,9 @@
56
56
  "!dist/**/*.test.d.ts"
57
57
  ],
58
58
  "dependencies": {
59
- "@syncular/core": "0.58.0"
59
+ "@syncular/core": "0.60.0"
60
60
  },
61
61
  "devDependencies": {
62
- "@syncular/server": "0.58.0"
62
+ "@syncular/server": "0.60.0"
63
63
  }
64
64
  }
package/src/cli.ts CHANGED
@@ -17,6 +17,7 @@
17
17
  */
18
18
  import { existsSync, readFileSync, watch, writeFileSync } from 'node:fs';
19
19
  import { resolve } from 'node:path';
20
+ import { TypegenError } from './errors';
20
21
  import { formatSyql } from './fmt';
21
22
  import {
22
23
  baselineMigrationHistory,
@@ -60,7 +61,7 @@ fmt options:
60
61
  migrations options:
61
62
  baseline create the first immutable history lock; refuses overwrite
62
63
  check validate the committed history without generating outputs
63
- upgrade-lock explicitly compact a validated format-1 lock to format 2
64
+ upgrade-lock convert a validated format-1 or format-2 lock to format 3
64
65
  --manifest-dir <dir> directory holding syncular.json (default: .)
65
66
 
66
67
  init options:
@@ -207,7 +208,12 @@ function parseManifestDir(argv: readonly string[]): string {
207
208
 
208
209
  /** Add a docs pointer to the fail-loud missing-input errors. */
209
210
  function friendlyGenerateError(error: unknown): string {
210
- const message = error instanceof Error ? error.message : String(error);
211
+ const message =
212
+ error instanceof TypegenError && error.code !== undefined
213
+ ? `${error.message}\n${error.code} ${JSON.stringify(error.details ?? {})}`
214
+ : error instanceof Error
215
+ ? error.message
216
+ : String(error);
211
217
  if (
212
218
  message.includes('manifest not found') ||
213
219
  message.includes('migrations directory does not exist') ||
@@ -370,7 +376,7 @@ function runMigrations(argv: readonly string[]): void {
370
376
  fail(friendlyGenerateError(error));
371
377
  }
372
378
  writeFileSync(output.path, output.content, 'utf8');
373
- console.log(`upgraded ${output.path} to compact format 2`);
379
+ console.log(`upgraded ${output.path} to format 3`);
374
380
  console.log('review and commit the migration-lock format upgrade');
375
381
  return;
376
382
  }