Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge EFv2 work to main #5798

Closed
wants to merge 287 commits into from
Closed
Changes from all commits
Commits
Show all changes
287 commits
Select commit Hold shift + click to select a range
a09301d
Support basic and model serializers
joheredi Aug 22, 2024
4fb3a50
Fix some format
joheredi Aug 22, 2024
1735401
Add basic model deserializer
joheredi Aug 23, 2024
7479795
Support utcDateTime
joheredi Aug 23, 2024
cf3495a
update imports and deps
joheredi Aug 23, 2024
9c199f4
support date encoding
joheredi Aug 23, 2024
bd9037e
Implement a few new typekits
bterlson Aug 23, 2024
b536b3e
Implement http typekits
bterlson Aug 23, 2024
3745030
Implement more typekit methods
bterlson Aug 24, 2024
1e886b3
Support utcDateTimeScalars
joheredi Aug 23, 2024
bce5879
fix missing import
joheredi Aug 24, 2024
e1bd386
remove only
joheredi Aug 24, 2024
1704166
Support Record and Array Serializers
joheredi Aug 26, 2024
80abd43
Organize output in folders
joheredi Aug 27, 2024
42b6c11
support basic client context
joheredi Aug 27, 2024
62ed94a
Collect types from interfaces
joheredi Aug 27, 2024
54f8dad
support appending or perpending additional parameters to operation.pa…
joheredi Aug 27, 2024
3238039
Generate http operations
joheredi Aug 28, 2024
b362495
add simple test for httpOperation typekit
joheredi Sep 3, 2024
1fa79c1
Low cost getPlausibleName
joheredi Sep 3, 2024
c2aef71
export model typekit and enhance EF test host
joheredi Sep 3, 2024
d99a487
Add type transform to convert models from client to wire and back
joheredi Sep 3, 2024
22f11e1
Cleanup serializer deserializers
joheredi Sep 4, 2024
0280f18
Add HttpRequest and HttpRequestOptions components
joheredi Sep 5, 2024
13fcd6e
Add httpResponse typekit
joheredi Sep 9, 2024
23853dc
update exports
joheredi Sep 9, 2024
aea55dc
Add getEffectiveModel and getResponses typekits
joheredi Sep 10, 2024
04a331e
Typescript Http Request and Response
joheredi Sep 10, 2024
3233a1e
Update http request components
joheredi Sep 11, 2024
3817643
Add union isExpresion
joheredi Sep 11, 2024
61fe412
Update graph traversal
joheredi Sep 11, 2024
7642e94
Update dependency and fix directories
joheredi Sep 12, 2024
93d4d2a
Move plausible name to type and handle template instances
joheredi Sep 12, 2024
8798f2b
Improve name resolution handling in ef/typescript
joheredi Sep 12, 2024
f628b97
cleanup ts emitter components
joheredi Sep 12, 2024
bfa69a9
Add end to end scenario test
joheredi Sep 12, 2024
b379237
Add Scenario Test Harness
joheredi Sep 13, 2024
d8d535d
Support tags for other languages
joheredi Sep 13, 2024
7be5c3c
Merge pull request #6 from joheredi/alloy-test-harness
joheredi Sep 13, 2024
49bbe3b
Implement getParameters typekit
joheredi Sep 14, 2024
98a9639
Update body handling logic
joheredi Sep 16, 2024
daa06c1
Initial support for discriminated unions
joheredi Sep 16, 2024
da117c4
Generate operations per namespace
joheredi Sep 17, 2024
c94396e
update deps and export barrell
joheredi Sep 17, 2024
c9746d4
Fixes to fetch wrapper
joheredi Sep 17, 2024
e490358
Only one barrell file
joheredi Sep 17, 2024
721da83
Account for bodyRoot
joheredi Sep 17, 2024
d24bd09
Enable alloy prettier pluggin and format js emitter
joheredi Sep 17, 2024
d6f05fc
format
joheredi Sep 17, 2024
5946512
Fix exports
joheredi Sep 18, 2024
18718aa
Handle error and bodyless responses
joheredi Sep 18, 2024
71c8c75
Support options bag and optionality checks
joheredi Sep 18, 2024
8359cde
Refactor ClientContext
joheredi Sep 19, 2024
9c350d9
Formatting
joheredi Sep 19, 2024
ef8741c
Merge remote-tracking branch 'upstream/main' into alloy
joheredi Sep 19, 2024
e37c6d1
Fix breaking changes from TS update
joheredi Sep 19, 2024
0ab6c50
Pull alloy from npmjs
joheredi Sep 20, 2024
a350bf4
Merge remote-tracking branch 'upstream/main' into alloy
joheredi Sep 20, 2024
b3e5fa7
Add Classical Clients
joheredi Sep 23, 2024
01685af
Fix tests and generate clientlet for interfaces
joheredi Sep 23, 2024
f4b0f69
Update alloy deps
joheredi Sep 23, 2024
49ebcb0
Merge remote-tracking branch 'upstream/main' into alloy
joheredi Sep 23, 2024
62e3575
Merge main and move typekits to experimental
joheredi Sep 24, 2024
7e25e03
add todo and petstore output
joheredi Sep 24, 2024
05f18e7
Format after build
joheredi Sep 24, 2024
df60e23
can compile after removing html-program-viewer
Oct 22, 2024
21b93f9
initial commit with ClientLibrary typekit
Oct 24, 2024
f20526b
temp commit
Oct 25, 2024
afed6c6
check out client.tsx changes
Oct 28, 2024
facd479
get model typekit to work
Oct 28, 2024
efb930c
Merge pull request #8 from iscai-msft/clientLibraryTypeKit
iscai-msft Oct 28, 2024
1b33e34
additional typekit sketching
Oct 28, 2024
c6d28c3
Add builtin kit
joheredi Oct 28, 2024
4eb4b23
Add tests
joheredi Oct 28, 2024
f3076ed
working through model and modelproperty kits
Oct 28, 2024
1788e2d
Merge branch 'alloy' of https://github.com/bterlson/typespec into alloy
Oct 28, 2024
5a4c25f
Fix bultin types
joheredi Oct 29, 2024
65e5116
Create ClientLibrary packages for 3p and Azure
joheredi Oct 29, 2024
44665b8
Merge pull request #9 from joheredi/client-library-packages
iscai-msft Oct 29, 2024
48e2643
Format and Test for client-library
joheredi Oct 29, 2024
6d1fda4
before trying nested approach
Oct 30, 2024
fa726a7
Merge branch 'alloy' of https://github.com/bterlson/typespec into alloy
Oct 30, 2024
e955615
Merge branch 'main' into alloy
bterlson Oct 31, 2024
435047f
Add some missing files
bterlson Oct 31, 2024
8701d69
adding tests, stuck on credential
Oct 31, 2024
19b1285
Merge branch 'alloy' of https://github.com/bterlson/typespec into alloy
Oct 31, 2024
4163016
Move Client Library Typekits out of JS emitter
joheredi Oct 31, 2024
f07f5f7
Merge pull request #12 from joheredi/move-client-library
iscai-msft Nov 1, 2024
afe9521
Add ClientLibraryContext
joheredi Nov 1, 2024
2626a68
Fallback when no service defined
joheredi Nov 1, 2024
1a182a2
return credential parameter with string literal type
Nov 4, 2024
d8e286f
format and add usage, access, and name to typekits
Nov 4, 2024
00ec0a1
add accessors for return types onto sdk operation kit
Nov 5, 2024
c99612c
switch to getParams and isPubliclyInitializable
Nov 6, 2024
fddddeb
list clients also takes interface
Nov 6, 2024
c7a4b1c
have getName take a client
Nov 6, 2024
5f59d55
fix getname for client
Nov 6, 2024
0f6b124
have list clients take in namespace or client
Nov 6, 2024
598e0f3
move listSubNamespaces onto listNamespaces
Nov 6, 2024
4a58358
add tests for subclients
Nov 6, 2024
3212e9e
format
Nov 6, 2024
819b157
add tests for getting subclients
Nov 6, 2024
12bd772
switch to clients having a list of constructors
Nov 7, 2024
de30f06
add constructors to client
Nov 8, 2024
2cc861d
fix multiple servers test
Nov 8, 2024
2a21cd5
add properties to the list
Nov 11, 2024
30f7fb5
Merge branch 'main' into alloy
bterlson Nov 12, 2024
e9329b9
Fix circular references
bterlson Nov 13, 2024
c4d34bf
clean up is on client code in model property typekit
Nov 18, 2024
2429714
temporarily use name sort for finding params
Nov 18, 2024
2de2da7
add credential tests
Nov 19, 2024
a254092
TypeCollector skip template instances
joheredi Nov 20, 2024
d45a4de
fix getting service operations and namespaces
Dec 3, 2024
fba3b82
add $.client.getClient()
Dec 3, 2024
953c159
Add Zod Sketch project
joheredi Dec 3, 2024
142c158
Add client.flat helper and client cache
joheredi Dec 6, 2024
26a343e
Add helpers to ef-typescript
joheredi Dec 7, 2024
cd54a9c
update todo spec
joheredi Dec 7, 2024
40ac28b
Add Symbol to identify credential parameters
joheredi Dec 7, 2024
31a88c8
Finished primitives
MattGertz Dec 9, 2024
ae4ea83
Add max and min value typekit
joheredi Dec 9, 2024
105c3fa
Merge branch 'alloy' of https://github.com/bterlson/typespec into alloy
MattGertz Dec 9, 2024
1b01d2a
Primitives and min/max values supported
MattGertz Dec 9, 2024
4ecadcb
Fixed stupid logic bug for basic value vs. defined value
MattGertz Dec 9, 2024
0be9851
Max and Min Length typekits
joheredi Dec 10, 2024
105c488
Use client library
joheredi Dec 10, 2024
98c487a
Move data type discovery to clientLibrary
joheredi Dec 10, 2024
2c7a837
Cleanup JS emitter
joheredi Dec 10, 2024
08fefd4
Update integration tests
joheredi Dec 10, 2024
b5e0956
name client to handle multi client in the future
joheredi Dec 10, 2024
e2fe889
Added support for string min/max lengths
MattGertz Dec 10, 2024
f09ee41
Remove unnecessary Reference components
joheredi Dec 10, 2024
65daee1
Added support for enums (native only)
MattGertz Dec 10, 2024
845248a
Merge branch 'alloy' of https://github.com/bterlson/typespec into alloy
MattGertz Dec 10, 2024
113799d
Forgot to quote string values
MattGertz Dec 10, 2024
5f00461
Got rid of unused EnumValue include
MattGertz Dec 11, 2024
e178ae0
Fixed bug in Int16 where I forget to remove obsolete code in favor oo…
MattGertz Dec 11, 2024
1a4b663
Merge remote-tracking branch 'upstream/main' into alloy
joheredi Dec 11, 2024
db64259
Fix breaking changes
joheredi Dec 11, 2024
2a55f4a
Fix breaking changes
joheredi Dec 11, 2024
a74dfec
Use typekit subexport
joheredi Dec 11, 2024
cd1540f
Update zod emitted
joheredi Dec 11, 2024
a003912
Support date extensions
MattGertz Dec 16, 2024
e5a4fda
Merge branch 'alloy' of https://github.com/bterlson/typespec into alloy
MattGertz Dec 16, 2024
8075443
Stubbed out arrays; support nested models
MattGertz Dec 16, 2024
f76ea5d
Stubbed out records
MattGertz Dec 16, 2024
3221d1c
Explicitly cased out empty string value on Model names instead of sup…
MattGertz Dec 16, 2024
d617e9e
Only Proxy typekit namespaces
joheredi Dec 16, 2024
445be64
Improve Intrinsic Scalar handling
joheredi Dec 16, 2024
d185dd5
Update listServiceOperations to set returnType to the body type
joheredi Dec 16, 2024
4ce3158
Add listDataTypes function to collect all data types from a client re…
joheredi Dec 16, 2024
a4e5ef3
Generate Cadl Ranch Specs
joheredi Dec 16, 2024
9b34172
Improve cadl ranch gen script
joheredi Dec 16, 2024
2b95a68
Add min and max length typekits
joheredi Dec 16, 2024
4820a99
fine tune script
joheredi Dec 16, 2024
18cfccd
Support min and maxItems on arrays (etc.)
MattGertz Dec 16, 2024
78bf8cc
Use $.model.is() and .expresion() for identification instead of strin…
MattGertz Dec 17, 2024
2c9a876
Switched records and arrays to using toolkits
MattGertz Dec 17, 2024
9547276
Added support for record and array element constraints; fixed bigint …
MattGertz Dec 17, 2024
6fe0497
Updated some comments on next work items, added a test case
MattGertz Dec 17, 2024
2a014b4
Completely rewrote constraints code to recursive through all basal & …
MattGertz Dec 17, 2024
f095590
Finished support for optional; added test case for nullable
MattGertz Dec 17, 2024
3e2519b
Made it easier to add new numeric constraints and analyze them for re…
MattGertz Dec 17, 2024
151cb32
Got rid of superfluous scalar calls, fixed a minor error & a whitespa…
MattGertz Dec 17, 2024
7f01994
Removed erroneous & misleading comment
MattGertz Dec 17, 2024
06bb883
And remove another completely incorrect comment that got leftover
MattGertz Dec 17, 2024
8d7ceeb
Added support for unions
MattGertz Dec 18, 2024
d57fe3f
Completed model and model-property referencing. (Model properties use…
MattGertz Dec 18, 2024
5911245
Got rid of erroneous nullable comment since I already fixed it.
MattGertz Dec 18, 2024
a6c7825
Fixed bug in model reference not emitting .optional() if necessary.
MattGertz Dec 18, 2024
bd68ba4
Fixed one test case to account for model references
MattGertz Dec 18, 2024
89ef29f
Cleanup of code, fixing typos in variable names, moving comments to b…
MattGertz Dec 18, 2024
5a9624c
Use references for passing arguments
joheredi Dec 17, 2024
4cd4ab2
Handle TypeExpression for UnionVariant
joheredi Dec 17, 2024
534134b
Add experimental typkit export
joheredi Dec 17, 2024
dea3e1d
Add support for inheritance based discriminated unions
joheredi Dec 17, 2024
cae9140
Handle model extending Record
joheredi Dec 18, 2024
00f8364
Handle correctly models extending and spreading Record
joheredi Dec 18, 2024
92c9e27
Support extends and spread models
joheredi Dec 19, 2024
35fac5b
Collect types from client constructor
joheredi Dec 19, 2024
1cf8dad
re-gen petstore
joheredi Dec 19, 2024
8a1be87
allow build test projects
joheredi Dec 19, 2024
18d3683
install generated deps
joheredi Dec 19, 2024
0b79761
update gitignore
joheredi Dec 19, 2024
7d6125e
Add Typekit to determine if 2 clients have the same constructor
joheredi Dec 20, 2024
288481e
Handle subclients gracefuly
joheredi Dec 20, 2024
b15ef9a
Add dev dependencies to simplify test validation
joheredi Dec 20, 2024
2d5d144
Fixes in serialization access to properties
joheredi Dec 21, 2024
433abcc
Fix incorrect Array Transform
joheredi Dec 21, 2024
a94a9ee
Handle model extending record and skip never properties in model tran…
joheredi Dec 22, 2024
8b3b356
Unpack multipart
joheredi Jan 3, 2025
6ee6fb4
Support explicit multipart form data
joheredi Jan 11, 2025
07b5399
Add array and scalar cadl ranch test
joheredi Jan 13, 2025
755534b
Handle literal contentType
joheredi Jan 13, 2025
e99b48d
Fix isErrorResponse
joheredi Jan 15, 2025
c055757
Use Multipart helpers and better File types
joheredi Jan 15, 2025
14992e6
Spike of clean up for type collection
joheredi Jan 22, 2025
ec40187
Update alloy packages
joheredi Jan 22, 2025
b1fd144
fix alloy dev versions
joheredi Jan 23, 2025
501fbc8
fix incorrect name check
joheredi Jan 23, 2025
9aa1360
revert gen changes
joheredi Jan 23, 2025
db2ebc4
do not collect anonymous models
joheredi Jan 23, 2025
6ab2e9b
More changes
joheredi Jan 23, 2025
d2368d6
Use ClientLibrary context
joheredi Jan 23, 2025
e0c70fe
Initial MPFD support
joheredi Jan 24, 2025
9794fbd
Fix filepart generation
joheredi Jan 24, 2025
6cdbbe2
Update scenario test
joheredi Jan 24, 2025
d06e587
Cleanup multipart generation
joheredi Jan 24, 2025
c31d08d
Rollback flattenResponses
joheredi Jan 24, 2025
b0af28a
Fix scenario tests
joheredi Jan 27, 2025
682c644
Cleanup harness
joheredi Jan 27, 2025
1894cf1
Support multiple top level clients
joheredi Jan 27, 2025
12ceb9c
Fix spread serializer issue
joheredi Jan 27, 2025
b559362
Fix infinite recursion by removing unecessary recursive calls in type…
joheredi Jan 27, 2025
d09180a
Add inheritance scenario
joheredi Jan 28, 2025
90c52e4
Fix missing declarations for derived types
joheredi Jan 28, 2025
60e6e26
Fix missing constant headers and filter out non-project namespaces
joheredi Jan 28, 2025
f0b3891
re-generate petstore and todo
joheredi Jan 28, 2025
b1c7e9c
Cleanup tests
joheredi Jan 28, 2025
1ebed1e
Merge branch 'main' of https://github.com/Microsoft/typespec into alloy
tjprescott Jan 28, 2025
3c45ddb
Remove unwanted packages.
tjprescott Jan 28, 2025
651ace4
Restore html-program-viewer.
tjprescott Jan 28, 2025
ad39aec
Remove http-client-library-azure.
tjprescott Jan 28, 2025
a9438c1
Remove http-client-javascript. Rename http-client-library to http-client
tjprescott Jan 29, 2025
74192cd
Create efv2merge-2025-0-29-18-4-56.md
tjprescott Jan 29, 2025
c04ded9
Merge branch 'main' of https://github.com/Microsoft/typespec into efv…
tjprescott Jan 29, 2025
e919f00
Cleanup for CI.
tjprescott Jan 29, 2025
1c6ec08
Merge branch 'main' of https://github.com/Microsoft/typespec into efv…
tjprescott Jan 29, 2025
4c05421
Update dependencies and format fixes.
tjprescott Jan 29, 2025
0d89af0
Fix misplacement of http typekits.
tjprescott Jan 29, 2025
5bd4ed2
Merge branch 'main' of https://github.com/Microsoft/typespec into efv…
tjprescott Jan 30, 2025
d4842fd
Merge branch 'main' of https://github.com/Microsoft/typespec into efv…
tjprescott Jan 30, 2025
b966516
Resolving build errors.
tjprescott Jan 31, 2025
9b34c93
Update imports.
tjprescott Jan 31, 2025
4f2648e
Relocate http-client typekits to experimental folder and fix build er…
tjprescott Jan 31, 2025
89c8aa3
Merge branch 'main' of https://github.com/Microsoft/typespec into efv…
tjprescott Jan 31, 2025
ce60d97
Change new package versions to match others.
tjprescott Feb 1, 2025
123e60f
Merge branch 'main' into efv2merge
tjprescott Feb 1, 2025
75ef87a
Add postinstall warning to emitter-framework package.
tjprescott Feb 1, 2025
f3b705f
Fix dependency mismatches.
tjprescott Feb 3, 2025
8d7f930
Temporarily disable failing test.
tjprescott Feb 3, 2025
b0d4a3a
Merge branch 'main' of https://github.com/Microsoft/typespec into efv…
tjprescott Feb 3, 2025
982c750
Remove unwanted files.
tjprescott Feb 4, 2025
3591e09
Workaround for playground e2e tests.
tjprescott Feb 4, 2025
f5a29ed
Resolve differences.
tjprescott Feb 5, 2025
5b4812f
Resolve build/test errors.
tjprescott Feb 5, 2025
4d08a61
Fix up tests.
tjprescott Feb 5, 2025
8b78fcb
Cleaning up tests.
tjprescott Feb 5, 2025
522c47a
Format.
tjprescott Feb 5, 2025
2db9da2
Merge branch 'main' of https://github.com/Microsoft/typespec into efv…
tjprescott Feb 5, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .chronus/changes/efv2merge-2025-0-29-10-39-55.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
- "@typespec/http"
---

Adds several TypeKits.
11 changes: 11 additions & 0 deletions .chronus/changes/efv2merge-2025-0-29-18-4-56.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---
Adds integration of experimental v2 emitter framework with TypeScript components.
Allow semantic walker to visit derived types.
Addition of numerous new TypeKits.


1 change: 1 addition & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
@@ -91,6 +91,7 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@alloy-js/core": "~0.4.0",
"@babel/code-frame": "~7.26.2",
"ajv": "~8.17.1",
"change-case": "~5.4.4",
4 changes: 3 additions & 1 deletion packages/compiler/src/core/emitter-utils.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,9 @@ export interface EmitFileOptions {
export async function emitFile(program: Program, options: EmitFileOptions): Promise<void> {
// ensure path exists
const outputFolder = getDirectoryPath(options.path);
await program.host.mkdirp(outputFolder);
if (outputFolder) {
await program.host.mkdirp(outputFolder);
}
const content =
options.newLine && options.newLine === "crlf"
? options.content.replace(/(\r\n|\n|\r)/gm, "\r\n")
23 changes: 22 additions & 1 deletion packages/compiler/src/core/program.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OutputDirectory, render } from "@alloy-js/core";
import { EmitterOptions } from "../config/types.js";
import { createAssetEmitter } from "../emitter-framework/asset-emitter.js";
import { setCurrentProgram } from "../experimental/typekit/index.js";
@@ -17,6 +18,7 @@ import { createBinder } from "./binder.js";
import { Checker, createChecker } from "./checker.js";
import { createSuppressCodeFix } from "./compiler-code-fixes/suppress.codefix.js";
import { compilerAssert } from "./diagnostics.js";
import { emitFile } from "./emitter-utils.js";
import { resolveTypeSpecEntrypoint } from "./entrypoint-resolution.js";
import { ExternalError } from "./external-error.js";
import { getLibraryUrlsLoaded } from "./library.js";
@@ -573,12 +575,31 @@ export async function compile(
},
};
try {
await emitter.emitFunction(context);
const result = (await emitter.emitFunction(context)) as any;
if (typeof result === "function") {
// assume this is an alloy component
const tree = render(result);
await writeOutputDirectory(tree, context.emitterOutputDir);
} else {
await emitter.emitFunction(context);
}
} catch (error: unknown) {
throw new ExternalError({ kind: "emitter", metadata: emitter.metadata, error });
}
}

async function writeOutputDirectory(dir: OutputDirectory, emitterOutputDir: string) {
for (const sub of dir.contents) {
if (Array.isArray(sub.contents)) {
await writeOutputDirectory(sub as OutputDirectory, emitterOutputDir);
} else {
await emitFile(program, {
content: sub.contents as string,
path: joinPaths(emitterOutputDir, sub.path),
});
}
}
}
async function runValidators() {
runCompilerValidators();
for (const validator of validateCbs) {
19 changes: 18 additions & 1 deletion packages/compiler/src/experimental/typekit/define-kit.ts
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ export type StripGuards<T> = {
: StripGuards<T[K]>;
};

export const TypekitNamespaceSymbol = Symbol("TypekitNamespace");

/**
* Defines an extension to the Typekit interface.
*
@@ -43,6 +45,21 @@ export function defineKit<T extends Record<string, any>>(
source: StripGuards<T> & ThisType<Typekit>,
): void {
for (const [name, fnOrNs] of Object.entries(source)) {
TypekitPrototype[name] = fnOrNs;
let kits = fnOrNs;

if (TypekitPrototype[name] !== undefined) {
kits = { ...TypekitPrototype[name], ...fnOrNs };
}

// Tag top-level namespace objects with the symbol
if (typeof kits === "object" && kits !== null) {
Object.defineProperty(kits, TypekitNamespaceSymbol, {
value: true,
enumerable: false, // Keep the symbol non-enumerable
configurable: false,
});
}

TypekitPrototype[name] = kits;
}
}
13 changes: 10 additions & 3 deletions packages/compiler/src/experimental/typekit/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { compilerAssert, Program } from "../../core/index.js";
import { Realm } from "../realm.js";
import { Typekit, TypekitPrototype } from "./define-kit.js";
import { Typekit, TypekitNamespaceSymbol, TypekitPrototype } from "./define-kit.js";

export * from "./define-kit.js";
export * from "./kits/index.js";
@@ -32,14 +32,16 @@ export function createTypekit(realm: Realm): Typekit {

const value = Reflect.get(target, prop, receiver);

// Wrap functions to set `this` correctly
if (typeof value === "function") {
return function (this: any, ...args: any[]) {
return value.apply(proxy, args);
};
}

if (typeof value === "object" && value !== null) {
return new Proxy(value, handler);
// Only wrap objects marked as Typekit namespaces
if (typeof value === "object" && value !== null && isTypekitNamespace(value)) {
return new Proxy(value, handler); // Wrap namespace objects
}

return value;
@@ -50,6 +52,11 @@ export function createTypekit(realm: Realm): Typekit {
return proxy;
}

// Helper function to check if an object is a Typekit namespace
function isTypekitNamespace(obj: any): boolean {
return obj && !!obj[TypekitNamespaceSymbol];
}

// #region Default Typekit

const CURRENT_PROGRAM = Symbol.for("TypeSpec.Typekit.CURRENT_PROGRAM");
43 changes: 43 additions & 0 deletions packages/compiler/src/experimental/typekit/kits/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { isArrayModelType } from "../../../core/type-utils.js";
import { Model, Type } from "../../../core/types.js";
import { defineKit } from "../define-kit.js";

export interface ArrayKit {
is(type: Type): boolean;
getElementType(type: Model): Type;
create(elementType: Type): Model;
}

interface TypekitExtension {
array: ArrayKit;
}

declare module "../define-kit.js" {
interface Typekit extends TypekitExtension {}
}

defineKit<TypekitExtension>({
array: {
is(type) {
return (
type.kind === "Model" && isArrayModelType(this.program, type) && type.properties.size === 0
);
},
getElementType(type) {
if (!this.array.is(type)) {
throw new Error("Type is not an array.");
}
return type.indexer!.value;
},
create(elementType) {
return this.model.create({
name: "Array",
properties: {},
indexer: {
key: this.builtin.integer,
value: elementType,
},
});
},
},
});
Loading
Loading