|
1 | 1 | import { |
2 | | - type ArrayModelType, |
3 | | - type Enum, |
4 | 2 | getDoc, |
5 | 3 | getTypeName, |
6 | 4 | type IndeterminateEntity, |
7 | | - isNeverType, |
8 | 5 | isNullType, |
9 | 6 | isTemplateInstance, |
10 | | - type Model, |
11 | 7 | type Program, |
12 | | - type RecordModelType, |
13 | | - type Scalar, |
14 | 8 | type Type, |
15 | 9 | type Union, |
16 | 10 | type UnionVariant, |
17 | 11 | type Value, |
18 | | - walkPropertiesInherited, |
19 | 12 | } from "@typespec/compiler"; |
20 | 13 | import { |
21 | 14 | type AliasStatementNode, |
@@ -75,14 +68,6 @@ export function stripNullVariants(union: Union): { |
75 | 68 | }; |
76 | 69 | } |
77 | 70 |
|
78 | | -/** Generate a GraphQL type name for a templated model (e.g., `ListOfString`). */ |
79 | | -export function getTemplatedModelName(model: Model): string { |
80 | | - const name = getTypeName(model, {}); |
81 | | - const baseName = toTypeName(name.replace(/<[^>]*>/g, "")); |
82 | | - const templateString = getTemplateString(model); |
83 | | - return templateString ? `${baseName}Of${templateString}` : baseName; |
84 | | -} |
85 | | - |
86 | 71 | function splitWithAcronyms( |
87 | 72 | splitFn: (name: string) => string[], |
88 | 73 | skipStart: boolean, |
@@ -233,64 +218,6 @@ function getUnionNameForOperation(program: Program, union: Union): string { |
233 | 218 | return toTypeName(getTypeName(operation)); |
234 | 219 | } |
235 | 220 |
|
236 | | -/** Convert a namespaced name to a single name by replacing dots with underscores. */ |
237 | | -export function getSingleNameWithNamespace(name: string): string { |
238 | | - return name.trim().replace(/\./g, "_"); |
239 | | -} |
240 | | - |
241 | | -/** |
242 | | - * Check if a model is an array type. |
243 | | - */ |
244 | | -export function isArray(model: Model): model is ArrayModelType { |
245 | | - return Boolean(model.indexer && model.indexer.key.name === "integer"); |
246 | | -} |
247 | | - |
248 | | -/** |
249 | | - * Check if a model is a record/map type. |
250 | | - */ |
251 | | -export function isRecordType(type: Model): type is RecordModelType { |
252 | | - return Boolean(type.indexer && type.indexer.key.name === "string"); |
253 | | -} |
254 | | - |
255 | | -/** Check if a model is an array of scalars or enums. */ |
256 | | -export function isScalarOrEnumArray(type: Model): type is ArrayModelType { |
257 | | - return ( |
258 | | - isArray(type) && (type.indexer?.value.kind === "Scalar" || type.indexer?.value.kind === "Enum") |
259 | | - ); |
260 | | -} |
261 | | - |
262 | | -/** Check if a model is an array of unions. */ |
263 | | -export function isUnionArray(type: Model): type is ArrayModelType { |
264 | | - return isArray(type) && type.indexer?.value.kind === "Union"; |
265 | | -} |
266 | | - |
267 | | -/** Extract the element type from an array model, or return the model itself. */ |
268 | | -export function unwrapModel(model: ArrayModelType): Model | Scalar | Enum | Union; |
269 | | -export function unwrapModel(model: Exclude<Model, ArrayModelType>): Model; |
270 | | -export function unwrapModel(model: Model): Model | Scalar | Enum | Union { |
271 | | - if (!isArray(model)) { |
272 | | - return model; |
273 | | - } |
274 | | - |
275 | | - if (model.indexer?.value.kind) { |
276 | | - if (["Model", "Scalar", "Enum", "Union"].includes(model.indexer.value.kind)) { |
277 | | - return model.indexer.value as Model | Scalar | Enum | Union; |
278 | | - } |
279 | | - throw new Error(`Unexpected array type: ${model.indexer.value.kind}`); |
280 | | - } |
281 | | - return model; |
282 | | -} |
283 | | - |
284 | | -/** Unwrap array types to get the inner element type. */ |
285 | | -export function unwrapType(type: Model): Model | Scalar | Enum | Union; |
286 | | -export function unwrapType(type: Type): Type; |
287 | | -export function unwrapType(type: Type): Type { |
288 | | - if (type.kind === "Model") { |
289 | | - return unwrapModel(type); |
290 | | - } |
291 | | - return type; |
292 | | -} |
293 | | - |
294 | 221 | /** Get the GraphQL description for a type from its doc comments. */ |
295 | 222 | export function getGraphQLDoc(program: Program, type: Type): string | undefined { |
296 | 223 | // GraphQL uses CommonMark for descriptions |
@@ -318,15 +245,3 @@ function getTemplateStringInternal( |
318 | 245 | return args.length > 0 ? args.map(toTypeName).join(options.conjunction) : ""; |
319 | 246 | } |
320 | 247 |
|
321 | | -/** Check if a model should be emitted as a GraphQL object type (not an array, record, or never). */ |
322 | | -export function isTrueModel(model: Model): boolean { |
323 | | - return !( |
324 | | - // Array of scalars/enums — represented as a list type, not an object type |
325 | | - isScalarOrEnumArray(model) || |
326 | | - // Array of unions — represented as a list type, not an object type |
327 | | - isUnionArray(model) || |
328 | | - isNeverType(model) || |
329 | | - // Pure record with no properties — emitted as a custom scalar, not an object type |
330 | | - (isRecordType(model) && [...walkPropertiesInherited(model)].length === 0) |
331 | | - ); |
332 | | -} |
0 commit comments