-
Notifications
You must be signed in to change notification settings - Fork 27
TypeSpec SourceFile and Namespace components #324
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
Draft
jpalvarezl
wants to merge
33
commits into
alloy-framework:feature/typespec
Choose a base branch
from
glecaros:jpalvarezl/base_context
base: feature/typespec
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 26 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
dbf58c5
Adding empty project for the TypeSpec library.
glecaros c440fb7
Ported name over to this branch
jpalvarezl d1e29f3
Added import statements
jpalvarezl c7b15e5
Added a couple of clarifying remarks
jpalvarezl fe648d9
Added source-file.tsx
jpalvarezl 0ec982e
Added all the entities in #1
jpalvarezl f016b14
Added missing imports
jpalvarezl f4b1e0a
Added model-property too
jpalvarezl 54b2498
Added regex validation for some element names in policy
jpalvarezl df9cbc4
Adding scopes, symbols and context
jpalvarezl a9ea352
PR feedback and disabling modelProperty
jpalvarezl 5948676
added namespace-scope for components and moved tests
jpalvarezl 260703d
focusing on namespace.test.tsx at least now the scope is not undefined
jpalvarezl f5221a1
Added missing JSX
jpalvarezl e44cf8e
Zoning in the problem
jpalvarezl 321666b
Going down the source-file rabbit hole
jpalvarezl 5e9fe05
Added TestNamespace util
jpalvarezl b657164
WIP
jpalvarezl daf16a2
WIP
jpalvarezl c0639b8
Fixed compilation
jpalvarezl 4f80830
Simplified source just to see if I am able to render something
jpalvarezl e184200
Add directory.
glecaros bf121bf
asdf
glecaros 74c06fd
wip
glecaros a972a51
Merge branch 'feature/typespec' into jpalvarezl/base_context
glecaros e3b6d51
namespaces!
glecaros 15839e4
PR feedback and cleanup
jpalvarezl 55a5e05
Removed more unnecessary code at this point
jpalvarezl db7d4ca
WIP: getting namespace as props not working yet
jpalvarezl 8421dee
WIP: still failing but now both failing tests create the namespacesym…
jpalvarezl b41eada
fixed one more test
jpalvarezl a9929d4
Consolidated namespaces under single component type
jpalvarezl 6390c62
Removed setting of removed flagged
jpalvarezl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import * as core from "@alloy-js/core"; | ||
| import { ref } from "../symbols/reference.js"; | ||
|
|
||
| export interface ReferenceProps { | ||
| refkey: core.Refkey; | ||
| } | ||
|
|
||
| // used to resolve refkey references within source files | ||
| export function Reference({ refkey }: ReferenceProps) { | ||
| const reference = ref(refkey); | ||
| const symbolRef = core.computed(() => reference()[1]); | ||
|
|
||
| core.emitSymbol(symbolRef); | ||
| return <>{reference()[0]}</>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface AliasProps {}; | ||
|
|
||
| export function Alias(props: AliasProps) { | ||
| return <></>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface DecoratorProps {}; | ||
|
|
||
| export function Decorator(props: DecoratorProps) { | ||
| return <></>; | ||
| } |
19 changes: 19 additions & 0 deletions
19
packages/typespec/src/components/directory/directory.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { expect, it } from "vitest"; | ||
| import { Directory } from "./directory.jsx"; | ||
| import { Output, SourceFile } from "@alloy-js/core"; | ||
|
|
||
| it("defines multiple directories with unique source files", () => { | ||
| expect( | ||
| <Output> | ||
| <Directory path="dir1"> | ||
| <SourceFile path="file.tsp" filetype="tsp">Content of File1</SourceFile> | ||
| </Directory> | ||
| <Directory path="dir2"> | ||
| <SourceFile path="file.tsp" filetype="tsp">Content of File2</SourceFile> | ||
| </Directory> | ||
| </Output>, | ||
| ).toRenderTo({ | ||
| "dir1/file.tsp": `Content of File1`, | ||
| "dir2/file.tsp": `Content of File2`, | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Children, Scope, SourceDirectory } from "@alloy-js/core"; | ||
| import { createDirectoryScope } from "../../symbols/factories.js"; | ||
|
|
||
| export interface DirectoryProps { | ||
| path: string; | ||
| children?: Children; | ||
| }; | ||
|
|
||
| export function Directory(props: DirectoryProps) { | ||
| const scope = createDirectoryScope(props.path); | ||
| return ( | ||
| <SourceDirectory path={props.path}> | ||
| <Scope value={scope}> | ||
| {props.children} | ||
| </Scope> | ||
| </SourceDirectory> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface EnumProps {}; | ||
|
|
||
| export function Enum(props: EnumProps) { | ||
| return <></>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface ImportProps {}; | ||
|
|
||
| export function Import(props: ImportProps) { | ||
| return <></>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export * from "./alias.jsx"; | ||
| export * from "./decorator.jsx"; | ||
| export * from "./enum.jsx"; | ||
| export * from "./import.jsx"; | ||
| export * from "./interface.jsx"; | ||
| export * from "./model-property.jsx"; | ||
| export * from "./model.jsx"; | ||
| export * from "./namespace.jsx"; | ||
| export * from "./operation.jsx" | ||
| export * from "./scalar.jsx"; | ||
| export * from "./directory/directory.jsx" | ||
| export * from "./source-file/source-file.jsx"; | ||
| export * from "./union.jsx"; | ||
| export * from "./value.jsx"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface InterfaceProps {}; | ||
|
|
||
| export function Interface(props: InterfaceProps) { | ||
| return <></>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Children, createNamedContext, Declaration, Name } from "@alloy-js/core"; | ||
| import { createPropertySymbol } from "../symbols/factories.js"; | ||
|
|
||
| export interface ModelPropertyProps { | ||
| name: string; | ||
| doc?: Children; | ||
| }; | ||
|
|
||
| export function ModelProperty(props: ModelPropertyProps) { | ||
| // const symbol = createPropertySymbol(props.name); | ||
|
|
||
| // return ( | ||
| // <Declaration symbol={symbol}> | ||
| // <Name /> | ||
| // </Declaration> | ||
| // ); | ||
| return <></>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface ModelProps {}; | ||
|
|
||
| export function Model(props: ModelProps) { | ||
| return <></>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { expect, it } from "vitest"; | ||
| import { Output } from "@alloy-js/core"; | ||
| import { Directory } from "#components/directory/directory.jsx"; | ||
| import { SourceFile } from "#components/source-file/source-file.jsx"; | ||
| import { Namespace } from "./block.jsx"; | ||
| import { d } from "@alloy-js/core/testing"; | ||
|
|
||
| it("renders a namespace with contents", () => { | ||
| expect( | ||
| <Output> | ||
| <SourceFile path="main.tsp"> | ||
| <Namespace name="My.Namespace"> | ||
| Contents! | ||
| </Namespace> | ||
| </SourceFile> | ||
| </Output>, | ||
| ).toRenderTo({ | ||
| "main.tsp": d` | ||
| namespace My.Namespace { | ||
| Contents! | ||
| }`, | ||
| }); | ||
| }); | ||
|
|
||
| it("renders nested block namespaces", () => { | ||
| expect( | ||
| <Output> | ||
| <SourceFile path="main.tsp"> | ||
| <Namespace name="My.Namespace"> | ||
| <Namespace name="Inner.Namespace"> | ||
| Inner Contents! | ||
| </Namespace> | ||
| </Namespace> | ||
| </SourceFile> | ||
| </Output>, | ||
| ).toRenderTo({ | ||
| "main.tsp": d` | ||
| namespace My.Namespace { | ||
| namespace Inner.Namespace { | ||
| Inner Contents! | ||
| } | ||
| }`, | ||
| }); | ||
| }); | ||
|
|
||
| it("renders namespaces when a file level namespace is present", () => { | ||
| expect( | ||
| <Output> | ||
| <SourceFile path="main.tsp" namespace="File.Level"> | ||
| <Namespace name="My.Namespace"> | ||
| Contents! | ||
| </Namespace> | ||
| </SourceFile> | ||
| </Output>, | ||
| ).toRenderTo({ | ||
| "main.tsp": d` | ||
| namespace File.Level; | ||
|
|
||
| namespace My.Namespace { | ||
| Contents! | ||
| }`, | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Block, Children, Namekey, Refkey, Scope, useScope } from "@alloy-js/core"; | ||
| import { NamespaceScope, useNamespace } from "../../scopes/namespace.js"; | ||
| import { createNamespaceSymbol } from "../../symbols/factories.js"; | ||
| import { Declaration } from "../../../../core/src/index.browser.js"; | ||
|
|
||
| export interface NamespaceProps { | ||
| name: string | Namekey | (string | Namekey)[]; | ||
| refkey?: Refkey | Refkey[]; | ||
| children?: Children; | ||
| } | ||
|
|
||
| export function Namespace(props: NamespaceProps) { | ||
| const parent = useNamespace(); | ||
|
|
||
| if (parent === undefined) { | ||
| throw new Error("Block namespace must be declared within another namespace."); | ||
| } | ||
|
|
||
| const symbol = createNamespaceSymbol(props.name, { | ||
| refkeys: props.refkey, | ||
| }); | ||
|
|
||
| const scope = new NamespaceScope(symbol, parent); | ||
| const name = symbol.getScopedName(parent.ownerSymbol); | ||
|
|
||
| return ( | ||
| <Scope value={scope}> | ||
| <Declaration symbol={symbol}> | ||
| namespace {name} <Block> | ||
| {props.children} | ||
| </Block> | ||
| </Declaration> | ||
| </Scope> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { Children, Declaration, Name, Namekey, Refkey, Scope, Show, useBinder } from "@alloy-js/core"; | ||
| import { useSourceFileScope } from "../../scopes/source-file.js"; | ||
| import { getGlobalNamespace } from "../../contexts/global-namespace.js"; | ||
| import { NamespaceSymbol } from "../../symbols/namespace.js"; | ||
| import { createNamespaceSymbol } from "../../symbols/factories.js"; | ||
| import { NamespaceScope } from "../../scopes/namespace.js"; | ||
|
|
||
| export interface FileLevelNamespaceProps { | ||
jpalvarezl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| name?: string | Namekey | (string | Namekey)[]; | ||
| refkey?: Refkey | Refkey[]; | ||
| children?: Children; | ||
| isGlobal?: boolean; | ||
| } | ||
|
|
||
| export function FileLevelNamespace(props: FileLevelNamespaceProps) { | ||
| if (props.name === undefined && !props.isGlobal) { | ||
| throw new Error("File level namespace requires a name or global flag"); | ||
| } | ||
| if (props.name !== undefined && props.isGlobal) { | ||
| throw new Error("File level namespace cannot have both a name and global flag"); | ||
| } | ||
|
|
||
| const parent = useSourceFileScope(); | ||
| if (parent === undefined) { | ||
| throw new Error("File level namespace must be used within a source file scope"); | ||
| } | ||
|
|
||
| let symbol: NamespaceSymbol; | ||
| if (props.name === undefined) { | ||
| symbol = getGlobalNamespace(useBinder()); | ||
| } else { | ||
| symbol = createNamespaceSymbol(props.name, { | ||
| refkeys: props.refkey, | ||
| }); | ||
| } | ||
|
|
||
| const namespaceScope = new NamespaceScope(symbol, parent); | ||
|
|
||
| return ( | ||
| <Scope value={namespaceScope} > | ||
| <Declaration symbol={symbol}> | ||
| <Show when={!props.isGlobal}> | ||
| namespace {symbol.getFullyQualifiedName()}; | ||
| <hbr /> | ||
| <hbr /> | ||
| </Show> | ||
| {props.children} | ||
| </Declaration> | ||
| </Scope> | ||
| ) | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface OperationProps {}; | ||
|
|
||
| export function Operation(props: OperationProps) { | ||
| return <></>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface ScalarProps {}; | ||
|
|
||
| export function Scalar(props: ScalarProps) { | ||
| return <></>; | ||
| } |
33 changes: 33 additions & 0 deletions
33
packages/typespec/src/components/source-file/source-file.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { expect, it } from "vitest"; | ||
| import { Output } from "@alloy-js/core"; | ||
| import { SourceFile } from "./source-file.jsx"; | ||
| import { Directory } from "#components/directory/directory.jsx"; | ||
|
|
||
| it("defines multiple directories with unique source files", () => { | ||
| expect( | ||
| <Output> | ||
| <Directory path="dir1"> | ||
| <SourceFile path="file.tsp">Content of File1</SourceFile> | ||
| </Directory> | ||
| <Directory path="dir2"> | ||
| <SourceFile path="file.tsp">Content of File2</SourceFile> | ||
| </Directory> | ||
| </Output>, | ||
| ).toRenderTo({ | ||
| "dir1/file.tsp": `Content of File1`, | ||
| "dir2/file.tsp": `Content of File2`, | ||
| }); | ||
| }); | ||
|
|
||
| it("declares a file level namespace when one is provided", () => { | ||
| expect( | ||
| <Output> | ||
| <SourceFile namespace="My.Namespace" path="main.tsp"> | ||
| </SourceFile> | ||
| </Output> | ||
| ).toRenderTo({ | ||
| "main.tsp": ` | ||
| namespace My.Namespace; | ||
| `, | ||
| }); | ||
| }); |
52 changes: 52 additions & 0 deletions
52
packages/typespec/src/components/source-file/source-file.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { Children } from "@alloy-js/core/jsx-runtime"; | ||
| import { SourceFileScope } from "../../scopes/source-file.js"; | ||
| import { SourceFile as CoreSourceFile, Scope } from "@alloy-js/core"; | ||
| import { | ||
| useTypeSpecFormatOptions | ||
| } from "../../contexts/format-options.js"; | ||
| import { Reference } from "../Reference.jsx"; | ||
| import { useDirectoryScope } from "../../scopes/contexts.js"; | ||
| import { FileLevelNamespace } from "#components/namespace/file-level.jsx"; | ||
|
|
||
| export interface SourceFileProps { | ||
| path: string; | ||
|
|
||
| /** If present, it defines a file-level namespace (if not present, it uses the global namespace) */ | ||
| namespace?: string; | ||
|
|
||
| children?: Children; | ||
|
|
||
| /** | ||
| * A list of using directives to explicitly include. Note that providing | ||
| * explicit usings is not necessary when referencing symbols via refkeys. | ||
| */ | ||
| using?: string[]; | ||
|
|
||
| /** | ||
| * A list of import directives to explicitly include. Note that providing | ||
| * explicit imports is not necessary when referencing symbols via refkeys. | ||
| */ | ||
| import?: string[]; | ||
| }; | ||
|
|
||
| export function SourceFile(props: SourceFileProps) { | ||
| const parent = useDirectoryScope(); | ||
| const sourceFileScope = new SourceFileScope(props.path, parent); | ||
|
|
||
| const options = useTypeSpecFormatOptions(); | ||
|
|
||
| return ( | ||
| <CoreSourceFile | ||
| path={props.path} | ||
| filetype=".tsp" | ||
jpalvarezl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| reference={Reference} | ||
| {...options} | ||
| > | ||
| <Scope value={sourceFileScope}> | ||
| <FileLevelNamespace name={props.namespace} isGlobal={props.namespace === undefined}> | ||
| {props.children} | ||
| </FileLevelNamespace> | ||
| </Scope> | ||
| </CoreSourceFile> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface UnionProps {}; | ||
|
|
||
| export function Union(props: UnionProps) { | ||
| return <></>; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.