Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | /* eslint-disable @typescript-eslint/no-duplicate-enum-values */ |
| 2 | |
| 3 | /** |
| 4 | * TypeScript operator precedence levels for automatic parentheses placement. |
| 5 | * Higher numbers indicate higher precedence (bind more tightly). |
| 6 | * |
| 7 | * Based on TypeScript/JavaScript operator precedence: |
| 8 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence |
| 9 | */ |
| 10 | export enum OperatorPrecedence { |
| 11 | // Lowest precedence |
| 12 | Union = 1, // A | B |
| 13 | Intersection = 2, // A & B |
| 14 | Conditional = 3, // A extends B ? C : D |
| 15 | |
| 16 | // Typeof types are special cases, see: |
| 17 | // https://stackoverflow.com/questions/70102722/whats-the-precedence-of-typescript-operators#comment123924051_70102722 |
| 18 | TypeofType = 4, // typeof T |
| 19 | |
| 20 | // Function types |
| 21 | FunctionType = 4, // () => void |
| 22 | |
| 23 | // Object types (lower precedence than arrays so they need parentheses in more contexts) |
| 24 | ObjectType = 5, // { a: string } |
| 25 | |
| 26 | // Array and tuple types |
| 27 | ArrayType = 6, // T[] |
| 28 | TupleType = 6, // [A, B, C] |
| 29 | |
| 30 | // Indexing and property access |
| 31 | IndexAccess = 7, // T[K] |
| 32 | |
| 33 | // Unary operators |
| 34 | KeyofType = 8, // keyof T |
| 35 | |
| 36 | // Highest precedence (atomic types, least likely to need parentheses) |
| 37 | Atomic = 10, // string, number, boolean, literal types, named types |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Context in which a type is being written, used to determine if parentheses are needed |
| 42 | */ |
| 43 | export enum TypeContext { |
| 44 | Root = 'root', // Top-level, no context |
| 45 | UnionMember = 'union_member', // Inside a union type |
| 46 | IntersectionMember = 'intersection_member', // Inside an intersection type |
| 47 | ArrayElement = 'array_element', // T in T[] |
| 48 | TupleElement = 'tuple_element', // T in [T, ...] |
| 49 | IndexAccess = 'index_access', // T in T[K] or K in T[K] |
| 50 | KeyofOperand = 'keyof_operand', // T in keyof T |
| 51 | TypeofOperand = 'typeof_operand', // T in typeof T |
| 52 | FunctionReturn = 'function_return', // T in () => T |
| 53 | FunctionParameter = 'function_parameter', // T in (x: T) => void |
| 54 | ObjectProperty = 'object_property', // T in { prop: T } |
| 55 | ConditionalCheck = 'conditional_check', // T in T extends U ? V : W |
| 56 | ConditionalTrue = 'conditional_true', // V in T extends U ? V : W |
| 57 | ConditionalFalse = 'conditional_false', // W in T extends U ? V : W |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Maps contexts to the minimum precedence level that doesn't require parentheses |
| 62 | */ |
| 63 | export const CONTEXT_PRECEDENCE: Record<TypeContext, OperatorPrecedence> = { |
| 64 | [TypeContext.Root]: OperatorPrecedence.Union, |
| 65 | [TypeContext.UnionMember]: OperatorPrecedence.ArrayType, // Functions (4) < ArrayType (6) = true, parentheses added |
| 66 | [TypeContext.IntersectionMember]: OperatorPrecedence.Conditional, |
| 67 | [TypeContext.ArrayElement]: OperatorPrecedence.Atomic, // Objects (5) < Atomic (10) = true, parentheses added |
| 68 | [TypeContext.TupleElement]: OperatorPrecedence.ArrayType, |
| 69 | [TypeContext.IndexAccess]: OperatorPrecedence.ArrayType, // Objects (5) < ArrayType (6) = true, Arrays (6) < ArrayType (6) = false |
| 70 | [TypeContext.KeyofOperand]: OperatorPrecedence.ObjectType, // Functions (4) < ObjectType (5) = true, Unions (1) < ObjectType (5) = true, Objects (5) < ObjectType (5) = false, Arrays (6) < ObjectType (5) = false |
| 71 | [TypeContext.FunctionReturn]: OperatorPrecedence.FunctionType, |
| 72 | [TypeContext.TypeofOperand]: OperatorPrecedence.TypeofType, |
| 73 | [TypeContext.FunctionParameter]: OperatorPrecedence.FunctionType, |
| 74 | [TypeContext.ObjectProperty]: OperatorPrecedence.ObjectType, |
| 75 | [TypeContext.ConditionalCheck]: OperatorPrecedence.Conditional, |
| 76 | [TypeContext.ConditionalTrue]: OperatorPrecedence.Conditional, |
| 77 | [TypeContext.ConditionalFalse]: OperatorPrecedence.Conditional, |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * Determines if parentheses are needed when writing a type in a given context |
| 82 | */ |
| 83 | export function needsParentheses( |
| 84 | typePrecedence: OperatorPrecedence, |
| 85 | context: TypeContext |
| 86 | ): boolean { |
| 87 | const contextPrecedence = CONTEXT_PRECEDENCE[context]; |
| 88 | return typePrecedence < contextPrecedence; |
| 89 | } |