@contractkit/plugin-typescript 0.17.2 → 0.17.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contractkit/plugin-typescript",
3
- "version": "0.17.2",
3
+ "version": "0.17.4",
4
4
  "description": "ContractKit built-in plugin: TypeScript codegen (SDK clients, Koa routers, Zod schemas, plain types)",
5
5
  "author": {
6
6
  "name": "Marooned Software",
@@ -29,8 +29,8 @@
29
29
  "@contractkit/core": "0.13.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@repo/config-eslint": "0.3.1",
33
- "@repo/config-typescript": "0.1.0"
32
+ "@repo/config-typescript": "0.1.0",
33
+ "@repo/config-eslint": "0.3.1"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsup src/index.ts --format esm --sourcemap --dts && tsc --emitDeclarationOnly --declaration",
@@ -40,6 +40,7 @@ export function modeToWrapper(mode: ObjectMode): string {
40
40
 
41
41
  // ─── Cross-file import resolution ─────────────────────────────────────────
42
42
 
43
+ /** Cross-file context passed to `generateContract` to wire up imports and Input/Output variant tracking. */
43
44
  export interface ContractCodegenContext {
44
45
  /** Map from model name → absolute output file path */
45
46
  modelOutPaths: Map<string, string>;
@@ -250,7 +251,7 @@ function generateModel(
250
251
  const needsInputSplit = effective.fields.some(f => f.visibility !== 'normal') || (modelsWithInput?.has(effective.name) ?? false);
251
252
 
252
253
  const lines = needsInputSplit
253
- ? generateThreeSchemaModel(effective, outPath, modelsWithInput, modelsWithWriteonly)
254
+ ? generateThreeSchemaModel(effective, outPath, modelsWithInput, modelsWithWriteonly, modelMap)
254
255
  : generateSimpleModel(effective, outPath);
255
256
 
256
257
  // Emit Output type alias when this model (transitively) has format(output=...)
@@ -342,7 +343,27 @@ function buildExtendChain(bases: string[], resolveName: (b: string) => string):
342
343
  return { head, tail };
343
344
  }
344
345
 
345
- function generateThreeSchemaModel(model: ModelNode, outPath?: string, modelsWithInput?: Set<string>, modelsWithWriteonly?: Set<string>): string[] {
346
+ function collectEffectiveWritableFieldNames(modelName: string, modelMap: Map<string, ModelNode>): Set<string> {
347
+ const model = modelMap.get(modelName);
348
+ if (!model || model.type) return new Set();
349
+ const result = new Set<string>();
350
+ for (const base of model.bases ?? []) {
351
+ for (const f of collectEffectiveWritableFieldNames(base, modelMap)) result.add(f);
352
+ }
353
+ for (const field of model.fields) {
354
+ if (field.visibility === 'readonly') result.delete(field.name);
355
+ else result.add(field.name);
356
+ }
357
+ return result;
358
+ }
359
+
360
+ function generateThreeSchemaModel(
361
+ model: ModelNode,
362
+ outPath?: string,
363
+ modelsWithInput?: Set<string>,
364
+ modelsWithWriteonly?: Set<string>,
365
+ modelMap?: Map<string, ModelNode>,
366
+ ): string[] {
346
367
  const lines: string[] = [];
347
368
  const name = model.name;
348
369
 
@@ -388,9 +409,28 @@ function generateThreeSchemaModel(model: ModelNode, outPath?: string, modelsWith
388
409
  // extends ParentInput if parent has an Input variant, else extends parent read schema
389
410
  const writeFields = allFields.filter(f => f.visibility !== 'readonly');
390
411
  const writeBody = modelsWithInput ? renderInputFields(writeFields, modelsWithInput, model.mode) : renderFields(writeFields, model.mode);
412
+ // Fields that become readonly in this model but were writable in a base must be omitted from
413
+ // the base Input schema — Zod's .extend() cannot remove inherited fields.
414
+ const fieldsToOmit = new Set<string>();
415
+ if (bases.length > 0 && modelMap) {
416
+ for (const field of allFields) {
417
+ if (field.visibility === 'readonly') {
418
+ for (const base of bases) {
419
+ if (collectEffectiveWritableFieldNames(base, modelMap).has(field.name)) {
420
+ fieldsToOmit.add(field.name);
421
+ break;
422
+ }
423
+ }
424
+ }
425
+ }
426
+ }
427
+ const omitClause =
428
+ fieldsToOmit.size > 0
429
+ ? `.omit({ ${[...fieldsToOmit].map(f => `${quoteKey(f)}: true`).join(', ')} })`
430
+ : '';
391
431
  if (bases.length > 0) {
392
432
  const { head, tail } = buildExtendChain(bases, b => (modelsWithInput?.has(b) ? `${b}Input` : b));
393
- lines.push(`export const ${name}Input = ${head}${tail}.extend({`);
433
+ lines.push(`export const ${name}Input = ${head}${tail}${omitClause}.extend({`);
394
434
  } else {
395
435
  lines.push(`export const ${name}Input = ${wrapper}({`);
396
436
  }
@@ -907,6 +947,7 @@ function rootNeedsDateTime(root: ContractRootNode): boolean {
907
947
  return root.models.some(m => (m.type && typeNeedsDateTime(m.type)) || m.fields.some(f => typeNeedsDateTime(f.type)));
908
948
  }
909
949
 
950
+ /** Returns true if `type` (recursively) contains a scalar with the given `name`. */
910
951
  export function typeNeedsScalar(type: ContractTypeNode, name: string): boolean {
911
952
  switch (type.kind) {
912
953
  case 'scalar':
@@ -932,10 +973,12 @@ export function typeNeedsScalar(type: ContractTypeNode, name: string): boolean {
932
973
  }
933
974
  }
934
975
 
976
+ /** Returns true if any model in `root` uses a scalar with the given `name`. */
935
977
  export function rootNeedsScalar(root: ContractRootNode, name: string): boolean {
936
978
  return root.models.some(m => (m.type && typeNeedsScalar(m.type, name)) || m.fields.some(f => typeNeedsScalar(f.type, name)));
937
979
  }
938
980
 
981
+ /** Returns true if `type` (recursively) contains a `date`, `time`, or `datetime` scalar. */
939
982
  export function typeNeedsDateTime(type: ContractTypeNode): boolean {
940
983
  switch (type.kind) {
941
984
  case 'scalar':
@@ -955,6 +998,7 @@ export function typeNeedsDateTime(type: ContractTypeNode): boolean {
955
998
  }
956
999
  }
957
1000
 
1001
+ /** Collect model names referenced in `root` that are not defined locally (need to be imported). */
958
1002
  export function collectExternalRefs(root: ContractRootNode): string[] {
959
1003
  const localNames = new Set(root.models.map(m => m.name));
960
1004
  const refs = new Set<string>();
@@ -55,8 +55,10 @@ export function generatePlainTypes(root: ContractRootNode, context?: ContractCod
55
55
  lines.push('');
56
56
  }
57
57
 
58
+ const modelMap = new Map(root.models.map(m => [m.name, m]));
59
+
58
60
  for (const model of topoSortModels(root.models)) {
59
- lines.push(...generateModel(model, context?.currentOutPath, allModelsWithInput, allModelsWithOutput));
61
+ lines.push(...generateModel(model, context?.currentOutPath, allModelsWithInput, allModelsWithOutput, modelMap));
60
62
  lines.push('');
61
63
  }
62
64
 
@@ -65,7 +67,13 @@ export function generatePlainTypes(root: ContractRootNode, context?: ContractCod
65
67
 
66
68
  // ─── Model ─────────────────────────────────────────────────────────────────
67
69
 
68
- function generateModel(model: ModelNode, outPath?: string, modelsWithInput?: Set<string>, modelsWithOutput?: Set<string>): string[] {
70
+ function generateModel(
71
+ model: ModelNode,
72
+ outPath?: string,
73
+ modelsWithInput?: Set<string>,
74
+ modelsWithOutput?: Set<string>,
75
+ modelMap?: Map<string, ModelNode>,
76
+ ): string[] {
69
77
  // Type alias: Name : typeExpression
70
78
  if (model.type) {
71
79
  return generateTypeAlias(model, outPath, modelsWithInput, modelsWithOutput);
@@ -75,7 +83,7 @@ function generateModel(model: ModelNode, outPath?: string, modelsWithInput?: Set
75
83
  // transitively references models that have Input variants (captured in modelsWithInput).
76
84
  const needsInputSplit = model.fields.some(f => f.visibility !== 'normal') || (modelsWithInput?.has(model.name) ?? false);
77
85
 
78
- const lines = needsInputSplit ? generateVisibilityModel(model, outPath, modelsWithInput) : generateSimpleModel(model, outPath);
86
+ const lines = needsInputSplit ? generateVisibilityModel(model, outPath, modelsWithInput, modelMap) : generateSimpleModel(model, outPath, modelMap);
79
87
 
80
88
  if (modelsWithOutput?.has(model.name)) {
81
89
  lines.push('');
@@ -84,6 +92,30 @@ function generateModel(model: ModelNode, outPath?: string, modelsWithInput?: Set
84
92
  return lines;
85
93
  }
86
94
 
95
+ /** Recursively collect every field name defined on `bases` and their ancestors. Used to detect
96
+ * fields that the child re-declares without an explicit `override` keyword — those still need an
97
+ * `Omit<Base, …>` wrap, otherwise the child's narrower/incompatible declaration collides with the
98
+ * inherited one. */
99
+ function collectInheritedFieldNames(bases: string[], modelMap: Map<string, ModelNode>): Set<string> {
100
+ const result = new Set<string>();
101
+ const visit = (name: string): void => {
102
+ const m = modelMap.get(name);
103
+ if (!m || m.type) return;
104
+ for (const f of m.fields) result.add(f.name);
105
+ for (const b of m.bases ?? []) visit(b);
106
+ };
107
+ for (const b of bases) visit(b);
108
+ return result;
109
+ }
110
+
111
+ /** Names of fields the child declaration overrides — explicit `override` plus any field whose
112
+ * name shadows an inherited one. The latter catches single-base redeclarations that omit the
113
+ * `override` keyword (e.g. narrowing `kind: BusinessRoleKind` → `kind: 'employee'`). */
114
+ function computeOverrideNames(model: ModelNode, modelMap?: Map<string, ModelNode>): string[] {
115
+ const inherited = modelMap ? collectInheritedFieldNames(model.bases ?? [], modelMap) : new Set<string>();
116
+ return model.fields.filter(f => f.override || inherited.has(f.name)).map(f => f.name);
117
+ }
118
+
87
119
  function generateComments(model: ModelNode, outPath?: string): string[] {
88
120
  const lines: string[] = [];
89
121
  lines.push('/**');
@@ -113,11 +145,11 @@ function generateTypeAlias(model: ModelNode, outPath?: string, modelsWithInput?:
113
145
  return lines;
114
146
  }
115
147
 
116
- /** Build the `extends` clause for a multi-base model.
117
- * Override-marked field names are wrapped in `Omit<Base, 'name1' | 'name2'>` per base so the
118
- * subclass can legally redeclare them with new types. TypeScript's `Omit<T, K extends keyof any>`
119
- * tolerates omit keys that don't appear on the base, so we omit unconditionally — no need to know
120
- * each base's actual field set. */
148
+ /** Build the `extends` clause for a model.
149
+ * Each entry in `overrideNames` is wrapped in `Omit<Base, 'name1' | 'name2'>` per base so the
150
+ * subclass can legally redeclare those fields with new (possibly incompatible) types.
151
+ * TypeScript's `Omit<T, K extends keyof any>` tolerates omit keys that don't appear on the base,
152
+ * so we apply the same omit list to every base without per-base field-set lookup. */
121
153
  function buildExtendsClause(bases: string[], overrideNames: string[], baseNameResolver: (b: string) => string): string {
122
154
  if (bases.length === 0) return '';
123
155
  if (overrideNames.length === 0) return ` extends ${bases.map(baseNameResolver).join(', ')}`;
@@ -126,12 +158,12 @@ function buildExtendsClause(bases: string[], overrideNames: string[], baseNameRe
126
158
  return ` extends ${wrapped.join(', ')}`;
127
159
  }
128
160
 
129
- function generateSimpleModel(model: ModelNode, outPath?: string): string[] {
161
+ function generateSimpleModel(model: ModelNode, outPath?: string, modelMap?: Map<string, ModelNode>): string[] {
130
162
  const lines: string[] = [];
131
163
  lines.push(...generateComments(model, outPath));
132
164
 
133
165
  const bases = model.bases ?? [];
134
- const overrideNames = model.fields.filter(f => f.override).map(f => f.name);
166
+ const overrideNames = computeOverrideNames(model, modelMap);
135
167
  lines.push(`export interface ${model.name}${buildExtendsClause(bases, overrideNames, b => b)} {`);
136
168
 
137
169
  for (const field of model.fields) {
@@ -142,12 +174,12 @@ function generateSimpleModel(model: ModelNode, outPath?: string): string[] {
142
174
  return lines;
143
175
  }
144
176
 
145
- function generateVisibilityModel(model: ModelNode, outPath?: string, modelsWithInput?: Set<string>): string[] {
177
+ function generateVisibilityModel(model: ModelNode, outPath?: string, modelsWithInput?: Set<string>, modelMap?: Map<string, ModelNode>): string[] {
146
178
  const lines: string[] = [];
147
179
  lines.push(...generateComments(model, outPath));
148
180
 
149
181
  const bases = model.bases ?? [];
150
- const overrideNames = model.fields.filter(f => f.override).map(f => f.name);
182
+ const overrideNames = computeOverrideNames(model, modelMap);
151
183
 
152
184
  // Read type — omit writeonly fields
153
185
  const readFields = model.fields.filter(f => f.visibility !== 'writeonly');
@@ -657,6 +657,32 @@ describe('generateContract', () => {
657
657
  // A has Input variant (because of readonly), B doesn't.
658
658
  expect(output).toContain('export const TestInput = AInput.extend(B.shape).extend({');
659
659
  });
660
+
661
+ it('omits override-readonly field from child Input schema', () => {
662
+ const root = contractRoot([
663
+ model('Base', [field('id', scalarType('uuid')), field('name', scalarType('string'))]),
664
+ model('Child', [field('name', scalarType('string'), { visibility: 'readonly', override: true })], {
665
+ bases: ['Base'],
666
+ }),
667
+ ]);
668
+ const output = generateContract(root);
669
+ // Base has no visibility — no BaseInput
670
+ expect(output).not.toContain('BaseInput');
671
+ // Child re-declares 'name' as readonly, so 'name' must be omitted from Base before extending
672
+ expect(output).toContain('export const ChildInput = Base.omit({ name: true }).extend({');
673
+ });
674
+
675
+ it('omits override-readonly field inherited transitively through multiple bases', () => {
676
+ const root = contractRoot([
677
+ model('Root', [field('legalId', scalarType('uuid'))]),
678
+ model('Mid', [], { bases: ['Root'] }),
679
+ model('Child', [field('legalId', scalarType('uuid'), { visibility: 'readonly', override: true })], {
680
+ bases: ['Mid'],
681
+ }),
682
+ ]);
683
+ const output = generateContract(root);
684
+ expect(output).toContain('export const ChildInput = Mid.omit({ legalId: true }).extend({');
685
+ });
660
686
  });
661
687
 
662
688
  // ─── Type alias Input variants ────────────────────────────────
@@ -419,6 +419,44 @@ describe('generatePlainTypes', () => {
419
419
  const output = generatePlainTypes(root);
420
420
  expect(output).toContain("export interface Test5 extends Omit<A, 'a' | 'b'> {");
421
421
  });
422
+
423
+ it('omits implicit single-base redeclarations even without explicit override', () => {
424
+ // BusinessRoleOfficer-style: child redeclares `kind` without `override` keyword,
425
+ // narrowing to an incompatible literal — must wrap base in Omit to compile.
426
+ const root = contractRoot([
427
+ model('Employee', [field('kind', literalType('employee')), field('title', scalarType('string'))]),
428
+ model('Officer', [field('kind', literalType('officer')), field('isControlPerson', scalarType('boolean'))], {
429
+ bases: ['Employee'],
430
+ }),
431
+ ]);
432
+ const output = generatePlainTypes(root);
433
+ expect(output).toContain("export interface Officer extends Omit<Employee, 'kind'> {");
434
+ });
435
+
436
+ it('applies implicit-override Omit to both Read and Input interfaces', () => {
437
+ const root = contractRoot([
438
+ model('Employee', [
439
+ field('id', scalarType('uuid'), { visibility: 'readonly' }),
440
+ field('kind', literalType('employee')),
441
+ ]),
442
+ model('Officer', [field('kind', literalType('officer')), field('isControlPerson', scalarType('boolean'))], {
443
+ bases: ['Employee'],
444
+ }),
445
+ ]);
446
+ const output = generatePlainTypes(root);
447
+ expect(output).toContain("export interface Officer extends Omit<Employee, 'kind'> {");
448
+ expect(output).toContain("export interface OfficerInput extends Omit<EmployeeInput, 'kind'> {");
449
+ });
450
+
451
+ it('detects redeclaration through transitive base chain', () => {
452
+ const root = contractRoot([
453
+ model('Root', [field('kind', scalarType('string'))]),
454
+ model('Mid', [], { bases: ['Root'] }),
455
+ model('Leaf', [field('kind', literalType('leaf'))], { bases: ['Mid'] }),
456
+ ]);
457
+ const output = generatePlainTypes(root);
458
+ expect(output).toContain("export interface Leaf extends Omit<Mid, 'kind'> {");
459
+ });
422
460
  });
423
461
 
424
462
  // ─── Type alias Input variants ────────────────────────────────