Skip to content

Commit 8fa67f9

Browse files
committed
Add JSON syntax linting to CodeMirror v6 editor
1 parent f5c1710 commit 8fa67f9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

client/modules/IDE/components/Editor/stateUtils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,34 @@ function makeJsLinter(callback) {
230230
};
231231
}
232232

233+
function makeJsonLinter(callback) {
234+
return (view) => {
235+
const documentContent = view.state.doc.toString();
236+
const diagnostics = [];
237+
238+
try {
239+
JSON.parse(documentContent);
240+
} catch (e) {
241+
let pos = 0;
242+
const match = e.message.match(/at position (\d+)/);
243+
if (match) {
244+
pos = parseInt(match[1], 10);
245+
}
246+
247+
diagnostics.push({
248+
from: pos,
249+
to: pos + 1,
250+
severity: 'error',
251+
message: e.message
252+
});
253+
}
254+
255+
if (callback) callback(diagnostics);
256+
257+
return diagnostics;
258+
};
259+
}
260+
233261
function getFileLinter(fileName, callback) {
234262
const fileMode = getFileMode(fileName);
235263

@@ -240,6 +268,8 @@ function getFileLinter(fileName, callback) {
240268
return linter(makeHtmlLinter(callback));
241269
case 'css':
242270
return linter(makeCssLinter(callback));
271+
case 'application/json':
272+
return linter(makeJsonLinter(callback));
243273
default:
244274
return null;
245275
}

0 commit comments

Comments
 (0)