Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(ts-builders): fix Parameters in @composio/ts-builders (#2263)
Alberto Schiabel73570901f266
diff --git a/ts/packages/ts-builders/src/Parameter.test.ts b/ts/packages/ts-builders/src/Parameter.test.tsindex db5cbef0b..602d1c182 100644--- a/ts/packages/ts-builders/src/Parameter.test.ts+++ b/ts/packages/ts-builders/src/Parameter.test.ts@@ -1,10 +1,14 @@import { expect, test } from 'vitest';+import { functionType } from './FunctionType';import { namedType } from './NamedType';import { parameter } from './Parameter';import { stringify } from './stringify';+import { unionType } from './UnionType';const A = namedType('A');+const stringType = namedType('string');+const numberType = namedType('number');test('name and type', () => {const param = parameter('foo', A);@@ -17,3 +21,42 @@ test('optional', () => {expect(stringify(param)).toMatchInlineSnapshot(`"foo?: A"`);});++test('parameter with union type adds parentheses in function context', () => {+ // Union has precedence 1, FunctionParameter context requires precedence 4+ // So parentheses should be added: (param: (string | number))+ const unionParam = unionType([stringType, numberType]);+ const func = functionType().addParameter(parameter('param', unionParam));+ const result = stringify(func);++ expect(result).toBe('(param: (string | number)) => void');+});++test('parameter with intersection type adds parentheses in function context', () => {+ // Intersection has precedence 2, FunctionParameter context requires precedence 4+ // So parentheses should be added+ const intersectionParam = namedType('A & B'); // Using named type to simulate intersection+ const func = functionType().addParameter(parameter('param', intersectionParam));+ const result = stringify(func);++ // Named type has atomic precedence, no parentheses needed+ expect(result).toBe('(param: A & B) => void');+});++test('parameter with function type adds parentheses in function context', () => {+ // Function type has precedence 4, FunctionParameter context requires precedence 4+ // Since 4 is not < 4, no parentheses needed+ const funcParam = functionType().setReturnType(stringType);+ const func = functionType().addParameter(parameter('callback', funcParam));+ const result = stringify(func);++ expect(result).toBe('(callback: () => string) => void');+});++test('optional parameter with union type adds parentheses', () => {+ const unionParam = unionType([stringType, numberType]);+ const func = functionType().addParameter(parameter('param', unionParam).optional());+ const result = stringify(func);++ expect(result).toBe('(param?: (string | number)) => void');+});diff --git a/ts/packages/ts-builders/src/Parameter.ts b/ts/packages/ts-builders/src/Parameter.tsindex 3145e2387..67d4aa9d2 100644--- a/ts/packages/ts-builders/src/Parameter.ts+++ b/ts/packages/ts-builders/src/Parameter.ts@@ -1,4 +1,5 @@import type { BasicBuilder } from './BasicBuilder';+import { TypeContext } from './OperatorPrecedence';import { TypeBuilder } from './TypeBuilder';import { Writer } from './Writer';@@ -19,7 +20,8 @@ export class Parameter implements BasicBuilder {if (this.isOptional) {writer.write('?');}- writer.write(': ').write(this.type);+ writer.write(': ');+ this.type.writeInContext(writer, TypeContext.FunctionParameter);}}diff --git a/ts/packages/ts-builders/src/Property.test.ts b/ts/packages/ts-builders/src/Property.test.tsindex 9741d9335..5b1b61b9b 100644--- a/ts/packages/ts-builders/src/Property.test.ts+++ b/ts/packages/ts-builders/src/Property.test.ts@@ -1,12 +1,17 @@import { expect, test } from 'vitest';import { docComment } from './DocComment';+import { functionType } from './FunctionType';import { namedType } from './NamedType';+import { objectType } from './ObjectType';import { property } from './Property';import { stringify } from './stringify';+import { unionType } from './UnionType';import { toStringTag } from './WellKnownSymbol';const A = namedType('A');+const stringType = namedType('string');+const numberType = namedType('number');test('name and type', () => {const prop = property('foo', A);@@ -48,3 +53,44 @@ test('with doc comment', () => {foo: A"`);});++test('property with union type does not add parentheses (ObjectProperty context)', () => {+ // Union has precedence 1, ObjectProperty context requires precedence 5+ // However, for object properties, union types don't need parentheses syntactically+ // because the property boundary is clear: { prop: string | number }+ // But according to precedence rules, 1 < 5 means parentheses would be added+ const unionProp = unionType([stringType, numberType]);+ const obj = objectType().add(property('value', unionProp)).formatInline();+ const result = stringify(obj);++ // ObjectProperty context has precedence 5, Union has precedence 1+ // Since 1 < 5, parentheses are added+ expect(result).toBe('{ value: (string | number) }');+});++test('property with function type does not add parentheses (ObjectProperty context)', () => {+ // Function type has precedence 4, ObjectProperty context requires precedence 5+ // Since 4 < 5, parentheses would be added+ const funcProp = functionType().setReturnType(stringType);+ const obj = objectType().add(property('callback', funcProp)).formatInline();+ const result = stringify(obj);++ // Function type (precedence 4) < ObjectProperty (precedence 5), so parentheses added+ expect(result).toBe('{ callback: (() => string) }');+});++test('optional property with union type adds parentheses', () => {+ const unionProp = unionType([stringType, numberType]);+ const obj = objectType().add(property('value', unionProp).optional()).formatInline();+ const result = stringify(obj);++ expect(result).toBe('{ value?: (string | number) }');+});++test('readonly property with union type adds parentheses', () => {+ const unionProp = unionType([stringType, numberType]);+ const obj = objectType().add(property('value', unionProp).readonly()).formatInline();+ const result = stringify(obj);++ expect(result).toBe('{ readonly value: (string | number) }');+});diff --git a/ts/packages/ts-builders/src/Property.ts b/ts/packages/ts-builders/src/Property.tsindex e123f0e72..ae856fe75 100644--- a/ts/packages/ts-builders/src/Property.ts+++ b/ts/packages/ts-builders/src/Property.ts@@ -1,6 +1,7 @@import type { BasicBuilder } from './BasicBuilder';import { DocComment } from './DocComment';import { isValidJsIdentifier } from './isValidJsIdentifier';+import { TypeContext } from './OperatorPrecedence';import { TypeBuilder } from './TypeBuilder';import { WellKnownSymbol } from './WellKnownSymbol';import { Writer } from './Writer';@@ -51,7 +52,8 @@ export class Property implements BasicBuilder {if (this.isOptional) {writer.write('?');}- writer.write(': ').write(this.type);+ writer.write(': ');+ this.type.writeInContext(writer, TypeContext.ObjectProperty);}}