Skip to content

Commit

Permalink
Compiler fixes (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
csansoon authored Sep 30, 2024
1 parent aee65a8 commit c76fa89
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 14 deletions.
39 changes: 38 additions & 1 deletion packages/compiler/src/compiler/readMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,43 @@ describe('config', async () => {
})
})

it('does not confuse several dashes with a config section', async () => {
const prompt = removeCommonIndent(`
This is not config:
--------------------
Nor this:
----
This ain't either:
--
`)

const metadata = await readMetadata({
prompt: removeCommonIndent(prompt),
})

expect(metadata.errors[0]?.toString()).toBeUndefined()
expect(metadata.errors.length).toBe(0)
expect(metadata.config).toEqual({})
})

it('can be escaped', async () => {
const prompt = removeCommonIndent(`
This is NOT a config:
\\---
foo: bar
baz:
- qux
- quux
\\---
`)

const metadata = await readMetadata({ prompt })

expect(metadata.config).toEqual({})
})

it('fails when there is content before the config section', async () => {
const prompt = removeCommonIndent(`
Lorem ipsum
Expand Down Expand Up @@ -360,7 +397,7 @@ describe('config', async () => {

it('returns the correct positions of parsing errors', async () => {
const prompt = removeCommonIndent(`
/*
/*
Lorem ipsum
*/
---
Expand Down
21 changes: 16 additions & 5 deletions packages/compiler/src/compiler/readMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class ReadMetadata {
})
}

const parsedObj = parsedYaml.toJS()
const parsedObj = parsedYaml.toJS() ?? {}

try {
this.configSchema?.parse(parsedObj)
Expand All @@ -289,14 +289,21 @@ export class ReadMetadata {
err.errors.forEach((error) => {
const issue = error.message

const [errorStart, errorEnd] = findYAMLItemPosition(
const range = findYAMLItemPosition(
parsedYaml.contents as YAMLItem,
error.path,
)!
)

const errorStart = range
? node.start! + CONFIG_START_OFFSET + range[0]
: node.start!
const errorEnd = range
? node.start! + CONFIG_START_OFFSET + range[1] + 1
: node.end!

this.baseNodeError(errors.invalidConfig(issue), node, {
start: node.start! + CONFIG_START_OFFSET + errorStart,
end: node.start! + CONFIG_START_OFFSET + errorEnd + 1,
start: errorStart,
end: errorEnd,
})
})
}
Expand Down Expand Up @@ -629,6 +636,10 @@ export class ReadMetadata {
}
}

if (node.children.length) {
this.baseNodeError(errors.invalidStepChildren, node)
}

return
}

Expand Down
6 changes: 4 additions & 2 deletions packages/compiler/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ export function findYAMLItemPosition(
parent: YAMLItem,
path: (string | number)[],
): YAMLItemRange {
const parentRange: YAMLItemRange = parent.range
const parentRange: YAMLItemRange = parent?.range
? [parent.range[0], parent.range[1]]
: undefined

if (path.length === 0 || !('items' in parent)) return parentRange
if (!parentRange || path.length === 0 || !('items' in parent)) {
return parentRange
}

let child: YAMLItem | undefined
if (parent instanceof YAMLMap) {
Expand Down
11 changes: 8 additions & 3 deletions packages/compiler/src/error/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export default {
},
unexpectedEofToken: (token: string) => ({
code: 'unexpected-eof',
message: `Unexpected '${token}'`,
message: `Unexpected EOF. Expected '${token}' but did not find it.`,
}),
unexpectedToken: (token: string) => ({
code: 'unexpected-token',
message: `Expected '${token}'`,
message: `Expected '${token}' but did not find it.`,
}),
unexpectedBlockClose: {
code: 'unexpected-block-close',
Expand Down Expand Up @@ -265,6 +265,11 @@ export default {
},
invalidStepConfig: {
code: 'invalid-step-config',
message: 'Step config must be an object',
message: 'Response config must be an object',
},
invalidStepChildren: {
code: 'invalid-step-children',
message:
'Response tags cannot have children.\nIf you need to add content to the response context, just add it before the response tag.',
},
}
5 changes: 5 additions & 0 deletions packages/compiler/src/parser/state/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PARSER_ERRORS from '$compiler/error/errors'
import { Parser } from '$compiler/parser'
import type { Config } from '$compiler/parser/interfaces'

Expand All @@ -6,7 +7,11 @@ export function config(parser: Parser) {
parser.eat('---')

// Read until there is a line break followed by a triple dash
const currentIndex = parser.index
const data = parser.readUntil(/\n\s*---\s*/)
if (parser.index === parser.template.length) {
parser.error(PARSER_ERRORS.unexpectedToken('---'), currentIndex + 1)
}

parser.allowWhitespace()
parser.eat('---', true)
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/parser/state/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function fragment(parser: Parser): (parser: Parser) => void {
if (parser.match('/*') || parser.match('*/')) {
return multiLineComment
}
if (parser.match('---')) {
if (parser.matchRegex(/-{3}(?!-)/)) {
return config
}

Expand Down
12 changes: 10 additions & 2 deletions packages/compiler/src/parser/state/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function text(parser: Parser) {
const isEscaping = ENDS_WITH_ESCAPE_REGEX.test(data)
if (isEscaping) data = data.slice(0, -1) // Remove the escape character

if (!isEscaping && parser.match('---')) {
if (!isEscaping && parser.matchRegex(/-{3}(?!-)/)) {
// Detecting ONLY 3 consecutive dashes
break
}

Expand All @@ -23,7 +24,14 @@ export function text(parser: Parser) {
) {
break
}
data += parser.template[parser.index++]

const nonConfigDashes = parser.matchRegex(/-+/)
if (nonConfigDashes) {
data += nonConfigDashes
parser.index += nonConfigDashes.length
} else {
data += parser.template[parser.index++]
}
}

const node = {
Expand Down

0 comments on commit c76fa89

Please sign in to comment.