Skip to content

Show codeframe with location of fluent parse errors #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions __tests__/frameworks/vite/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ describe('Error checking', () => {
await expect(code).rejects.toThrowErrorMatchingInlineSnapshot(`
"Fluent parse errors:
E0003: Expected token: \\"}\\" (2:31)
E0010: Expected one of the variants to be marked as default (*) (9:3)"
1 | # Simple things are simple.
2 | hello-user = Hello, {$userName!
| ^
3 |
4 | # Complex things are possible.
E0010: Expected one of the variants to be marked as default (*) (9:3)
7 | [one] added one photo
8 | [other] added {$photoCount} new photo
9 | }to {$userGender ->
| ^
10 | [male] his stream
11 | [female] her stream"
`)
})

Expand All @@ -53,7 +64,18 @@ describe('Error checking', () => {
await expect(code).rejects.toThrowErrorMatchingInlineSnapshot(`
"Fluent parse errors:
E0003: Expected token: \\"}\\" (2:31)
E0010: Expected one of the variants to be marked as default (*) (9:3)"
1 | # Simple things are simple.
2 | hello-user = Hello, {$userName!
| ^
3 |
4 | # Complex things are possible.
E0010: Expected one of the variants to be marked as default (*) (9:3)
7 | [one] added one photo
8 | [other] added {$photoCount} new photo
9 | }to {$userGender ->
| ^
10 | [male] his stream
11 | [female] her stream"
`)
})
})
47 changes: 45 additions & 2 deletions src/plugins/ftl/parse.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
import type { Junk } from '@fluent/syntax'
import { columnOffset, lineOffset, parse } from '@fluent/syntax'

export function getSyntaxErrors(source: string): string | undefined {
function padRight(str: string | number, len: number) {
return str + ' '.repeat(len - String(str).length)
}

const RANGE = 2

/**
* Generate a string that highlights the position of the error in the source
* @param source The source string
* @param line The line number of the error (1-indexed)
* @param column The column number of the error (1-indexed)
* Example:
* | proper-key = Value
* | key-with-error = error {->
* | ^
* | continuation = Value
*/
export function generateCodeFrame(
source: string,
line: number,
column: number,
): string {
const lines = source.split(/\r?\n/)
const start = Math.max(line - RANGE - 1, 0)
const end = Math.min(lines.length, line + RANGE)

const result = []

const lineNumberLength = String(end).length

for (let i = start; i < end; i++) {
result.push(`${padRight(i + 1, lineNumberLength)} | ${lines[i]}`)

if (i + 1 === line)
result.push(`${padRight(' ', lineNumberLength)} | ${' '.repeat(column - 1)}^`)
}

return result.join('\n')
}

export function getSyntaxErrors(
source: string,
): string | undefined {
const parsed = parse(source, { withSpans: true })
const junks = parsed.body.filter(x => x.type === 'Junk') as Junk[]
const errors = junks.map(x => x.annotations).flat()
if (errors.length > 0) {
const errorsText = errors.map((x) => {
const line = lineOffset(source, x.span.start) + 1
const column = columnOffset(source, x.span.start) + 1
return ` ${x.code}: ${x.message} (${line}:${column})`
return ` ${x.code}: ${x.message} (${line}:${column})
${generateCodeFrame(source, line, column)}`
}).join('\n')

return `Fluent parse errors:\n${errorsText}`
Expand Down