Skip to content

Commit c3ca4c6

Browse files
committed
update
1 parent f7bf471 commit c3ca4c6

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

packages/schema/src/language-server/zmodel-documentation-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AstNode, JSDocDocumentationProvider } from 'langium';
55
*/
66
export class ZModelDocumentationProvider extends JSDocDocumentationProvider {
77
getDocumentation(node: AstNode): string | undefined {
8-
// prefer to user triple-slash comments
8+
// prefer to use triple-slash comments
99
if ('comments' in node && Array.isArray(node.comments) && node.comments.length > 0) {
1010
return node.comments.map((c: string) => c.replace(/^[/]*\s*/, '')).join('\n');
1111
}

packages/schema/src/plugins/prisma/schema-generator.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export class PrismaSchemaGenerator {
100100
`;
101101

102102
private mode: 'logical' | 'physical' = 'physical';
103+
private customAttributesAsComments = false;
103104

104105
// a mapping from full names to shortened names
105106
private shortNameMap = new Map<string, string>();
@@ -117,6 +118,14 @@ export class PrismaSchemaGenerator {
117118
this.mode = options.mode as 'logical' | 'physical';
118119
}
119120

121+
if (
122+
options.customAttributesAsComments !== undefined &&
123+
typeof options.customAttributesAsComments !== 'boolean'
124+
) {
125+
throw new PluginError(name, 'option "customAttributesAsComments" must be a boolean');
126+
}
127+
this.customAttributesAsComments = options.customAttributesAsComments === true;
128+
120129
const prismaVersion = getPrismaVersion();
121130
if (prismaVersion && semver.lt(prismaVersion, PRISMA_MINIMUM_VERSION)) {
122131
warnings.push(
@@ -284,10 +293,7 @@ export class PrismaSchemaGenerator {
284293

285294
// user defined comments pass-through
286295
decl.comments.forEach((c) => model.addComment(c));
287-
288-
decl.attributes
289-
.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr))
290-
.forEach((attr) => model.addComment('/// - _' + this.zModelGenerator.generate(attr) + '_'));
296+
this.getCustomAttributesAsComments(decl).forEach((c) => model.addComment(c));
291297

292298
// generate relation fields on base models linking to concrete models
293299
this.generateDelegateRelationForBase(model, decl);
@@ -763,12 +769,8 @@ export class PrismaSchemaGenerator {
763769
)
764770
.map((attr) => this.makeFieldAttribute(attr));
765771

766-
const nonPrismaAttributes = field.attributes.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr));
767-
768772
// user defined comments pass-through
769-
const docs: string[] = [...field.comments];
770-
docs.push(...nonPrismaAttributes.map((attr) => '/// - _' + this.zModelGenerator.generate(attr) + '_'));
771-
773+
const docs = [...field.comments, ...this.getCustomAttributesAsComments(field)];
772774
const result = model.addField(field.name, type, attributes, docs, addToFront);
773775

774776
if (this.mode === 'logical') {
@@ -900,23 +902,27 @@ export class PrismaSchemaGenerator {
900902

901903
// user defined comments pass-through
902904
decl.comments.forEach((c) => _enum.addComment(c));
903-
904-
decl.attributes
905-
.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr))
906-
.forEach((attr) => _enum.addComment('/// - _' + this.zModelGenerator.generate(attr) + '_'));
905+
this.getCustomAttributesAsComments(decl).forEach((c) => _enum.addComment(c));
907906
}
908907

909908
private generateEnumField(_enum: PrismaEnum, field: EnumField) {
910909
const attributes = field.attributes
911910
.filter((attr) => this.isPrismaAttribute(attr))
912911
.map((attr) => this.makeFieldAttribute(attr));
913912

914-
const nonPrismaAttributes = field.attributes.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr));
915-
916-
const docs = [...field.comments];
917-
docs.push(...nonPrismaAttributes.map((attr) => '/// - _' + this.zModelGenerator.generate(attr) + '_'));
913+
const docs = [...field.comments, ...this.getCustomAttributesAsComments(field)];
918914
_enum.addField(field.name, attributes, docs);
919915
}
916+
917+
private getCustomAttributesAsComments(decl: DataModel | DataModelField | Enum | EnumField) {
918+
if (!this.customAttributesAsComments) {
919+
return [];
920+
} else {
921+
return decl.attributes
922+
.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr))
923+
.map((attr) => `/// ${this.zModelGenerator.generate(attr)}`);
924+
}
925+
}
920926
}
921927

922928
function isDescendantOf(model: DataModel, superModel: DataModel): boolean {

packages/schema/tests/generator/prisma-generator.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe('Prisma generator test', () => {
8585
schemaPath: 'schema.zmodel',
8686
output: 'schema.prisma',
8787
format: false,
88+
customAttributesAsComments: true,
8889
});
8990

9091
const content = fs.readFileSync('schema.prisma', 'utf-8');
@@ -96,9 +97,9 @@ describe('Prisma generator test', () => {
9697
);
9798
expect(content).toContain('schemas = ["auth", "public"]');
9899
expect(content).toContain('/// My user model');
99-
expect(content).toContain(`/// - _@@allow('all', true)_`);
100+
expect(content).toContain(`/// @@allow('all', true)`);
100101
expect(content).toContain(`/// the id field`);
101-
expect(content).toContain(`/// - _@allow('read', this == auth())_`);
102+
expect(content).toContain(`/// @allow('read', this == auth())`);
102103
expect(content).not.toContain('/// My post model');
103104
expect(content).toContain('/// User roles');
104105
expect(content).toContain('/// Admin role');
@@ -205,6 +206,7 @@ describe('Prisma generator test', () => {
205206
provider: '@core/prisma',
206207
schemaPath: 'schema.zmodel',
207208
output: name,
209+
customAttributesAsComments: true,
208210
});
209211

210212
const content = fs.readFileSync(name, 'utf-8');
@@ -237,6 +239,7 @@ describe('Prisma generator test', () => {
237239
provider: '@core/prisma',
238240
schemaPath: 'schema.zmodel',
239241
output: name,
242+
customAttributesAsComments: true,
240243
});
241244

242245
const content = fs.readFileSync(name, 'utf-8');
@@ -430,6 +433,7 @@ describe('Prisma generator test', () => {
430433
schemaPath: 'schema.zmodel',
431434
output: name,
432435
generateClient: false,
436+
customAttributesAsComments: true,
433437
});
434438

435439
const content = fs.readFileSync(name, 'utf-8');
@@ -480,6 +484,7 @@ describe('Prisma generator test', () => {
480484
schemaPath: 'schema.zmodel',
481485
output: name,
482486
format: true,
487+
customAttributesAsComments: true,
483488
});
484489

485490
const content = fs.readFileSync(name, 'utf-8');
@@ -511,6 +516,7 @@ describe('Prisma generator test', () => {
511516
schemaPath: 'schema.zmodel',
512517
output: name,
513518
format: true,
519+
customAttributesAsComments: true,
514520
});
515521

516522
const content = fs.readFileSync(name, 'utf-8');

0 commit comments

Comments
 (0)