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

Initial commit of alloy-js/csharp #19

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions packages/csharp/babel.config.cjs
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"],
};
49 changes: 49 additions & 0 deletions packages/csharp/package.json
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"
}
127 changes: 127 additions & 0 deletions packages/csharp/src/components/Class.tsx
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{"}
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

<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>;
}
17 changes: 17 additions & 0 deletions packages/csharp/src/components/Declaration.tsx
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>;
}
63 changes: 63 additions & 0 deletions packages/csharp/src/components/Enum.tsx
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>;
}
11 changes: 11 additions & 0 deletions packages/csharp/src/components/Name.tsx
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}</>;
}
39 changes: 39 additions & 0 deletions packages/csharp/src/components/Namespace.tsx
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>;
}
22 changes: 22 additions & 0 deletions packages/csharp/src/components/Parameters.tsx
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: ", " },
);
}
32 changes: 32 additions & 0 deletions packages/csharp/src/components/ProjectDirectory.tsx
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>
</>;
}
13 changes: 13 additions & 0 deletions packages/csharp/src/components/Reference.tsx
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}</>;
}
Loading
Loading