ingorichter / de.richter.brackets.jsonlint

JSONLint Extension for Brackets
MIT License
5 stars 1 forks source link

Suggestion to parse the error #4

Open ficristo opened 8 years ago

ficristo commented 8 years ago

In the past days I was trying to write a brackets extension to lint json for myself. For it I used https://github.com/zaach/jsonlint too and I was able to overwrite the error to have something more useful for me. Since I used your extension and saw you do some custom parsing I decided to file this issue. I'm not sure it is correct or documented but still something I think is useful. (I changed a bit my code so I'm not sure everything is still correct)

linter.js

var jsonlint = require('jsonlint');
var parser = jsonlint.parser;
parser.parseError = parser.lexer.parseError = function (str, hash) {
    var err = new Error(str);
    err.line = hash.loc.first_line;
    err.col = hash.loc.last_column;
    err.token = hash.token;
    err.expected = hash.expected;
    throw err;
};

linterDomain.js

function cmdLintFile(text, fullPath, options) {
    try {
        jsonlinter.parse(text, options);
    } catch (e) {
        var message = 'Expecting: ' + e.expected.join(' or ') + '. Found: \'' + e.token + '\'.';
        var error = {
            pos: {
                line: e.line - 1,
                ch: e.col
            },
            message: message,
            type: domainName + '.type'
        };

        error.e = e;

        var remapped = { errors: [error] };
        return remapped;
    }
    return null;
}
ficristo commented 8 years ago

@ingorichter I realized that your extension parses better some errors, especially if the json is all in one line (or so it seemed to me). So if you aren't interested feel free to close this.

ingorichter commented 8 years ago

@ficristo thanks for the suggestion. I'll try it out and see how it looks like. Thank you!