Skip to content

Commit

Permalink
Fix docs & eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Dec 7, 2024
1 parent f0d2eeb commit 4d900c2
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 56 deletions.
9 changes: 2 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ module.exports = {
ignoreRestSiblings: true
}
],
"@typescript-eslint/no-namespace": [
"error",
{
allowDeclarations: true
}
],
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/explicit-module-boundary-types": [
"error",
{
Expand Down Expand Up @@ -129,7 +124,7 @@ module.exports = {
{
files: ['**/*.test.ts', '**/*.spec.ts'],
rules: {
'no-console': 'off'
'no-console': 'off'
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Package,
PathParameter
} from "@fern-fern/ir-sdk/api";
import { convertHttpPathToExpressRoute, getTextOfTsNode, maybeAddDocs, PackageId } from "@fern-typescript/commons";
import { convertHttpPathToExpressRoute, getTextOfTsNode, maybeAddDocsNode, PackageId } from "@fern-typescript/commons";
import { ExpressContext, GeneratedExpressService } from "@fern-typescript/contexts";
import { ClassDeclaration, InterfaceDeclaration, Scope, ts } from "ts-morph";

Expand Down Expand Up @@ -82,7 +82,7 @@ export class GeneratedExpressServiceImpl implements GeneratedExpressService {
name: this.serviceClassName,
isExported: true
});
maybeAddDocs(serviceClass, this.package_.docs);
maybeAddDocsNode(serviceClass, this.package_.docs);

serviceClass.addProperty({
scope: Scope.Private,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ExampleTypeShape, TypeReference } from "@fern-fern/ir-sdk/api";
import { GetReferenceOpts, getTextOfTsNode, maybeAddDocs, writerToString } from "@fern-typescript/commons";
import {
GetReferenceOpts,
getTextOfTsNode,
maybeAddDocsNode,
maybeAddDocsStructure,
writerToString
} from "@fern-typescript/commons";
import { BaseContext, NotBrandedGeneratedAliasType } from "@fern-typescript/contexts";
import {
ModuleDeclarationStructure,
Expand Down Expand Up @@ -36,7 +42,7 @@ export class GeneratedAliasTypeImpl<Context extends BaseContext>
type: getTextOfTsNode(context.type.getReferenceToType(this.shape).typeNode),
isExported: true
};
maybeAddDocs(typeAlias, this.getDocs(context));
maybeAddDocsStructure(typeAlias, this.getDocs(context));
return typeAlias;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
GetReferenceOpts,
getTextOfTsKeyword,
getTextOfTsNode,
maybeAddDocs,
maybeAddDocsNode,
maybeAddDocsStructure,
writerToString
} from "@fern-typescript/commons";
import { BrandedGeneratedAliasType, BaseContext } from "@fern-typescript/contexts";
Expand Down Expand Up @@ -73,7 +74,7 @@ export class GeneratedBrandedStringAliasImpl<Context extends BaseContext>
),
isExported: true
};
maybeAddDocs(typeAlias, this.getDocs(context));
maybeAddDocsStructure(typeAlias, this.getDocs(context));
return typeAlias;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
GetReferenceOpts,
getTextOfTsNode,
getWriterForMultiLineUnionType,
maybeAddDocs
maybeAddDocsNode,
maybeAddDocsStructure
} from "@fern-typescript/commons";
import { BaseContext, GeneratedEnumType } from "@fern-typescript/contexts";
import {
Expand Down Expand Up @@ -59,7 +60,7 @@ export class GeneratedEnumTypeImpl<Context extends BaseContext>
)
};

maybeAddDocs(type, this.getDocs(context));
maybeAddDocsStructure(type, this.getDocs(context));
return type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
TypeDeclaration,
TypeReference
} from "@fern-fern/ir-sdk/api";
import { GetReferenceOpts, getTextOfTsNode, maybeAddDocs, TypeReferenceNode } from "@fern-typescript/commons";
import {
GetReferenceOpts,
getTextOfTsNode,
maybeAddDocsNode,
maybeAddDocsStructure,
TypeReferenceNode
} from "@fern-typescript/commons";
import { GeneratedObjectType, BaseContext } from "@fern-typescript/contexts";
import {
InterfaceDeclarationStructure,
Expand Down Expand Up @@ -71,9 +77,9 @@ export class GeneratedObjectTypeImpl<Context extends BaseContext>
return this.generatePropertiesInternal(context).map(({ name, type, hasQuestionToken, docs }) => {
const propertyNode: PropertySignatureStructure = {
kind: StructureKind.PropertySignature,
name: name,
name,
type: getTextOfTsNode(type),
hasQuestionToken: hasQuestionToken,
hasQuestionToken,
docs: docs != null ? [{ description: docs }] : undefined
};

Expand Down Expand Up @@ -113,7 +119,7 @@ export class GeneratedObjectTypeImpl<Context extends BaseContext>
isExported: true
};

maybeAddDocs(interfaceNode, this.getDocs(context));
maybeAddDocsStructure(interfaceNode, this.getDocs(context));
const iExtends = [];
for (const extension of this.shape.extends) {
iExtends.push(getTextOfTsNode(context.type.getReferenceToNamedType(extension).getTypeNode()));
Expand All @@ -135,7 +141,9 @@ export class GeneratedObjectTypeImpl<Context extends BaseContext>
this.shape.properties
.map((property): [ObjectProperty, NamedType] | undefined => {
const namedType = getNamedType(property.valueType);
if (namedType) return [property, namedType];
if (namedType) {
return [property, namedType];
}
return undefined;
})
.filter((x): x is [ObjectProperty, NamedType] => x !== undefined)
Expand Down Expand Up @@ -348,6 +356,7 @@ function getNamedType(typeReference: TypeReference): NamedType | undefined {
default:
assertNever(typeReference.container);
}
// fallthrough
case "primitive":
return undefined;
case "unknown":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import {
UndiscriminatedUnionMember,
UndiscriminatedUnionTypeDeclaration
} from "@fern-fern/ir-sdk/api";
import { GetReferenceOpts, getWriterForMultiLineUnionType, maybeAddDocs } from "@fern-typescript/commons";
import {
GetReferenceOpts,
getWriterForMultiLineUnionType,
maybeAddDocsNode,
maybeAddDocsStructure
} from "@fern-typescript/commons";
import { GeneratedUndiscriminatedUnionType, BaseContext } from "@fern-typescript/contexts";
import {
ModuleDeclarationStructure,
Expand Down Expand Up @@ -50,7 +55,7 @@ export class GeneratedUndiscriminatedUnionTypeImpl<Context extends BaseContext>
})
)
};
maybeAddDocs(alias, this.getDocs(context));
maybeAddDocsStructure(alias, this.getDocs(context));
return alias;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export namespace ConvertTypeReferenceParams {
/**
* Metadata for converting inline types
*/
export type InlineType = {
export interface InlineType {
parentTypeName: string;
propertyName: string;
genericIn?: InlineType.GenericIn;
};
}

export namespace InlineType {
export const GenericIn = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
FernWriters,
getTextOfTsNode,
getWriterForMultiLineUnionType,
maybeAddDocs,
maybeAddDocsNode,
maybeAddDocsStructure,
ObjectWriter,
Reference
} from "@fern-typescript/commons";
Expand Down Expand Up @@ -279,9 +280,7 @@ export class GeneratedUnionImpl<Context extends ModelContext> implements Generat
),
isExported: true
};
if (this.getDocs != null) {
maybeAddDocs(typeAlias, this.getDocs(context));
}
maybeAddDocsStructure(typeAlias, this.getDocs?.(context));
return typeAlias;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getTextOfTsNode,
ImportsManager,
JavaScriptRuntime,
maybeAddDocs,
maybeAddDocsStructure,
NpmPackage,
PackageId
} from "@fern-typescript/commons";
Expand Down Expand Up @@ -459,7 +459,7 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {
methods: [],
getAccessors: []
};
maybeAddDocs(serviceClass, this.package_.docs);
maybeAddDocsStructure(serviceClass, this.package_.docs);

if (this.isRoot && context.generateOAuthClients) {
serviceClass.properties.push({
Expand Down Expand Up @@ -608,7 +608,7 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {
)
)
),
initializer: optionsInterface.properties!.every((property) => property.hasQuestionToken)
initializer: optionsInterface.properties?.every((property) => property.hasQuestionToken)
? "{}"
: undefined
}
Expand Down Expand Up @@ -651,7 +651,7 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {
serviceClass.methods.push(method);

if (overloads.length === 0) {
maybeAddDocs(method, docs);
maybeAddDocsStructure(method, docs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
import {
getExampleEndpointCalls,
getTextOfTsNode,
maybeAddDocs,
maybeAddDocsNode,
maybeAddDocsStructure,
PackageId,
TypeReferenceNode,
visitJavaScriptRuntime
Expand Down Expand Up @@ -108,7 +109,7 @@ export class GeneratedRequestWrapperImpl implements GeneratedRequestWrapper {
),
hasQuestionToken: type.isOptional
});
maybeAddDocs(property, queryParameter.docs);
maybeAddDocsNode(property, queryParameter.docs);
}
for (const header of this.getAllNonLiteralHeaders(context)) {
const type = context.type.getReferenceToType(header.valueType);
Expand All @@ -117,7 +118,7 @@ export class GeneratedRequestWrapperImpl implements GeneratedRequestWrapper {
type: getTextOfTsNode(type.typeNodeWithoutUndefined),
hasQuestionToken: type.isOptional
});
maybeAddDocs(property, header.docs);
maybeAddDocsNode(property, header.docs);
}
if (this.endpoint.requestBody != null) {
HttpRequestBody._visit(this.endpoint.requestBody, {
Expand Down Expand Up @@ -145,7 +146,7 @@ export class GeneratedRequestWrapperImpl implements GeneratedRequestWrapper {
type: getTextOfTsNode(type.typeNodeWithoutUndefined),
hasQuestionToken: type.isOptional
});
maybeAddDocs(property, referenceToRequestBody.docs);
maybeAddDocsNode(property, referenceToRequestBody.docs);
},
fileUpload: (fileUploadRequest) => {
for (const property of fileUploadRequest.properties) {
Expand Down Expand Up @@ -290,7 +291,9 @@ export class GeneratedRequestWrapperImpl implements GeneratedRequestWrapper {
.filter((prop): prop is InlinedRequestBodyProperty => prop !== undefined)
.map((property): [ObjectProperty, NamedType] | undefined => {
const namedType = getNamedType(property?.valueType);
if (namedType) return [property, namedType];
if (namedType) {
return [property, namedType];
}
return undefined;
})
.filter((x): x is [ObjectProperty, TypeReference.Named] => x != null)
Expand Down Expand Up @@ -601,7 +604,6 @@ export class GeneratedRequestWrapperImpl implements GeneratedRequestWrapper {
return property.type === "fileArray" ? ts.factory.createArrayTypeNode(value) : value;
}
}

function getNamedType(typeReference: TypeReference): NamedType | undefined {
switch (typeReference.type) {
case "named":
Expand All @@ -621,6 +623,7 @@ function getNamedType(typeReference: TypeReference): NamedType | undefined {
default:
assertNever(typeReference.container);
}
// fallthrough
case "primitive":
return undefined;
case "unknown":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { JSDocableNode, JSDocableNodeStructure } from "ts-morph";

export function maybeAddDocs(node: JSDocableNodeStructure | JSDocableNode, docs: string | null | undefined): void {
export function maybeAddDocsNode(node: JSDocableNode, docs: string | null | undefined): void {
if (docs != null) {
docs = "\n" + docs;
if ("docs" in node) {
// add newline so ts-morph makes it a multiline comment
node.docs = [docs];
return;
} else if ("addJsDoc" in node) {
node.addJsDoc(docs);
}
node.addJsDoc(docs);
}
}

export function maybeAddDocsStructure(node: JSDocableNodeStructure, docs: string | null | undefined): void {
if (docs != null) {
docs = "\n" + docs;
// add newline so ts-morph makes it a multiline comment
node.docs = [docs];
return;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CodeBlockWriter, ts, WriterFunction } from "ts-morph";
import { CodeBlockWriter, WriterFunction } from "ts-morph";

export function writerToString(writer: WriterFunction | string): string {
if (typeof writer === "string") return writer;
let result = "";
if (typeof writer === "string") {
return writer;
}
// Create a minimal writer context that captures the output
const writerContext = new CodeBlockWriter();
// Execute the writer with our context
writer(writerContext);
return result;
return writerContext.toString();
}
2 changes: 1 addition & 1 deletion generators/typescript/utils/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { getPropertyKey } from "./codegen-utils/getPropertyKey";
export { getSchemaOptions } from "./codegen-utils/getSchemaOptions";
export { getTextOfTsKeyword } from "./codegen-utils/getTextOfTsKeyword";
export { getTextOfTsNode } from "./codegen-utils/getTextOfTsNode";
export { maybeAddDocs } from "./codegen-utils/maybeAddDocs";
export { maybeAddDocsNode, maybeAddDocsStructure } from "./codegen-utils/maybeAddDocs";
export { writerToString } from "./codegen-utils/writerToString";
export * from "./core-utilities";
export { type Zurg } from "./core-utilities/zurg/Zurg";
Expand Down
Loading

0 comments on commit 4d900c2

Please sign in to comment.