From 944d0fb4d9c56d824191c2ba98c9edff982d7363 Mon Sep 17 00:00:00 2001 From: Cephas369 <84059136+Cephas369@users.noreply.github.com> Date: Sat, 15 Feb 2025 11:32:28 -0400 Subject: [PATCH] fix(compiler): fixed contract roles value parsing (#273) * fix(compiler): fixed contract roles value parsing * fix(compiler): stop compilation if parser returns errors * fix(compiler): stop compilation if parser returns errors (lint fix) --- packages/cli/src/buildAeriaLang.ts | 8 +++---- .../compiler/src/codegen/generateContracts.ts | 23 +++++++++++-------- packages/compiler/src/compile.ts | 2 +- packages/compiler/src/parser.ts | 7 +++++- playground/schemas/main.aeria | 5 ++++ 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/packages/cli/src/buildAeriaLang.ts b/packages/cli/src/buildAeriaLang.ts index 1b9fee12..dbf5399d 100644 --- a/packages/cli/src/buildAeriaLang.ts +++ b/packages/cli/src/buildAeriaLang.ts @@ -13,14 +13,14 @@ export const buildAeriaLangPhase = async () => { outDir: '.aeria/out', }) - if( !('emittedFiles' in result) ) { - return Result.result('no aeria schemas to build') - } - if( !result.success ) { return Result.error(result.errors.map((error) => `\n${error.fileLocation}:${error.location.line} at column (${error.location.start}-${error.location.end}) - ${error.message}`).join(' | ')) } + if( !('emittedFiles' in result) ) { + return Result.result('no aeria schemas to build') + } + return Result.result('aeria schemas built') } diff --git a/packages/compiler/src/codegen/generateContracts.ts b/packages/compiler/src/codegen/generateContracts.ts index e5396169..cfd29064 100644 --- a/packages/compiler/src/codegen/generateContracts.ts +++ b/packages/compiler/src/codegen/generateContracts.ts @@ -1,5 +1,5 @@ import type * as AST from '../ast.js' -import type { Property } from '@aeriajs/types' +import type { ContractWithRoles, Property } from '@aeriajs/types' import { errorSchema, resultSchema } from '@aeriajs/types' import { getProperties, propertyToSchema, stringify, UnquotedSymbol, type StringifyProperty } from './utils.js' @@ -52,12 +52,15 @@ const makeJSContractsCode = (contractAst: AST.ContractNode[]) => { } } - const contractSchema: Record = getProperties(contractProperty) + const contractSchema: Record = getProperties(contractProperty) if (responseString) { contractSchema.response = { [UnquotedSymbol]: responseString, } } + if (roles) { + contractSchema.roles = roles + } return `export const ${name} = defineContract(${ stringify(contractSchema) @@ -91,14 +94,16 @@ const makeTSContractsCode = (contractAst: AST.ContractNode[]) => { } } - const contractProperties = getProperties(contractSchema) + const contractProperties: ContractWithRoles = getProperties(contractSchema) + if (responseSchema) { + contractProperties.response = responseSchema + } + if (roles) { + contractProperties.roles = roles + } + return `export declare const ${contractNode.name}: ${ - stringify({ - ...contractProperties, - ...(responseSchema && { - response: responseSchema, - }), - }) + stringify(contractProperties) }` }).join('\n\n') } diff --git a/packages/compiler/src/compile.ts b/packages/compiler/src/compile.ts index 6c59f885..f0402b57 100644 --- a/packages/compiler/src/compile.ts +++ b/packages/compiler/src/compile.ts @@ -75,7 +75,7 @@ export const compileFromFiles = async (schemaDir: string, options: CompilationOp } const result = await parseAndCheck(sources, options) - if( !result.ast ) { + if( !result.ast || result.errorCount > 0 ) { return result } diff --git a/packages/compiler/src/parser.ts b/packages/compiler/src/parser.ts index 8c86644c..485f7d42 100644 --- a/packages/compiler/src/parser.ts +++ b/packages/compiler/src/parser.ts @@ -743,9 +743,14 @@ export const parse = (tokens: (Token | undefined)[]) => { const { value: keyword } = consume(TokenType.Keyword, lexer.CONTRACT_KEYWORDS) switch( keyword ) { case 'roles': { - if( match(TokenType.QuotedString, 'unauthenticated') ) { + if( match(TokenType.Boolean) ) { + const { value: boolean } = consume(TokenType.Boolean) + node.roles = boolean + } else if( match(TokenType.QuotedString, 'unauthenticated') ) { + consume(TokenType.QuotedString) node.roles = 'unauthenticated' } else if( match(TokenType.QuotedString, 'unauthenticated-only') ) { + consume(TokenType.QuotedString) node.roles = 'unauthenticated-only' } else { const { value, symbols } = parseArrayBlock() diff --git a/playground/schemas/main.aeria b/playground/schemas/main.aeria index ee09f62e..93fe965c 100644 --- a/playground/schemas/main.aeria +++ b/playground/schemas/main.aeria @@ -242,4 +242,9 @@ contract ContractTest { contacts Topic } } + response + | Error { properties { name str } } + | Result { properties { name str, age num } } + | str + roles true } \ No newline at end of file