-
Notifications
You must be signed in to change notification settings - Fork 277
[Typekit] - Add builtin.is(type: Type): boolean
#7407
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
||
Introduce builtin.is(type: Type): boolean, which returns true for any type defined in the global TypeSpec namespace (i.e. built-in/standard library types). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import type { Scalar } from "../../core/types.js"; | ||
import type { Namespace, Scalar, Type } from "../../core/types.js"; | ||
import { defineKit } from "../define-kit.js"; | ||
|
||
/** | ||
* A kit of built-in types. | ||
* @typekit builtin | ||
*/ | ||
export interface BuiltinKit { | ||
/** | ||
* Checks if the given type is a built-in type. | ||
* A type is considered built-in if it is defined in the TypeSpec namespace. | ||
*/ | ||
is(type: Type): boolean; | ||
|
||
/** | ||
* Accessor for the string builtin type. | ||
*/ | ||
|
@@ -142,6 +148,36 @@ declare module "../define-kit.js" { | |
|
||
defineKit<TypekitExtension>({ | ||
builtin: { | ||
is(type: Type): boolean { | ||
// Model property does not have a namespace, | ||
joheredi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// so we will use the Model where it is defined. | ||
if (type.kind === "ModelProperty" && type.model) { | ||
type = type.model; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this is worth it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the tests to show when this is helpful namespace TypeSpec {
model InTypeSpec {
foo: string;
}
}
model Foo {
bar: TypeSpec.InTypeSpec.foo;
} Without this $.builtin.is(Foo.bar.type) would return true |
||
} | ||
|
||
// Consider the type not built-in if it has no namespace | ||
// TypeKit might create types without a namespace | ||
const ns = (type as any).namespace as Namespace | undefined; | ||
if (!ns) { | ||
return false; | ||
} | ||
|
||
const globalNs = this.program.getGlobalNamespaceType(); | ||
// If it’s already in the global root, it's not in global.TypeSpec | ||
if (ns === globalNs) { | ||
return false; | ||
} | ||
|
||
// Find the immediate child of `globalNs` in this namespace's ancestry. | ||
const topLevel = getImmediateChildOfGlobal(ns, globalNs); | ||
if (!topLevel) { | ||
return false; | ||
} | ||
|
||
// Finally, compare by identity | ||
const typeSpecNs = globalNs.namespaces.get("TypeSpec"); | ||
return topLevel === typeSpecNs; | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that always working when using mutators? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I seem to recall at some point that we had special-cased TypeSpec so as to avoid mutating built-in types. Is that not the case? If it's not, then I think we might have more bugs in other places, e.g. the intrinsic is checks use reference equality. |
||
get string(): Scalar { | ||
return this.program.checker.getStdType("string"); | ||
}, | ||
|
@@ -219,3 +255,16 @@ defineKit<TypekitExtension>({ | |
}, | ||
}, | ||
}); | ||
|
||
/** | ||
* Walks from `ns` up to `root`, returning the first namespace | ||
* whose parent is `root`. Returns undefined if `root` is not | ||
* actually an ancestor. | ||
*/ | ||
function getImmediateChildOfGlobal(ns: Namespace, root: Namespace): Namespace | undefined { | ||
let current: Namespace | undefined = ns; | ||
while (current.namespace && current.namespace !== root) { | ||
current = current.namespace; | ||
} | ||
return current?.namespace === root ? current : undefined; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.