-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use JSON Linting #21
Comments
This should work fine, I just tested it locally. I am suspecting you are not getting through the setup correctly. The old repo had a similar issue seen here JedWatson/react-codemirror#89 I made sure to include all the necessary files, which, on my end consisted of... require('codemirror/addon/lint/lint');
require('codemirror/addon/lint/json-lint'); @import 'codemirror/addon/lint/lint.css'; I was then presented with an error in the browser...
Apparently we need these libs at runtime. So I added the following... window.jsonlint = require('jsonlint'); I tested this with |
I use the same imports as you mentioned. After import jsonlint the error is gone but still no linting. maybe I do something wrong with the jsonlint import. I guess I'll try again with minimal example. thx for help! |
I had a lot of weirdness going on with requiring jsonlint with webpack, similar to what's going on here zaach/jsonlint#57. You might need to explore a little bit to get around this. Let me know what you find! |
@scniro I tested with JSHINT and the linter works, but with jsonlint does not work
|
the problem here is not the jsonlint in my opinion but that it wont show linting errors in codemirror.
|
@slobaton I tried with After digging into associated codes, I found a workout. There's two steps to solve this:
var CodeMirror = require('codemirror');
// the 'fork-and-fixed' jsonlint file
window['jsonlint'] = require('./jsonlint');
CodeMirror.registerHelper("lint", "json", function(text) {
var found = [];
if (!window.jsonlint) {
if (window.console) {
window.console.error("Error: window.jsonlint not defined, CodeMirror JSON linting cannot run.");
}
return found;
}
var jsonlint = window.jsonlint.parser;
jsonlint.parseError = function(str, hash) {
var 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: str});
};
try { jsonlint.parse(text); }
catch(e) {}
return found;
}); |
codemirror/codemirror5#5377 @aprilandjan Also if you want to require jsonlint, there's a fork of jsonlint that works (as jsonlint is unmaintained) -> jsonlint-mod |
@aprilandjan great solution! |
i went into this as well, and solution from @aprilandjan works. codemirror shall fix this too. |
For anyone looking at this today, looks like codemirror still uses the non fixed version of const jsonlint = require("jsonlint-mod");
window.jsonlint = jsonlint;
const cmOption = {
mode: 'application/json',
gutters: ["CodeMirror-lint-markers"],
styleActiveLine: true,
lineNumbers: true,
line: true,
lint: true,
}; Nothing else required. I dislike having to put Edit caveat: I haven't been able to get the duplicate keys error to display a sidebar warning, but it is running the jsonlint-mod code. |
|
In vanilla JS this works
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
lineNumbers: true,
styleActiveLine: true,
theme: "material",
mode: "application/json",
gutters: ["CodeMirror-lint-markers"],
lint: true
}); |
import 'codemirror/addon/lint/lint.js'
import 'codemirror/addon/lint/lint.css'
import 'codemirror/addon/lint/javascript-lint.js'
import 'codemirror/addon/hint/javascript-hint.js'
window.JSHINT = JSHINT;
const options = {
mode: 'application/json',
lint: true,
gutters: [ "CodeMirror-lint-markers"],
} |
Hi,
could you maybe point me to a solution how to use json linting in react-codemirror2?
When i use mode javascript i get error highlighting in code but no gutters. This works with the react-codemirror 1 version. But as r_cm1 is outdated i would like to use this version.
is use the following option set
options={{
mode: 'application/json',
theme: 'material',
lineNumbers: true,
lineWrapping: true,
lineHeight: 10,
lint: true,
gutters: ["CodeMirror-lint-markers"],
}}
thanks in advance
Greetings Sascha
The text was updated successfully, but these errors were encountered: