mirror of
https://github.com/maputnik/editor.git
synced 2026-08-26 06:57:26 +00:00
abstract out tryToParse
This commit is contained in:
+11
-26
@@ -14,7 +14,9 @@ CodeMirror.defineMode("mgl", (config, parserConfig) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
CodeMirror.registerHelper("lint", "json", (text: string) => {
|
|
||||||
|
function tryToParse(text: string) {
|
||||||
|
|
||||||
const found: MarkerRangeWithMessage[] = [];
|
const found: MarkerRangeWithMessage[] = [];
|
||||||
try {
|
try {
|
||||||
parse(text);
|
parse(text);
|
||||||
@@ -38,37 +40,20 @@ CodeMirror.registerHelper("lint", "json", (text: string) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return found;
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
CodeMirror.registerHelper("lint", "json", (text: string) => {
|
||||||
|
return tryToParse(text);
|
||||||
});
|
});
|
||||||
|
|
||||||
CodeMirror.registerHelper("lint", "mgl", (text: string, opts: any, doc: any) => {
|
CodeMirror.registerHelper("lint", "mgl", (text: string, opts: any, doc: any) => {
|
||||||
const found: MarkerRangeWithMessage[] = [];
|
|
||||||
|
const found: MarkerRangeWithMessage[] = tryToParse(text);
|
||||||
|
|
||||||
const {context} = opts;
|
const {context} = opts;
|
||||||
|
|
||||||
try {
|
|
||||||
parse(text);
|
|
||||||
}
|
|
||||||
catch (err: any) {
|
|
||||||
|
|
||||||
const errorMatch = err.toString().match(/line (\d+), column (\d+)/);
|
|
||||||
if (errorMatch) {
|
|
||||||
const loc = {
|
|
||||||
first_line: parseInt(errorMatch[1], 10),
|
|
||||||
first_column: parseInt(errorMatch[2], 10),
|
|
||||||
last_line: parseInt(errorMatch[1], 10),
|
|
||||||
last_column: parseInt(errorMatch[2], 10)
|
|
||||||
};
|
|
||||||
|
|
||||||
// const loc = hash.loc;
|
|
||||||
found.push({
|
|
||||||
from: CodeMirror.Pos(loc.first_line - 1, loc.first_column),
|
|
||||||
to: CodeMirror.Pos(loc.last_line - 1, loc.last_column),
|
|
||||||
message: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (found.length > 0) {
|
if (found.length > 0) {
|
||||||
// JSON invalid so don't go any further
|
// JSON invalid so don't go any further
|
||||||
return found;
|
return found;
|
||||||
|
|||||||
Reference in New Issue
Block a user