-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageformat-lint.js
75 lines (70 loc) · 3 KB
/
messageformat-lint.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
function createAnnotation(msg, severity, from, to) {
return {
message: msg,
severity: severity,
from: CodeMirror.Pos(from.line-1, from.column-1),
to: CodeMirror.Pos(to.line-1, to.column-1),
};
}
function extractHintsFromAst(astNode) {
switch(astNode.type) {
case "messageFormatPattern":
return [].concat.apply([], astNode.statements.map(extractHintsFromAst));
case "invalidUnicode":
return [createAnnotation("Invalid unicode escape sequence: " + astNode.text, "error", astNode.start, astNode.end)];
case "invalidEscapeChar":
return [createAnnotation("Invalid escape character: " + astNode.text, "error", astNode.start, astNode.end)];
case "invalidLiteral":
return [createAnnotation("Invalid literal character: " + astNode.text, "error", astNode.start, astNode.end)];
case "invalidMessageFormatElement":
var msg = (astNode.unterminated ? "Unterminated" : "Invalid") + " messageformat element";
return [createAnnotation(msg, "error", astNode.start, astNode.end)];
case "messageFormatElement":
if(astNode.elementFormat) {
var elementFormat = astNode.elementFormat;
if(["plural", "selectordinal", "select"].indexOf(elementFormat.key) >= 0) {
var annotations = [];
//check the own keys
var duplicatedKeys = [];
var indexedForms = {};
var forms = elementFormat.key == "select" ? elementFormat.val.selectForms : elementFormat.val.pluralForms;
forms.forEach(function(form) {
if(!indexedForms[form.key.val]) indexedForms[form.key.val] = [];
indexedForms[form.key.val].push(form);
if(indexedForms[form.key.val].length > 1) duplicatedKeys.push(form.key.val);
});
//missing "other" key?
if(!indexedForms.other) {
annotations.push(createAnnotation("missing other key", "error", elementFormat.start, elementFormat.end));
}
//duplicated keys?
if(duplicatedKeys.length) {
duplicatedKeys.forEach(function (key) {
var msg = "duplicated key: " + key;
indexedForms[key].forEach(function(form) {
annotations.push(createAnnotation(msg, "warning", form.key.start, form.key.end));
});
});
}
//don't forget the children...
return [].concat.apply(annotations, forms.map(function(f) {return extractHintsFromAst(f.val);}));
}
}
}
return [];
}
CodeMirror.registerHelper("lint", "messageformat.js", function(text, options, editor) {
var lineCnt = editor.lineCount();
var ast = forgivingMessageformatParser.parse(text);
return extractHintsFromAst(ast);
});
});