Skip to content

Commit 0a46ea8

Browse files
authored
improvement(message-compiler): Improves parse error messages (#251)
1 parent 45008fe commit 0a46ea8

File tree

4 files changed

+285
-26
lines changed

4 files changed

+285
-26
lines changed

packages/message-compiler/src/errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export const enum CompileErrorCodes {
3535

3636
// parser error codes
3737
MUST_HAVE_MESSAGES_IN_PLURAL,
38+
UNEXPECTED_EMPTY_LINKED_MODIFIER,
39+
UNEXPECTED_EMPTY_LINKED_KEY,
3840
UNEXPECTED_LEXICAL_ANALYSIS,
3941

4042
// Special value for higher-order compilers to pick up the last code
@@ -58,6 +60,8 @@ export const errorMessages: { [code: number]: string } = {
5860
[CompileErrorCodes.INVALID_LINKED_FORMAT]: `Invalid linked format`,
5961
// parser error messages
6062
[CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
63+
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
64+
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
6165
[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`
6266
}
6367

packages/message-compiler/src/parser.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ export function createParser(options: ParserOptions = {}): Parser {
231231
// empty modifier
232232
emitError(
233233
tokenizer,
234-
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
234+
CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER,
235235
context.lastStartLoc,
236-
0,
237-
token.type
236+
0
238237
)
239238
node.value = ''
240239
endNode(node, offset, loc)
@@ -250,7 +249,7 @@ export function createParser(options: ParserOptions = {}): Parser {
250249
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
251250
context.lastStartLoc,
252251
0,
253-
token.type
252+
getTokenCaption(token)
254253
)
255254
}
256255
node.value = token.value || ''
@@ -299,7 +298,7 @@ export function createParser(options: ParserOptions = {}): Parser {
299298
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
300299
context.lastStartLoc,
301300
0,
302-
token.type
301+
getTokenCaption(token)
303302
)
304303
}
305304
token = tokenizer.nextToken()
@@ -317,7 +316,7 @@ export function createParser(options: ParserOptions = {}): Parser {
317316
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
318317
context.lastStartLoc,
319318
0,
320-
token.type
319+
getTokenCaption(token)
321320
)
322321
}
323322
linkedNode.key = parseLinkedKey(tokenizer, token.value || '')
@@ -329,7 +328,7 @@ export function createParser(options: ParserOptions = {}): Parser {
329328
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
330329
context.lastStartLoc,
331330
0,
332-
token.type
331+
getTokenCaption(token)
333332
)
334333
}
335334
linkedNode.key = parseNamed(tokenizer, token.value || '')
@@ -341,7 +340,7 @@ export function createParser(options: ParserOptions = {}): Parser {
341340
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
342341
context.lastStartLoc,
343342
0,
344-
token.type
343+
getTokenCaption(token)
345344
)
346345
}
347346
linkedNode.key = parseList(tokenizer, token.value || '')
@@ -353,7 +352,7 @@ export function createParser(options: ParserOptions = {}): Parser {
353352
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
354353
context.lastStartLoc,
355354
0,
356-
token.type
355+
getTokenCaption(token)
357356
)
358357
}
359358
linkedNode.key = parseLiteral(tokenizer, token.value || '')
@@ -362,10 +361,9 @@ export function createParser(options: ParserOptions = {}): Parser {
362361
// empty key
363362
emitError(
364363
tokenizer,
365-
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
364+
CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY,
366365
context.lastStartLoc,
367-
0,
368-
token.type
366+
0
369367
)
370368
const nextContext = tokenizer.context()
371369
const emptyLinkedKeyNode = startNode(
@@ -419,7 +417,7 @@ export function createParser(options: ParserOptions = {}): Parser {
419417
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
420418
context.lastStartLoc,
421419
0,
422-
token.type
420+
getTokenCaption(token)
423421
)
424422
}
425423
node.items.push(parseText(tokenizer, token.value || ''))
@@ -431,7 +429,7 @@ export function createParser(options: ParserOptions = {}): Parser {
431429
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
432430
context.lastStartLoc,
433431
0,
434-
token.type
432+
getTokenCaption(token)
435433
)
436434
}
437435
node.items.push(parseList(tokenizer, token.value || ''))
@@ -443,7 +441,7 @@ export function createParser(options: ParserOptions = {}): Parser {
443441
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
444442
context.lastStartLoc,
445443
0,
446-
token.type
444+
getTokenCaption(token)
447445
)
448446
}
449447
node.items.push(parseNamed(tokenizer, token.value || ''))
@@ -455,7 +453,7 @@ export function createParser(options: ParserOptions = {}): Parser {
455453
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
456454
context.lastStartLoc,
457455
0,
458-
token.type
456+
getTokenCaption(token)
459457
)
460458
}
461459
node.items.push(parseLiteral(tokenizer, token.value || ''))
@@ -552,7 +550,7 @@ export function createParser(options: ParserOptions = {}): Parser {
552550
CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,
553551
context.lastStartLoc,
554552
0,
555-
context.currentType
553+
source[context.offset] || ''
556554
)
557555
}
558556

@@ -562,3 +560,11 @@ export function createParser(options: ParserOptions = {}): Parser {
562560

563561
return { parse }
564562
}
563+
564+
function getTokenCaption(token: Token) {
565+
if (token.type === TokenTypes.EOF) {
566+
return 'EOF'
567+
}
568+
const name = (token.value || '').replace(/\r?\n/gu, '\\n')
569+
return name.length > 10 ? name.slice(0, 9) + '…' : name
570+
}

0 commit comments

Comments
 (0)