Skip to content

Commit

Permalink
Format.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Feb 5, 2025
1 parent 8b78fcb commit b0f02fc
Show file tree
Hide file tree
Showing 19 changed files with 441 additions and 331 deletions.
1 change: 0 additions & 1 deletion packages/emitter-framework/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export interface ArrayExpressionProps {
}

export function ArrayExpression({ elementType }: ArrayExpressionProps) {

return (
<>
Array{"<"}<TypeExpression type={elementType} />{">"}
Array{"<"}
<TypeExpression type={elementType} />
{">"}
</>
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { refkey as getRefkey } from "@alloy-js/core";
import * as ts from "@alloy-js/typescript";
import { Operation } from "@typespec/compiler";
import {refkey as getRefkey} from "@alloy-js/core"
import { TypeExpression } from "./type-expression.jsx";
import { buildParameterDescriptors, getReturnType } from "../utils/operation.js";
import { TypeExpression } from "./type-expression.jsx";

export interface ClassMethodPropsWithType extends Omit<ts.ClassMethodProps, "name"> {
type: Operation;
Expand All @@ -19,19 +19,22 @@ export function ClassMethod(props: ClassMethodProps) {
const refkey = props.refkey ?? getRefkey(props.type, "method");

const name = props.name ? props.name : ts.useTSNamePolicy().getName(props.type.name, "function");
const returnType = props.returnType === null ? undefined : <TypeExpression type={getReturnType(props.type)} />;
const returnType =
props.returnType === null ? undefined : <TypeExpression type={getReturnType(props.type)} />;

return <ts.ClassMethod
refkey={refkey}
name={name}
async={props.async}
returnType={returnType}
parameters={buildParameterDescriptors(props.type.parameters)}
>
{props.children}
</ts.ClassMethod>
return (
<ts.ClassMethod
refkey={refkey}
name={name}
async={props.async}
returnType={returnType}
parameters={buildParameterDescriptors(props.type.parameters)}
>
{props.children}
</ts.ClassMethod>
);
}

function isTypedMethodDeclarationProps(props: ClassMethodProps): props is ClassMethodPropsWithType {
return (props as ClassMethodPropsWithType).type !== undefined;
}
function isTypedMethodDeclarationProps(props: ClassMethodProps): props is ClassMethodPropsWithType {
return (props as ClassMethodPropsWithType).type !== undefined;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { mapJoin, Refkey, refkey } from "@alloy-js/core";
import * as ts from "@alloy-js/typescript";
import { Enum, EnumMember as TspEnumMember, Union } from "@typespec/compiler";
import { $ } from "@typespec/compiler/experimental/typekit";
import * as ts from "@alloy-js/typescript";
import { mapJoin, Refkey, refkey } from "@alloy-js/core";
import { reportDiagnostic } from "../../lib.js";

export interface EnumDeclarationProps extends Omit<ts.TypeDeclarationProps, "name">{
export interface EnumDeclarationProps extends Omit<ts.TypeDeclarationProps, "name"> {
name?: string;
type: Union | Enum;
}
Expand All @@ -20,29 +20,35 @@ export function EnumDeclaration(props: EnumDeclarationProps) {
type = props.type;
}

const members = mapJoin(type.members, (key, value) => {
return <EnumMember
type={value}
refkey={$.union.is(props.type)
? refkey(props.type.variants.get(key))
: refkey(value)
} />
}, { joiner: ",\n" });

if(!props.type.name || props.type.name === "") {
reportDiagnostic($.program, {code: "type-declaration-missing-name", target: props.type});
const members = mapJoin(
type.members,
(key, value) => {
return (
<EnumMember
type={value}
refkey={$.union.is(props.type) ? refkey(props.type.variants.get(key)) : refkey(value)}
/>
);
},
{ joiner: ",\n" },
);

if (!props.type.name || props.type.name === "") {
reportDiagnostic($.program, { code: "type-declaration-missing-name", target: props.type });
}

const name = props.name ?? ts.useTSNamePolicy().getName(props.type.name!, "enum");

return <ts.EnumDeclaration
name={name}
refkey={refkey(props.type)}
default={props.default}
export={props.export}
>
{ members }
</ts.EnumDeclaration>
return (
<ts.EnumDeclaration
name={name}
refkey={refkey(props.type)}
default={props.default}
export={props.export}
>
{members}
</ts.EnumDeclaration>
);
}

export interface EnumMemberProps {
Expand All @@ -51,9 +57,11 @@ export interface EnumMemberProps {
}

export function EnumMember(props: EnumMemberProps) {
return <ts.EnumMember
name={props.type.name}
jsValue={props.type.value ?? props.type.name}
refkey={refkey(props.refkey ?? props.type)}
/>
return (
<ts.EnumMember
name={props.type.name}
jsValue={props.type.value ?? props.type.name}
refkey={refkey(props.refkey ?? props.type)}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Children, code, refkey as getRefkey, mapJoin } from "@alloy-js/core";
import * as ts from "@alloy-js/typescript";
import { Interface, Model, ModelProperty, Operation, RekeyableMap } from "@typespec/compiler";
import {createRekeyableMap} from "@typespec/compiler/utils"
import { Interface, Model, ModelProperty, Operation, RekeyableMap } from "@typespec/compiler";
import { $ } from "@typespec/compiler/experimental/typekit";
import { createRekeyableMap } from "@typespec/compiler/utils";
import { reportDiagnostic } from "../../lib.js";
import { InterfaceMember } from "./interface-member.js";
import { TypeExpression } from "./type-expression.jsx";
import { reportDiagnostic } from "../../lib.js";
export interface TypedInterfaceDeclarationProps extends Omit<ts.InterfaceDeclarationProps, "name"> {
type: Model | Interface;
name?: string;
Expand All @@ -24,11 +24,10 @@ export function InterfaceDeclaration(props: InterfaceDeclarationProps) {

let name = props.name ?? props.type.name;

if(!name || name === "") {
reportDiagnostic($.program, {code: "type-declaration-missing-name", target: props.type});
if (!name || name === "") {
reportDiagnostic($.program, { code: "type-declaration-missing-name", target: props.type });
}


name = namePolicy.getName(name, "interface");

const refkey = props.refkey ?? getRefkey(props.type);
Expand All @@ -39,7 +38,7 @@ export function InterfaceDeclaration(props: InterfaceDeclarationProps) {

const children = [...members];

if(Array.isArray(props.children)) {
if (Array.isArray(props.children)) {
children.push(...props.children);
} else if (props.children) {
children.push(props.children);
Expand All @@ -60,7 +59,7 @@ export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
}

function isTypedInterfaceDeclarationProps(
props: InterfaceDeclarationProps
props: InterfaceDeclarationProps,
): props is TypedInterfaceDeclarationProps {
return "type" in props;
}
Expand All @@ -83,7 +82,7 @@ export function InterfaceExpression({ type, children }: InterfaceExpressionProps
}

function getExtendsType(type: Model | Interface): Children | undefined {
if(!$.model.is(type)) {
if (!$.model.is(type)) {
return undefined;
}

Expand All @@ -92,39 +91,39 @@ function getExtendsType(type: Model | Interface): Children | undefined {
const recordExtends = code`Record<string, unknown>`;

if (type.baseModel) {
if($.array.is(type.baseModel)) {
extending.push(<TypeExpression type={type.baseModel} />)
} else if($.record.is(type.baseModel)){
if ($.array.is(type.baseModel)) {
extending.push(<TypeExpression type={type.baseModel} />);
} else if ($.record.is(type.baseModel)) {
extending.push(recordExtends);
// When extending a record we need to override the element type to be unknown to avoid type errors
} else {
extending.push(getRefkey(type.baseModel))
extending.push(getRefkey(type.baseModel));
}
}

const spreadType = $.model.getSpreadType(type);
if(spreadType) {
if (spreadType) {
// When extending a record we need to override the element type to be unknown to avoid type errors
if($.record.is(spreadType)) {
if ($.record.is(spreadType)) {
extending.push(recordExtends);
} else {
extending.push(<TypeExpression type={spreadType} />);
}
}

if(extending.length === 0) {
if (extending.length === 0) {
return undefined;
}

return mapJoin(extending, (ext) => ext, {joiner: ","} );
return mapJoin(extending, (ext) => ext, { joiner: "," });
}

function membersFromType(type: Model | Interface) {
let typeMembers: RekeyableMap<string, ModelProperty | Operation> | undefined;
if ($.model.is(type)) {
typeMembers = $.model.getProperties(type);
} else {
typeMembers = createRekeyableMap(type.operations)
typeMembers = createRekeyableMap(type.operations);
}

return mapJoin(typeMembers, (_, prop) => <InterfaceMember type={prop} />, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { useTSNamePolicy } from "@alloy-js/typescript";
import { isNeverType, ModelProperty, Operation } from "@typespec/compiler";
import { FunctionDeclaration } from "./function-declaration.js";
import { TypeExpression } from "./type-expression.js";
import { $ } from "@typespec/compiler/experimental/typekit";
import { getHttpPart } from "@typespec/http";
import { FunctionDeclaration } from "./function-declaration.js";
import { TypeExpression } from "./type-expression.js";

export interface InterfaceMemberProps {
type: ModelProperty | Operation;
optional?: boolean;
}

export function InterfaceMember({ type, optional }: InterfaceMemberProps) {
export function InterfaceMember({ type, optional }: InterfaceMemberProps) {
const namer = useTSNamePolicy();
const name = namer.getName(type.name, "object-member-getter")
const name = namer.getName(type.name, "object-member-getter");

if ($.modelProperty.is(type)) {
const optionality = type.optional ?? optional ? "?" : "";
const optionality = (type.optional ?? optional) ? "?" : "";

if(isNeverType(type.type)) {
if (isNeverType(type.type)) {
return null;
}

let unpackedType = type.type;
const part = getHttpPart($.program, type.type);
if(part) {
if (part) {
unpackedType = part.type;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { code } from "@alloy-js/core";
import { Type } from "@typespec/compiler";
import { TypeExpression } from "./type-expression.js";
import { code } from "@alloy-js/core";

export interface RecordExpressionProps {
elementType: Type;
}

export function RecordExpression({ elementType }: RecordExpressionProps) {
return code`
Record<string, ${<TypeExpression type={elementType} />}>
`
Record<string, ${(<TypeExpression type={elementType} />)}>
`;
}
Loading

0 comments on commit b0f02fc

Please sign in to comment.