| 80 | * Schema.TaggedClass<X>()("ID", FIELDS_EXPR) |
| 81 | * Returns null if the clause doesn't match. */ |
| 82 | const parseSchemaClassExtends = ( |
| 83 | expression: ts.LeftHandSideExpression, |
| 84 | ): { |
| 85 | kind: "Class" | "TaggedClass"; |
| 86 | identifier: string; |
| 87 | fieldsText: string; |
| 88 | } | null => { |
| 89 | // outermost call: schemaCall(fieldsExpr) for Class, or schemaCall("Tag", fieldsExpr) for TaggedClass |
| 90 | if (!ts.isCallExpression(expression)) return null; |
| 91 | const fieldsCall = expression; |
| 92 | const fieldsArgs = fieldsCall.arguments; |
| 93 | |
| 94 | // The callee is either: |
| 95 | // Schema.Class<X>("ID") (a CallExpression) |
| 96 | // Schema.TaggedClass<X>() (a CallExpression) |
| 97 | const innerCall = fieldsCall.expression; |
| 98 | if (!ts.isCallExpression(innerCall)) return null; |
| 99 | |
| 100 | // innerCall.expression should be `Schema.Class` or `Schema.TaggedClass` |
| 101 | // (possibly with type arguments via PropertyAccessExpression). |
| 102 | const innerCallee = innerCall.expression; |
| 103 | if (!ts.isPropertyAccessExpression(innerCallee)) return null; |
| 104 | if (!ts.isIdentifier(innerCallee.expression) || innerCallee.expression.text !== "Schema") { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | const methodName = innerCallee.name.text; |
| 109 | if (methodName === "Class") { |
| 110 | // Schema.Class<X>("ID")(FIELDS) |
| 111 | if (innerCall.arguments.length !== 1) return null; |
| 112 | const idArg = innerCall.arguments[0]; |
| 113 | if (!ts.isStringLiteral(idArg) && !ts.isNoSubstitutionTemplateLiteral(idArg)) return null; |
| 114 | if (fieldsArgs.length !== 1) return null; |
| 115 | return { |
| 116 | kind: "Class", |
| 117 | identifier: idArg.text, |
| 118 | fieldsText: fieldsArgs[0].getText(), |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | if (methodName === "TaggedClass") { |
| 123 | // Schema.TaggedClass<X>()("Tag", FIELDS) |
| 124 | if (innerCall.arguments.length !== 0) return null; |
| 125 | if (fieldsArgs.length !== 2) return null; |
| 126 | const tagArg = fieldsArgs[0]; |
| 127 | if (!ts.isStringLiteral(tagArg) && !ts.isNoSubstitutionTemplateLiteral(tagArg)) return null; |
| 128 | return { |
| 129 | kind: "TaggedClass", |
| 130 | identifier: tagArg.text, |
| 131 | fieldsText: fieldsArgs[1].getText(), |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | return null; |
| 136 | }; |
| 137 | |
| 138 | /** Build the replacement text for a migrated declaration. */ |
| 139 | const buildReplacement = (info: ClassDeclInfo): string => { |