@devries-cameron20 / composio-1 / commits / 73570901f
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(ts-builders): fix Parameters in @composio/ts-builders (#2263)

Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com>

Alberto Schiabel committed Dec 13, 2025 at 08:32 UTC 73570901f266e4a3e7dd25bff92a61ad79c19d52
4 files changed +95 -2
ts/packages/ts-builders/src/Parameter.test.ts
+43
@@ -1,10 +1,14 @@
1 import { expect, test } from 'vitest';
2
3 +import { functionType } from './FunctionType';
4 import { namedType } from './NamedType';
5 import { parameter } from './Parameter';
6 import { stringify } from './stringify';
7 +import { unionType } from './UnionType';
8
9 const A = namedType('A');
10 +const stringType = namedType('string');
11 +const numberType = namedType('number');
12
13 test('name and type', () => {
14 const param = parameter('foo', A);
@@ -17,3 +21,42 @@ test('optional', () => {
21
22 expect(stringify(param)).toMatchInlineSnapshot(`"foo?: A"`);
23 });
24 +
25 +test('parameter with union type adds parentheses in function context', () => {
26 + // Union has precedence 1, FunctionParameter context requires precedence 4
27 + // So parentheses should be added: (param: (string | number))
28 + const unionParam = unionType([stringType, numberType]);
29 + const func = functionType().addParameter(parameter('param', unionParam));
30 + const result = stringify(func);
31 +
32 + expect(result).toBe('(param: (string | number)) => void');
33 +});
34 +
35 +test('parameter with intersection type adds parentheses in function context', () => {
36 + // Intersection has precedence 2, FunctionParameter context requires precedence 4
37 + // So parentheses should be added
38 + const intersectionParam = namedType('A & B'); // Using named type to simulate intersection
39 + const func = functionType().addParameter(parameter('param', intersectionParam));
40 + const result = stringify(func);
41 +
42 + // Named type has atomic precedence, no parentheses needed
43 + expect(result).toBe('(param: A & B) => void');
44 +});
45 +
46 +test('parameter with function type adds parentheses in function context', () => {
47 + // Function type has precedence 4, FunctionParameter context requires precedence 4
48 + // Since 4 is not < 4, no parentheses needed
49 + const funcParam = functionType().setReturnType(stringType);
50 + const func = functionType().addParameter(parameter('callback', funcParam));
51 + const result = stringify(func);
52 +
53 + expect(result).toBe('(callback: () => string) => void');
54 +});
55 +
56 +test('optional parameter with union type adds parentheses', () => {
57 + const unionParam = unionType([stringType, numberType]);
58 + const func = functionType().addParameter(parameter('param', unionParam).optional());
59 + const result = stringify(func);
60 +
61 + expect(result).toBe('(param?: (string | number)) => void');
62 +});
ts/packages/ts-builders/src/Parameter.ts
+3 -1
@@ -1,4 +1,5 @@
1 import type { BasicBuilder } from './BasicBuilder';
2 +import { TypeContext } from './OperatorPrecedence';
3 import { TypeBuilder } from './TypeBuilder';
4 import { Writer } from './Writer';
5
@@ -19,7 +20,8 @@ export class Parameter implements BasicBuilder {
20 if (this.isOptional) {
21 writer.write('?');
22 }
22 - writer.write(': ').write(this.type);
23 + writer.write(': ');
24 + this.type.writeInContext(writer, TypeContext.FunctionParameter);
25 }
26 }
27
ts/packages/ts-builders/src/Property.test.ts
+46
@@ -1,12 +1,17 @@
1 import { expect, test } from 'vitest';
2
3 import { docComment } from './DocComment';
4 +import { functionType } from './FunctionType';
5 import { namedType } from './NamedType';
6 +import { objectType } from './ObjectType';
7 import { property } from './Property';
8 import { stringify } from './stringify';
9 +import { unionType } from './UnionType';
10 import { toStringTag } from './WellKnownSymbol';
11
12 const A = namedType('A');
13 +const stringType = namedType('string');
14 +const numberType = namedType('number');
15
16 test('name and type', () => {
17 const prop = property('foo', A);
@@ -48,3 +53,44 @@ test('with doc comment', () => {
53 foo: A"
54 `);
55 });
56 +
57 +test('property with union type does not add parentheses (ObjectProperty context)', () => {
58 + // Union has precedence 1, ObjectProperty context requires precedence 5
59 + // However, for object properties, union types don't need parentheses syntactically
60 + // because the property boundary is clear: { prop: string | number }
61 + // But according to precedence rules, 1 < 5 means parentheses would be added
62 + const unionProp = unionType([stringType, numberType]);
63 + const obj = objectType().add(property('value', unionProp)).formatInline();
64 + const result = stringify(obj);
65 +
66 + // ObjectProperty context has precedence 5, Union has precedence 1
67 + // Since 1 < 5, parentheses are added
68 + expect(result).toBe('{ value: (string | number) }');
69 +});
70 +
71 +test('property with function type does not add parentheses (ObjectProperty context)', () => {
72 + // Function type has precedence 4, ObjectProperty context requires precedence 5
73 + // Since 4 < 5, parentheses would be added
74 + const funcProp = functionType().setReturnType(stringType);
75 + const obj = objectType().add(property('callback', funcProp)).formatInline();
76 + const result = stringify(obj);
77 +
78 + // Function type (precedence 4) < ObjectProperty (precedence 5), so parentheses added
79 + expect(result).toBe('{ callback: (() => string) }');
80 +});
81 +
82 +test('optional property with union type adds parentheses', () => {
83 + const unionProp = unionType([stringType, numberType]);
84 + const obj = objectType().add(property('value', unionProp).optional()).formatInline();
85 + const result = stringify(obj);
86 +
87 + expect(result).toBe('{ value?: (string | number) }');
88 +});
89 +
90 +test('readonly property with union type adds parentheses', () => {
91 + const unionProp = unionType([stringType, numberType]);
92 + const obj = objectType().add(property('value', unionProp).readonly()).formatInline();
93 + const result = stringify(obj);
94 +
95 + expect(result).toBe('{ readonly value: (string | number) }');
96 +});
ts/packages/ts-builders/src/Property.ts
+3 -1
@@ -1,6 +1,7 @@
1 import type { BasicBuilder } from './BasicBuilder';
2 import { DocComment } from './DocComment';
3 import { isValidJsIdentifier } from './isValidJsIdentifier';
4 +import { TypeContext } from './OperatorPrecedence';
5 import { TypeBuilder } from './TypeBuilder';
6 import { WellKnownSymbol } from './WellKnownSymbol';
7 import { Writer } from './Writer';
@@ -51,7 +52,8 @@ export class Property implements BasicBuilder {
52 if (this.isOptional) {
53 writer.write('?');
54 }
54 - writer.write(': ').write(this.type);
55 + writer.write(': ');
56 + this.type.writeInContext(writer, TypeContext.ObjectProperty);
57 }
58 }
59