-
Notifications
You must be signed in to change notification settings - Fork 3
/
reporter.ts
61 lines (48 loc) · 4.21 KB
/
reporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Problem } from 'wollok-ts'
import { lang } from './settings'
// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
// VALIDATION MESSAGES DEFINITION
// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
type ValidationMessage = { [key: string]: string }
const FAILURE = 'failure'
const validationMessagesEn: ValidationMessage = {
'nameShouldBeginWithLowercase': 'The name {0} must start with lowercase',
'nameShouldBeginWithUppercase': 'The name {0} must start with uppercase',
'nameShouldNotBeKeyword': 'The name {0} is a keyword, you should pick another one',
'shouldNotBeEmpty': 'Should not make an empty definition.',
'shouldUseConditionalExpression': 'Bad usage of if! You must return the condition itself without using if.',
[FAILURE]: 'Rule failure: ',
}
const validationMessagesEs: ValidationMessage = {
'nameShouldBeginWithLowercase': 'El nombre {0} debe comenzar con minúsculas',
'nameShouldBeginWithUppercase': 'El nombre {0} debe comenzar con mayúsculas',
'nameShouldNotBeKeyword': 'El nombre {0} es una palabra reservada, debe cambiarla',
'shouldNotBeEmpty': 'El elemento no puede estar vacío: falta escribir código.',
'shouldUseConditionalExpression': 'Estás usando incorrectamente el if. Devolvé simplemente la expresión booleana.',
[FAILURE]: 'La siguiente regla falló: ',
}
const validationMessages: { [key: string]: ValidationMessage } = {
'en': validationMessagesEn,
'es': validationMessagesEs,
}
// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
// INTERNAL FUNCTIONS
// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
const convertToHumanReadable = (code: string) => {
if (!code) { return '' }
const result = code.replace(/[A-Z0-9]+/g, (match) => ' ' + match.toLowerCase())
return validationI18nized()[FAILURE] + result.charAt(0).toUpperCase() + result.slice(1, result.length)
}
const interpolateValidationMessage = (message: string, ...values: string[]) =>
message.replace(/{\d*}/g, (match: string) => {
const index = match.replace('{', '').replace('}', '') as unknown as number
return values[index] || ''
}
)
const getBasicMessage = (problem: Problem) => validationI18nized()[problem.code] || convertToHumanReadable(problem.code)
const validationI18nized = () =>
validationMessages[lang()] as ValidationMessage
// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
// PUBLIC INTERFACE
// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
export const reportMessage = (problem: Problem): string => interpolateValidationMessage(getBasicMessage(problem))