Skip to content
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

Suggestion to parse the error #4

Open
ficristo opened this issue Jan 4, 2016 · 2 comments
Open

Suggestion to parse the error #4

ficristo opened this issue Jan 4, 2016 · 2 comments

Comments

@ficristo
Copy link

ficristo commented Jan 4, 2016

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
Copy link
Author

@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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants