From 33b62cebba2dfe56a2aa1c1e03a99087b8b74a4f Mon Sep 17 00:00:00 2001 From: JG Date: Sun, 31 Dec 2023 11:54:59 +0000 Subject: [PATCH] fix: allow tripple comments at the end of the field --- packages/language/src/generated/grammar.ts | 26 ++++++++++++++++ packages/language/src/zmodel.langium | 4 +-- .../tests/generator/prisma-generator.test.ts | 30 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/language/src/generated/grammar.ts b/packages/language/src/generated/grammar.ts index 9ae762b6a..321131b09 100644 --- a/packages/language/src/generated/grammar.ts +++ b/packages/language/src/generated/grammar.ts @@ -2143,6 +2143,19 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "arguments": [] }, "cardinality": "*" + }, + { + "$type": "Assignment", + "feature": "comments", + "operator": "+=", + "terminal": { + "$type": "RuleCall", + "rule": { + "$ref": "#/rules@69" + }, + "arguments": [] + }, + "cardinality": "*" } ] }, @@ -2409,6 +2422,19 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "arguments": [] }, "cardinality": "*" + }, + { + "$type": "Assignment", + "feature": "comments", + "operator": "+=", + "terminal": { + "$type": "RuleCall", + "rule": { + "$ref": "#/rules@69" + }, + "arguments": [] + }, + "cardinality": "*" } ] }, diff --git a/packages/language/src/zmodel.langium b/packages/language/src/zmodel.langium index 6760bb87d..7aacc2ad0 100644 --- a/packages/language/src/zmodel.langium +++ b/packages/language/src/zmodel.langium @@ -190,7 +190,7 @@ DataModel: DataModelField: (comments+=TRIPLE_SLASH_COMMENT)* - name=RegularID type=DataModelFieldType (attributes+=DataModelFieldAttribute)*; + name=RegularID type=DataModelFieldType (attributes+=DataModelFieldAttribute)* (comments+=TRIPLE_SLASH_COMMENT)*; DataModelFieldType: (type=BuiltinType | unsupported=UnsupportedFieldType | reference=[TypeDeclaration:RegularID]) (array?='[' ']')? (optional?='?')?; @@ -209,7 +209,7 @@ Enum: EnumField: (comments+=TRIPLE_SLASH_COMMENT)* - name=RegularID (attributes+=DataModelFieldAttribute)*; + name=RegularID (attributes+=DataModelFieldAttribute)* (comments+=TRIPLE_SLASH_COMMENT)*; // function FunctionDecl: diff --git a/packages/schema/tests/generator/prisma-generator.test.ts b/packages/schema/tests/generator/prisma-generator.test.ts index 049104b1e..5bca598c8 100644 --- a/packages/schema/tests/generator/prisma-generator.test.ts +++ b/packages/schema/tests/generator/prisma-generator.test.ts @@ -158,6 +158,36 @@ describe('Prisma generator test', () => { expect(content).toContain(`/// @TypeGraphQL.field(name: 'bar')`); }); + it('triple slash comments complex', async () => { + const model = await loadModel(` + datasource db { + provider = 'sqlite' + url = 'file:dev.db' + } + + /// This is a comment + model Foo { + id String @id + /// Supposed to be for value, but will be counted for id + value Int /// comment for value + } + `); + + const { name } = tmp.fileSync({ postfix: '.prisma' }); + await new PrismaSchemaGenerator().generate(model, { + name: 'Prisma', + provider: '@core/prisma', + schemaPath: 'schema.zmodel', + output: name, + }); + + const content = fs.readFileSync(name, 'utf-8'); + await getDMMF({ datamodel: content }); + + expect(content).toContain('/// Supposed to be for value, but will be counted for id'); + expect(content).toContain('/// comment for value'); + }); + it('model and field mapping', async () => { const model = await loadModel(` datasource db {