@syncular/typegen 0.5.1 → 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 (56) hide show
  1. package/README.md +79 -32
  2. package/dist/cli.js +40 -17
  3. package/dist/emit-queries-dart.js +168 -55
  4. package/dist/emit-queries-kotlin.js +167 -55
  5. package/dist/emit-queries-swift.js +183 -57
  6. package/dist/emit-queries.js +286 -123
  7. package/dist/fmt.d.ts +2 -2
  8. package/dist/fmt.js +348 -316
  9. package/dist/generate.d.ts +1 -1
  10. package/dist/generate.js +28 -9
  11. package/dist/index.d.ts +3 -1
  12. package/dist/index.js +3 -1
  13. package/dist/lsp.d.ts +1 -3
  14. package/dist/lsp.js +349 -187
  15. package/dist/manifest.d.ts +1 -3
  16. package/dist/manifest.js +1 -1
  17. package/dist/query-ir.js +53 -42
  18. package/dist/query.d.ts +110 -63
  19. package/dist/query.js +89 -11
  20. package/dist/syql-ast.d.ts +12 -13
  21. package/dist/syql-ast.js +26 -20
  22. package/dist/syql-lexer.d.ts +2 -0
  23. package/dist/syql-lexer.js +9 -2
  24. package/dist/syql-lowering.d.ts +22 -0
  25. package/dist/syql-lowering.js +434 -0
  26. package/dist/syql-parser.d.ts +19 -18
  27. package/dist/syql-parser.js +341 -93
  28. package/dist/syql-semantics.d.ts +73 -0
  29. package/dist/syql-semantics.js +607 -0
  30. package/dist/syql-template-parser.d.ts +5 -20
  31. package/dist/syql-template-parser.js +116 -109
  32. package/dist/syql-validator.d.ts +35 -0
  33. package/dist/syql-validator.js +993 -0
  34. package/package.json +3 -3
  35. package/src/cli.ts +49 -21
  36. package/src/emit-queries-dart.ts +195 -69
  37. package/src/emit-queries-kotlin.ts +197 -69
  38. package/src/emit-queries-swift.ts +224 -68
  39. package/src/emit-queries.ts +410 -165
  40. package/src/fmt.ts +405 -303
  41. package/src/generate.ts +43 -9
  42. package/src/index.ts +3 -1
  43. package/src/lsp.ts +414 -216
  44. package/src/manifest.ts +2 -4
  45. package/src/query-ir.ts +52 -42
  46. package/src/query.ts +229 -79
  47. package/src/syql-ast.ts +38 -34
  48. package/src/syql-lexer.ts +12 -1
  49. package/src/syql-lowering.ts +664 -0
  50. package/src/syql-parser.ts +472 -134
  51. package/src/syql-semantics.ts +930 -0
  52. package/src/syql-template-parser.ts +119 -163
  53. package/src/syql-validator.ts +1486 -0
  54. package/dist/syql.d.ts +0 -102
  55. package/dist/syql.js +0 -1141
  56. package/src/syql.ts +0 -1470
package/src/generate.ts CHANGED
@@ -50,7 +50,10 @@ import {
50
50
  } from './query';
51
51
  import { serializeQueryIr } from './query-ir';
52
52
  import { applyMigrationSql, type ParsedTable } from './sql';
53
- import { analyzeSyqlFile } from './syql';
53
+ import { lowerSyqlQuery } from './syql-lowering';
54
+ import { buildSyqlModuleGraph } from './syql-modules';
55
+ import { analyzeSyqlSemantics } from './syql-semantics';
56
+ import { validateSyqlProgram } from './syql-validator';
54
57
 
55
58
  export interface MigrationInput {
56
59
  /** Directory name, e.g. `0001_initial`. */
@@ -421,18 +424,48 @@ export function analyzeQueries(
421
424
  ir: IrDocument,
422
425
  queries: readonly QueryInput[],
423
426
  naming?: QueryNamingOptions,
427
+ queriesRoot = resolve('/__syncular_queries__'),
424
428
  ): AnalyzedQuery[] {
425
429
  if (queries.length === 0) return [];
426
430
  const { db, close } = makeQueryDb(ir);
427
431
  try {
428
- // Each file may hold multiple statements; flatten in path order (stable).
429
- // Frontends dispatch on extension; both produce the same AnalyzedQuery
430
- // shape (§1 — nothing downstream knows the frontend).
431
- const analyzed = queries.flatMap((q) =>
432
- q.file.endsWith('.syql')
433
- ? analyzeSyqlFile(q.file, q.sql, ir, db, naming)
434
- : analyzeQueryFile(q.file, q.sql, ir, db, naming),
435
- );
432
+ const plain = queries
433
+ .filter((query) => query.file.endsWith('.sql'))
434
+ .flatMap((query) =>
435
+ analyzeQueryFile(query.file, query.sql, ir, db, naming),
436
+ );
437
+ const syqlInputs = queries.filter((query) => query.file.endsWith('.syql'));
438
+ let syql: AnalyzedQuery[] = [];
439
+ if (syqlInputs.length > 0) {
440
+ const root = resolve(queriesRoot);
441
+ const sourceByFile = new Map(
442
+ syqlInputs.map((query) => [resolve(root, query.file), query.sql]),
443
+ );
444
+ const displayByFile = new Map(
445
+ syqlInputs.map((query) => [resolve(root, query.file), query.file]),
446
+ );
447
+ const graph = buildSyqlModuleGraph(
448
+ root,
449
+ syqlInputs.map((query) => query.file),
450
+ (file) => sourceByFile.get(file),
451
+ );
452
+ const validated = validateSyqlProgram(
453
+ analyzeSyqlSemantics(graph),
454
+ ir,
455
+ db,
456
+ naming,
457
+ );
458
+ syql = validated.queries
459
+ .map((query) => {
460
+ const lowered = lowerSyqlQuery(query, ir, db, naming).analysis;
461
+ return {
462
+ ...lowered,
463
+ file: displayByFile.get(query.logical.module.file) ?? lowered.file,
464
+ };
465
+ })
466
+ .sort((left, right) => left.file.localeCompare(right.file));
467
+ }
468
+ const analyzed = [...plain, ...syql];
436
469
  // Names must be unique across the whole manifest — the filesystem no longer
437
470
  // guarantees uniqueness once `-- name:` overrides exist. Report BOTH source
438
471
  // locations (file + statement position) on a collision.
@@ -552,6 +585,7 @@ export function generate(manifestDir: string): GenerateResult {
552
585
  ir,
553
586
  loadQueries(resolve(manifestDir, manifest.queries)),
554
587
  naming,
588
+ resolve(manifestDir, manifest.queries),
555
589
  )
556
590
  : [];
557
591
  if (wantsQueries && analyzedQueries.length === 0) {
package/src/index.ts CHANGED
@@ -22,9 +22,11 @@ export * from './naming';
22
22
  export * from './query';
23
23
  export * from './query-ir';
24
24
  export * from './sql';
25
- export * from './syql';
26
25
  export * from './syql-ast';
27
26
  export * from './syql-lexer';
27
+ export * from './syql-lowering';
28
28
  export * from './syql-modules';
29
29
  export * from './syql-parser';
30
+ export * from './syql-semantics';
30
31
  export * from './syql-template-parser';
32
+ export * from './syql-validator';