@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/src/naming.ts CHANGED
@@ -18,6 +18,7 @@
18
18
  * keyword, or a leading underscore on the Dart target (library-private).
19
19
  */
20
20
  import { TypegenError } from './errors';
21
+ import type { IrTable } from './ir';
21
22
 
22
23
  /** Manifest `"naming"`: camelCase emission (default) or SQL-truth names. */
23
24
  export type NamingMode = 'camel' | 'preserve';
@@ -44,6 +45,36 @@ export function snakeToCamel(name: string): string {
44
45
  return lead + first + rest.join('') + trail;
45
46
  }
46
47
 
48
+ /** Generated type name of a table or subscription: `_`/`-` separated parts,
49
+ * each with its first character upper-cased (`project_tasks` →
50
+ * `ProjectTasks`). */
51
+ export function pascalCase(name: string): string {
52
+ return name
53
+ .split(/[_-]+/)
54
+ .filter((part) => part.length > 0)
55
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
56
+ .join('');
57
+ }
58
+
59
+ /** `value` as a string literal of a schema emitter's language. Swift and
60
+ * Kotlin delimit with `"`, TS and Dart with `'`; Kotlin and Dart also escape
61
+ * the `$` that would start an interpolation. */
62
+ export function stringLiteral(
63
+ value: string,
64
+ target: Exclude<NamingTarget, 'rust'>,
65
+ ): string {
66
+ const delimiter = target === 'swift' || target === 'kotlin' ? '"' : "'";
67
+ const interpolates = target === 'kotlin' || target === 'dart';
68
+ let body = '';
69
+ for (const char of value) {
70
+ body +=
71
+ char === '\\' || char === delimiter || (interpolates && char === '$')
72
+ ? `\\${char}`
73
+ : char;
74
+ }
75
+ return `${delimiter}${body}${delimiter}`;
76
+ }
77
+
47
78
  /** The emitter targets whose keyword sets we police. `ts` is included for
48
79
  * completeness (its emitters can quote any property, so its list is the
49
80
  * small set that breaks generated FUNCTION/const identifiers). */
@@ -419,3 +450,126 @@ export function buildNamingMap(
419
450
  langName: bySqlName.get(sqlName) as string,
420
451
  }));
421
452
  }
453
+
454
+ /**
455
+ * The schema identifier rules for every configured target in both naming
456
+ * modes. Table names, and column names within a table, stay distinct under
457
+ * SQLite's case-insensitive comparison; generated type names
458
+ * (`pascalCase(table)`) stay distinct; each target's emitted column names
459
+ * stay distinct and clear of its reserved words. Swift, Kotlin, and Dart
460
+ * always emit camelCase fields; TS and Rust follow the naming mode.
461
+ */
462
+ export function validateSchemaIdentifiers(
463
+ tables: readonly IrTable[],
464
+ mode: NamingMode,
465
+ targets: readonly NamingTarget[],
466
+ context: string,
467
+ ): void {
468
+ const distinct = (
469
+ names: readonly string[],
470
+ key: (name: string) => string,
471
+ code: string,
472
+ message: string,
473
+ details: Readonly<Record<string, unknown>> = {},
474
+ ): void => {
475
+ const seen = new Map<string, string>();
476
+ for (const name of names) {
477
+ const other = seen.get(key(name));
478
+ if (other !== undefined) {
479
+ throw new TypegenError(context, message, code, {
480
+ ...details,
481
+ names: [other, name],
482
+ });
483
+ }
484
+ seen.set(key(name), name);
485
+ }
486
+ };
487
+ const caseless = (name: string): string => name.toLowerCase();
488
+ const tableNames = tables.map((table) => table.name);
489
+ distinct(
490
+ tableNames,
491
+ caseless,
492
+ 'typegen.duplicate_identifier',
493
+ 'two table names differ only in letter case; SQLite compares identifiers case-insensitively',
494
+ );
495
+ distinct(
496
+ tableNames,
497
+ pascalCase,
498
+ 'typegen.identifier_collision',
499
+ 'two tables map to one generated type name',
500
+ );
501
+ for (const table of tables) {
502
+ const columnNames = table.columns.map((column) => column.name);
503
+ distinct(
504
+ columnNames,
505
+ caseless,
506
+ 'typegen.duplicate_identifier',
507
+ 'two column names differ only in letter case; SQLite compares identifiers case-insensitively',
508
+ { table: table.name },
509
+ );
510
+ for (const target of targets) {
511
+ const emit = (name: string): string => {
512
+ const langName =
513
+ mode === 'camel' || (target !== 'ts' && target !== 'rust')
514
+ ? snakeToCamel(name)
515
+ : name;
516
+ return target === 'rust' ? rustSnakeCase(langName) : langName;
517
+ };
518
+ distinct(
519
+ columnNames,
520
+ emit,
521
+ 'typegen.identifier_collision',
522
+ 'two columns map to one generated field name',
523
+ { target, table: table.name },
524
+ );
525
+ for (const column of columnNames) {
526
+ const emitted = emit(column);
527
+ if (
528
+ TARGET_KEYWORDS[target].has(emitted) ||
529
+ (target === 'dart' && emitted.startsWith('_'))
530
+ ) {
531
+ throw new TypegenError(
532
+ context,
533
+ 'a column maps to a generated field name that is a reserved word or library-private on an emitter target; rename the column',
534
+ 'typegen.reserved_identifier',
535
+ { target, table: table.name, column, emitted },
536
+ );
537
+ }
538
+ }
539
+ }
540
+ }
541
+ }
542
+
543
+ /**
544
+ * Refuse a query name, result column, or query input that some configured
545
+ * target cannot emit: TS uses the name as is, Rust its snake_case form, and
546
+ * Swift, Kotlin, and Dart its camelCase form. Each must be an identifier
547
+ * clear of the target's reserved words; Dart also refuses a leading `_`.
548
+ */
549
+ export function validateQueryIdentifier(
550
+ name: string,
551
+ targets: readonly NamingTarget[],
552
+ context: string,
553
+ scope: string,
554
+ ): void {
555
+ for (const target of targets) {
556
+ const emitted =
557
+ target === 'rust'
558
+ ? rustSnakeCase(name)
559
+ : target === 'ts'
560
+ ? name
561
+ : snakeToCamel(name);
562
+ if (
563
+ !/^[A-Za-z_][A-Za-z0-9_]*$/.test(emitted) ||
564
+ TARGET_KEYWORDS[target].has(emitted) ||
565
+ (target === 'dart' && emitted.startsWith('_'))
566
+ ) {
567
+ throw new TypegenError(
568
+ context,
569
+ 'a query name, result column, or query input is not a valid identifier on an emitter target; rename it or alias it in SQL (AS)',
570
+ 'typegen.invalid_query_identifier',
571
+ { target, scope, name, emitted },
572
+ );
573
+ }
574
+ }
575
+ }