Skip to content

Commit

Permalink
Fix multiline docs being concatenated
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Oct 12, 2021
1 parent 2fd867d commit cf1f0c1
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 21 deletions.
2 changes: 1 addition & 1 deletion experiments/prisma/generated/client/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions experiments/prisma/generated/client/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ enum PostKind {
}

model post {
/// first line of comment
/// second line of comment
/// third line of comment
uuid String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
3 changes: 2 additions & 1 deletion experiments/prisma/generated/type-graphql/dmmf.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@
"args": []
},
"isGenerated": false,
"isUpdatedAt": false
"isUpdatedAt": false,
"documentation": "first line of comment\nsecond line of comment\nthird line of comment"
},
{
"name": "createdAt",
Expand Down
16 changes: 12 additions & 4 deletions experiments/prisma/generated/type-graphql/models/MainUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import { Post } from "../models/Post";
import { Role } from "../enums/Role";
import { MainUserCount } from "../resolvers/outputs/MainUserCount";

/** User model doc */
/**
* User model doc
*/
@TypeGraphQL.ObjectType({
isAbstract: true,
description: "User model doc"
})
export class MainUser {
/** User model field doc */
/**
* User model field doc
*/
@TypeGraphQL.Field(_type => TypeGraphQL.ID, {
nullable: false,
description: "User model field doc"
Expand All @@ -24,7 +28,9 @@ export class MainUser {
})
email!: string;

/** renamed field doc */
/**
* renamed field doc
*/
name!: string | null;

@TypeGraphQL.Field(_type => TypeGraphQL.Int, {
Expand Down Expand Up @@ -63,7 +69,9 @@ export class MainUser {
})
_count?: MainUserCount | null;

/** renamed field doc */
/**
* renamed field doc
*/
@TypeGraphQL.Field(_type => String, {
nullable: true,
description: "renamed field doc"
Expand Down
8 changes: 7 additions & 1 deletion experiments/prisma/generated/type-graphql/models/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import { PostKind } from "../enums/PostKind";
isAbstract: true
})
export class Post {
/**
* first line of comment
* second line of comment
* third line of comment
*/
@TypeGraphQL.Field(_type => TypeGraphQL.ID, {
nullable: false
nullable: false,
description: "first line of comment\nsecond line of comment\nthird line of comment"
})
uuid!: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@
"args": []
},
"isGenerated": false,
"isUpdatedAt": false
"isUpdatedAt": false,
"documentation": "first line of comment\\nsecond line of comment\\nthird line of comment"
},
{
"name": "createdAt",
Expand Down
3 changes: 3 additions & 0 deletions experiments/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ enum PostKind {
}

model post {
/// first line of comment
/// second line of comment
/// third line of comment
uuid String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
3 changes: 2 additions & 1 deletion src/generator/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path";
import { generateTypeGraphQLImport } from "./imports";
import { enumsFolderName } from "./config";
import { DMMF } from "./dmmf/types";
import { convertNewLines } from "./helpers";

export default function generateEnumFromDef(
project: Project,
Expand All @@ -21,7 +22,7 @@ export default function generateEnumFromDef(
isExported: true,
name: enumDef.typeName,
...(enumDef.docs && {
docs: [{ description: enumDef.docs }],
docs: [{ description: convertNewLines(enumDef.docs) }],
}),
members: enumDef.valuesMap.map<OptionalKind<EnumMemberStructure>>(
({ name, value }) => ({
Expand Down
20 changes: 16 additions & 4 deletions src/generator/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,25 @@ export function cleanDocsString(
cleanedDocs = cleanedDocs.replace(modelAttributeRegex, "");
cleanedDocs = cleanedDocs.replace(fieldAttributeRegex, "");
cleanedDocs = cleanedDocs.split('"').join('\\"');
cleanedDocs = cleanedDocs.split("\r").join("");
cleanedDocs = cleanedDocs.split("\\r").join("");
cleanedDocs = cleanedDocs.split("\n").join("");
cleanedDocs = cleanedDocs.split("\\n").join("");
if (cleanedDocs.endsWith("\r")) {
cleanedDocs = cleanedDocs.slice(0, -1);
}
if (cleanedDocs.endsWith("\\r")) {
cleanedDocs = cleanedDocs.slice(0, -2);
}
if (cleanedDocs.endsWith("\n")) {
cleanedDocs = cleanedDocs.slice(0, -1);
}
if (cleanedDocs.endsWith("\\n")) {
cleanedDocs = cleanedDocs.slice(0, -2);
}
return cleanedDocs;
}

export function convertNewLines(str: string) {
return str.split("\\n").join("\n");
}

export function toUnixPath(maybeWindowsPath: string) {
return maybeWindowsPath.split("\\").join("/");
}
7 changes: 4 additions & 3 deletions src/generator/model-type-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { modelsFolderName } from "./config";
import { DMMF } from "./dmmf/types";
import { DmmfDocument } from "./dmmf/dmmf-document";
import { convertNewLines } from "./helpers";

export default function generateObjectTypeClassFromModel(
project: Project,
Expand Down Expand Up @@ -109,7 +110,7 @@ export default function generateObjectTypeClassFromModel(
]),
],
...(field.docs && {
docs: [{ description: field.docs }],
docs: [{ description: `\n${convertNewLines(field.docs)}` }],
}),
};
}),
Expand Down Expand Up @@ -166,12 +167,12 @@ export default function generateObjectTypeClassFromModel(
: `return this.${field.name} ?? null;`,
],
...(field.docs && {
docs: [{ description: field.docs }],
docs: [{ description: `\n${convertNewLines(field.docs)}` }],
}),
};
}),
...(model.docs && {
docs: [{ description: model.docs }],
docs: [{ description: `\n${convertNewLines(model.docs)}` }],
}),
});
}
31 changes: 26 additions & 5 deletions tests/regression/__snapshots__/models.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import { DecimalJSScalar } from \\"../scalars\\";
import { Post } from \\"../models/Post\\";
import { UserCount } from \\"../resolvers/outputs/UserCount\\";
/** User model doc */
/**
* User model doc
*/
@TypeGraphQL.ObjectType({
isAbstract: true,
description: \\"User model doc\\"
Expand All @@ -48,7 +50,9 @@ export class User {
})
id!: number;
/** field doc */
/**
* field doc
*/
@TypeGraphQL.Field(_type => String, {
nullable: false,
description: \\"field doc\\"
Expand All @@ -60,7 +64,20 @@ export class User {
})
intField!: number;
/** relation doc */
/**
* multiline doc 1
* multiline doc 2
*/
@TypeGraphQL.Field(_type => Boolean, {
nullable: false,
description: \\"multiline doc 1
multiline doc 2\\"
})
boolField!: boolean;
/**
* relation doc
*/
posts?: Post[];
@TypeGraphQL.Field(_type => UserCount, {
Expand Down Expand Up @@ -241,7 +258,9 @@ export class User {
})
dateOfBirth!: Date;
/** renamed field docs */
/**
* renamed field docs
*/
name!: string;
balance!: number | null;
Expand All @@ -253,7 +272,9 @@ export class User {
})
_count?: UserCount | null;
/** renamed field docs */
/**
* renamed field docs
*/
@TypeGraphQL.Field(_type => String, {
nullable: false,
description: \\"renamed field docs\\"
Expand Down
3 changes: 3 additions & 0 deletions tests/regression/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ describe("models", () => {
stringField String
// field comment
intField Int
/// multiline doc 1
/// multiline doc 2
boolField Boolean
/// relation doc
posts Post[]
}
Expand Down

0 comments on commit cf1f0c1

Please sign in to comment.