-
Notifications
You must be signed in to change notification settings - Fork 11
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
Initial commit of alloy-js/csharp #19
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains 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,4 @@ | ||
module.exports = { | ||
sourceMaps: true, | ||
presets: ["@babel/preset-typescript", "babel-preset-alloy"], | ||
}; |
This file contains 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,49 @@ | ||
{ | ||
"name": "@alloy-js/csharp", | ||
"version": "0.1.0", | ||
"description": "", | ||
"exports": { | ||
".": { | ||
"development": "./src/index.ts", | ||
"import": "./dist/src/index.js" | ||
}, | ||
"./stc": { | ||
"development": "./src/components/stc/index.ts", | ||
"import": "./dist/src/components/stc/index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"build-src": "babel src -d dist/src --extensions .ts,.tsx", | ||
"build-tsc": "tsc -p .", | ||
"build": "npm run build-src && npm run build-tsc", | ||
"clean": "rimraf dist/ .temp/", | ||
"format": "pnpm run prettier-exec --write", | ||
"prettier-exec": "prettier --config ../../.prettierrc.yaml **/*.{ts,js,tsx,json,yml,yaml}", | ||
"watch-src": "babel src -d dist/src --extensions .ts,.tsx --watch", | ||
"watch-tsc": "tsc -p . --watch", | ||
"watch": "concurrently --kill-others \"npm run watch-src\" \"npm run watch-tsc\"", | ||
"test": "vitest run" | ||
}, | ||
"keywords": [], | ||
"author": "[email protected]", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@alloy-js/core": "workspace:~", | ||
"change-case": "catalog:", | ||
"pathe": "catalog:" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "catalog:", | ||
"@babel/preset-typescript": "catalog:", | ||
"@rollup/plugin-babel": "catalog:", | ||
"@rollup/plugin-typescript": "catalog:", | ||
"babel-preset-alloy": "workspace:~", | ||
"concurrently": "catalog:", | ||
"typescript": "catalog:", | ||
"vitest": "catalog:" | ||
}, | ||
"overrides": { | ||
"esbuild": "0.23" | ||
}, | ||
"type": "module" | ||
} |
This file contains 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,127 @@ | ||
import * as core from "@alloy-js/core"; | ||
import * as csharp from "../index.js"; | ||
import * as symbols from "../symbols/index.js"; | ||
import * as base from "./index.js"; | ||
|
||
// properties for creating a class | ||
export interface ClassProps extends Omit<base.DeclarationProps, "nameKind"> { | ||
accessModifier?: csharp.AccessModifier; | ||
} | ||
|
||
// a C# class declaration | ||
export function Class(props: ClassProps) { | ||
const name = csharp.useCSharpNamePolicy().getName(props.name, "class"); | ||
const scope = symbols.useCSharpScope(); | ||
|
||
const thisClassSymbol = scope.binder.createSymbol<symbols.CSharpOutputSymbol>( | ||
{ | ||
name: name, | ||
scope, | ||
refkey: props.refkey ?? core.refkey(props.name), | ||
}, | ||
); | ||
|
||
// this creates a new scope for the class definition. | ||
// members will automatically "inherit" this scope so | ||
// that refkeys to them will produce the fully-qualified | ||
// name e.g. Foo.Bar. | ||
const thisClassScope = symbols.createCSharpMemberScope( | ||
scope.binder, | ||
scope, | ||
thisClassSymbol, | ||
"class", | ||
); | ||
|
||
return <core.Declaration symbol={thisClassSymbol}> | ||
{csharp.getAccessModifier(props.accessModifier)}class <base.Name />{!props.children && ";"}{props.children && | ||
<> | ||
{"\n{"} | ||
<core.Scope value={thisClassScope}> | ||
{props.children} | ||
</core.Scope> | ||
{"}"} | ||
</> | ||
} | ||
</core.Declaration>; | ||
} | ||
|
||
// properties for creating a class member | ||
export interface ClassMemberProps { | ||
accessModifier?: csharp.AccessModifier; | ||
name: string; | ||
type: core.Children; | ||
refkey?: core.Refkey; | ||
} | ||
|
||
// a C# class member (i.e. a field within a class like "private int count") | ||
export function ClassMember(props: ClassMemberProps) { | ||
const name = csharp.useCSharpNamePolicy().getName(props.name, "class-member"); | ||
const scope = symbols.useCSharpScope(); | ||
if (scope.kind !== "member" || scope.name !== "class") { | ||
throw new Error("can't define a class member outside of a class scope"); | ||
} | ||
|
||
const memberSymbol = scope.binder.createSymbol<symbols.CSharpOutputSymbol>({ | ||
name: name, | ||
scope, | ||
refkey: props.refkey ?? core.refkey(props.name), | ||
}); | ||
|
||
return <core.Declaration symbol={memberSymbol}> | ||
{csharp.getAccessModifier(props.accessModifier)}{props.type} <base.Name />; | ||
</core.Declaration>; | ||
} | ||
|
||
// properties for creating a method | ||
export interface ClassMethodProps | ||
extends Omit<base.DeclarationProps, "nameKind"> { | ||
accessModifier?: csharp.AccessModifier; | ||
methodModifier?: csharp.MethodModifier; | ||
parameters?: Record<string, core.Children>; | ||
returns?: core.Children; | ||
} | ||
|
||
// a C# class method | ||
export function ClassMethod(props: ClassMethodProps) { | ||
const name = csharp.useCSharpNamePolicy().getName(props.name, "class-method"); | ||
const scope = symbols.useCSharpScope(); | ||
if (scope.kind !== "member" || scope.name !== "class") { | ||
throw new Error("can't define a class method outside of a class scope"); | ||
} | ||
|
||
const methodSymbol = scope.binder.createSymbol<symbols.CSharpOutputSymbol>({ | ||
name: name, | ||
scope, | ||
refkey: props.refkey ?? core.refkey(props.name), | ||
}); | ||
|
||
// this creates a new scope for the class method. | ||
// members will automatically "inherit" this scope so | ||
// that refkeys to them will produce the fully-qualified | ||
// name e.g. Foo.Bar. | ||
const methodScope = symbols.createCSharpMemberScope( | ||
scope.binder, | ||
scope, | ||
methodSymbol, | ||
"method", | ||
); | ||
|
||
const accessModifier = csharp.getAccessModifier(props.accessModifier); | ||
const methodModifier = csharp.getMethodModifier(props.methodModifier); | ||
const params = props.parameters ? | ||
<base.Parameters parameters={props.parameters} /> | ||
: ""; | ||
const returns = props.returns ?? "void"; | ||
|
||
return <core.Declaration symbol={methodSymbol}> | ||
{accessModifier}{methodModifier}{returns} <base.Name />({params}){!props.children && " {}"}{props.children && | ||
<> | ||
{"\n{"} | ||
<core.Scope value={methodScope}> | ||
{props.children} | ||
</core.Scope> | ||
{"}"} | ||
</> | ||
} | ||
</core.Declaration>; | ||
} |
This file contains 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,17 @@ | ||
import * as core from "@alloy-js/core"; | ||
import * as symbols from "../symbols/index.js"; | ||
|
||
// properties for creating a declaration | ||
export interface DeclarationProps { | ||
name: string; | ||
refkey?: core.Refkey; | ||
children?: core.Children; | ||
} | ||
|
||
// declares a symbol in the program (class, enum, interface etc) | ||
export function Declaration(props: DeclarationProps) { | ||
const sym = symbols.createCSharpSymbol(props); | ||
return <core.Declaration symbol={sym}> | ||
{props.children} | ||
</core.Declaration>; | ||
} |
This file contains 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 * as core from "@alloy-js/core"; | ||
import * as csharp from "../index.js"; | ||
import * as symbols from "../symbols/index.js"; | ||
import * as base from "./index.js"; | ||
|
||
// properties for creating an enum | ||
export interface EnumProps extends Omit<base.DeclarationProps, "nameKind"> { | ||
accessModifier?: csharp.AccessModifier; | ||
} | ||
|
||
// a C# enum declaration | ||
export function Enum(props: EnumProps) { | ||
const name = csharp.useCSharpNamePolicy().getName(props.name, "enum"); | ||
const scope = symbols.useCSharpScope(); | ||
|
||
const thisEnumSymbol = scope.binder.createSymbol<symbols.CSharpOutputSymbol>({ | ||
name: name, | ||
scope, | ||
refkey: props.refkey ?? core.refkey(props.name), | ||
}); | ||
|
||
// this creates a new scope for the enum definition. | ||
// members will automatically "inherit" this scope so | ||
// that refkeys to them will produce the fully-qualified | ||
// name e.g. Foo.Bar. | ||
const thisEnumScope = symbols.createCSharpMemberScope( | ||
scope.binder, | ||
scope, | ||
thisEnumSymbol, | ||
"enum", | ||
); | ||
|
||
return <core.Declaration symbol={thisEnumSymbol}> | ||
{csharp.getAccessModifier(props.accessModifier)}enum <base.Name />{!props.children && ";"}{props.children && | ||
<> | ||
{"\n{"} | ||
<core.Scope value={thisEnumScope}> | ||
{props.children} | ||
</core.Scope> | ||
{"}"} | ||
</> | ||
} | ||
</core.Declaration>; | ||
} | ||
|
||
// properties for creating a C# enum member | ||
export interface EnumMemberProps { | ||
name: string; | ||
refkey?: core.Refkey; | ||
} | ||
|
||
// a member within a C# enum | ||
export function EnumMember(props: EnumMemberProps) { | ||
const scope = symbols.useCSharpScope(); | ||
if (scope.kind === "member" && scope.name !== "enum") { | ||
throw new Error("can't define an enum member outside of an enum scope"); | ||
} | ||
const name = csharp.useCSharpNamePolicy().getName(props.name, "enum-member"); | ||
|
||
return <base.Declaration name={name} refkey={props.refkey}> | ||
<base.Name /> | ||
</base.Declaration>; | ||
} |
This file contains 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,11 @@ | ||
import * as core from "@alloy-js/core"; | ||
|
||
// the name within the current declaration | ||
export function Name() { | ||
const declSymbol = core.useContext(core.DeclarationContext); | ||
if (!declSymbol) { | ||
throw new Error("missing declaration context"); | ||
} | ||
|
||
return <>{declSymbol.name}</>; | ||
} |
This file contains 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,39 @@ | ||
import * as core from "@alloy-js/core"; | ||
import * as symbols from "../symbols/index.js"; | ||
|
||
// contains the info for the current namespace | ||
export interface NamespaceContext { | ||
name: string; | ||
} | ||
|
||
const NamespaceContext = core.createContext<NamespaceContext>(); | ||
|
||
// returns the current namespace | ||
export function useNamespace(): NamespaceContext | undefined { | ||
return core.useContext(NamespaceContext) as NamespaceContext; | ||
} | ||
|
||
// properties for creating a C# namespace | ||
export interface NamespaceProps { | ||
name: string; | ||
children?: core.Children; | ||
} | ||
|
||
// a C# namespace. contains one or more source files | ||
export function Namespace(props: NamespaceProps) { | ||
const scope = symbols.createCSharpNamespaceScope( | ||
core.useBinder(), | ||
core.useScope(), | ||
props.name, | ||
); | ||
|
||
const namespaceCtx: NamespaceContext = { | ||
name: props.name, | ||
}; | ||
|
||
return <NamespaceContext.Provider value={namespaceCtx}> | ||
<core.Scope value={scope}> | ||
{props.children} | ||
</core.Scope> | ||
</NamespaceContext.Provider>; | ||
} |
This file contains 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,22 @@ | ||
import * as core from "@alloy-js/core"; | ||
import * as csharp from "../index.js"; | ||
|
||
export interface ParametersProps { | ||
// param name and type | ||
parameters: Record<string, core.Child>; | ||
} | ||
|
||
// param names and type | ||
export function Parameters(props: ParametersProps): Array<core.Child | string> { | ||
return core.mapJoin( | ||
new Map(Object.entries(props.parameters)), | ||
(name, type) => { | ||
return [ | ||
type, | ||
" ", | ||
csharp.useCSharpNamePolicy().getName(name, "parameter"), | ||
]; | ||
}, | ||
{ joiner: ", " }, | ||
); | ||
} |
This file contains 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,32 @@ | ||
import * as core from "@alloy-js/core"; | ||
|
||
export interface ProjectDirectoryProps { | ||
name: string; | ||
version: string; | ||
description: string; | ||
srcDir?: string; | ||
children?: core.Children; | ||
} | ||
|
||
// the top-level C# project directory. includes a csproj file | ||
export function ProjectDirectory(props: ProjectDirectoryProps) { | ||
if (!props.srcDir) { | ||
props.srcDir = "src"; | ||
} | ||
|
||
return <> | ||
<core.SourceFile path={props.name+".csproj"} filetype="xml"> | ||
{core.code` | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<Version>${props.version}</Version> | ||
<Description>${props.description}</Description> | ||
</PropertyGroup> | ||
</Project> | ||
`} | ||
</core.SourceFile> | ||
<core.SourceDirectory path={props.srcDir}> | ||
{props.children} | ||
</core.SourceDirectory> | ||
</>; | ||
} |
This file contains 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,13 @@ | ||
import * as core from "@alloy-js/core"; | ||
import * as symbols from "../symbols/index.js"; | ||
|
||
export interface ReferenceProps { | ||
refkey: core.Refkey; | ||
} | ||
|
||
// used to resolve refkey references within source files | ||
export function Reference({ refkey }: ReferenceProps) { | ||
const reference = symbols.ref(refkey); | ||
|
||
return <>{reference}</>; | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should be able to include just an empty line? If not, that's a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also consider a component which adds the braces for you, slightly more characters than {"{"} but less annoying to type maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line does work.
What's the idiom here? Having a blank line looks weird to me but if that's what folks do then I'll follow suit.