1- import {
2- SdkArrayType ,
3- SdkDictionaryType ,
4- SdkNullableType ,
5- SdkType
6- } from "@azure-tools/typespec-client-generator-core" ;
71import { Project , SourceFile } from "ts-morph" ;
82import type { TSCodeModel } from "../codemodel/index.js" ;
93import type { SdkContext } from "../utils/interfaces.js" ;
10- import {
11- flattenPropertyModelMap ,
12- emitQueue
13- } from "../framework/hooks/sdkTypes.js" ;
4+ import { flattenPropertyModelMap } from "../framework/hooks/sdkTypes.js" ;
145import {
156 addSerializationFunctions ,
167 emitType ,
@@ -21,28 +12,69 @@ import {
2112/**
2213 * Emit model, enum, and union files from the code model.
2314 *
24- * Serializers stay on the legacy helpers for now, but file selection and the
25- * declaration walk come from the new code model .
15+ * Serializers stay on the legacy helpers for now, but model selection comes
16+ * from the filtered IR rather than the global TCGC emit queue .
2617 */
2718export function emitModelFiles (
2819 project : Project ,
2920 codeModel : TSCodeModel ,
3021 sdkContext : SdkContext
3122) : SourceFile [ ] {
32- for ( const type of emitQueue ) {
33- if ( ! isGenerableType ( type ) ) {
23+ const rawModelLookup = buildRawTypeLookup ( sdkContext . sdkPackage . models , sdkContext ) ;
24+ const rawEnumLookup = buildRawTypeLookup ( sdkContext . sdkPackage . enums , sdkContext ) ;
25+ const rawUnionLookup = buildRawTypeLookup ( sdkContext . sdkPackage . unions , sdkContext ) ;
26+ const includedModelKeys = new Set < string > ( ) ;
27+
28+ for ( const model of codeModel . models ) {
29+ const rawModel = rawModelLookup . get ( getTypeKey ( model . name , model . namespace ) ) ;
30+ if ( ! rawModel ) {
31+ continue ;
32+ }
33+
34+ includedModelKeys . add ( getRawTypeKey ( rawModel , sdkContext ) ) ;
35+ const sourceFile = getOrCreateModelsFile (
36+ project ,
37+ codeModel . settings . sourceRoot ,
38+ model . namespace
39+ ) ;
40+ emitType ( sdkContext , rawModel , sourceFile ) ;
41+ }
42+
43+ for ( const enumType of codeModel . enums ) {
44+ const rawEnum = rawEnumLookup . get ( getTypeKey ( enumType . name , enumType . namespace ) ) ;
45+ if ( ! rawEnum ) {
46+ continue ;
47+ }
48+
49+ const sourceFile = getOrCreateModelsFile (
50+ project ,
51+ codeModel . settings . sourceRoot ,
52+ enumType . namespace
53+ ) ;
54+ emitType ( sdkContext , rawEnum , sourceFile ) ;
55+ }
56+
57+ for ( const unionType of codeModel . unions ) {
58+ const rawUnion = rawUnionLookup . get (
59+ getTypeKey ( unionType . name , unionType . namespace )
60+ ) ;
61+ if ( ! rawUnion ) {
3462 continue ;
3563 }
3664
3765 const sourceFile = getOrCreateModelsFile (
3866 project ,
3967 codeModel . settings . sourceRoot ,
40- getModelNamespaces ( sdkContext , type )
68+ unionType . namespace
4169 ) ;
42- emitType ( sdkContext , type , sourceFile ) ;
70+ emitType ( sdkContext , rawUnion , sourceFile ) ;
4371 }
4472
45- for ( const [ property ] of flattenPropertyModelMap ) {
73+ for ( const [ property , baseModel ] of flattenPropertyModelMap ) {
74+ if ( ! includedModelKeys . has ( getRawTypeKey ( baseModel , sdkContext ) ) ) {
75+ continue ;
76+ }
77+
4678 const sourceFile = getOrCreateModelsFile (
4779 project ,
4880 codeModel . settings . sourceRoot ,
@@ -54,19 +86,23 @@ export function emitModelFiles(
5486 return cleanupEmptyModelFiles ( project , codeModel . settings . sourceRoot ) ;
5587}
5688
57- function isGenerableType (
58- type : SdkType
59- ) : type is SdkArrayType | SdkDictionaryType | SdkNullableType | SdkType {
60- return (
61- type . kind === "model" ||
62- type . kind === "enum" ||
63- type . kind === "union" ||
64- type . kind === "dict" ||
65- type . kind === "array" ||
66- type . kind === "nullable"
89+ function buildRawTypeLookup < T extends { name : string } > (
90+ types : readonly T [ ] ,
91+ sdkContext : SdkContext
92+ ) : Map < string , T > {
93+ return new Map (
94+ types . map ( ( type ) => [ getRawTypeKey ( type , sdkContext ) , type ] as const )
6795 ) ;
6896}
6997
98+ function getRawTypeKey ( type : { name : string } , sdkContext : SdkContext ) : string {
99+ return getTypeKey ( type . name , getModelNamespaces ( sdkContext , type as any ) ) ;
100+ }
101+
102+ function getTypeKey ( name : string , namespace : string [ ] ) : string {
103+ return `${ namespace . join ( "/" ) } :${ name } ` ;
104+ }
105+
70106function getOrCreateModelsFile (
71107 project : Project ,
72108 sourceRoot : string ,
0 commit comments