Skip to content

Commit

Permalink
fix(compiler): fixed contract roles value parsing (#273)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
Cephas369 authored Feb 15, 2025
1 parent b14b8f6 commit 944d0fb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/cli/src/buildAeriaLang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

23 changes: 14 additions & 9 deletions packages/compiler/src/codegen/generateContracts.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -52,12 +52,15 @@ const makeJSContractsCode = (contractAst: AST.ContractNode[]) => {
}
}

const contractSchema: Record<string, unknown> = getProperties(contractProperty)
const contractSchema: Record<keyof ContractWithRoles, unknown> = getProperties(contractProperty)
if (responseString) {
contractSchema.response = {
[UnquotedSymbol]: responseString,
}
}
if (roles) {
contractSchema.roles = roles
}

return `export const ${name} = defineContract(${
stringify(contractSchema)
Expand Down Expand Up @@ -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')
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion packages/compiler/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions playground/schemas/main.aeria
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,9 @@ contract ContractTest {
contacts Topic
}
}
response
| Error { properties { name str } }
| Result { properties { name str, age num } }
| str
roles true
}

0 comments on commit 944d0fb

Please sign in to comment.