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 #7

Closed
ficristo opened this issue Jan 4, 2016 · 1 comment
Closed

Suggestion to parse the error #7

ficristo opened this issue Jan 4, 2016 · 1 comment

Comments

@ficristo
Copy link
Contributor

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;
}

FWIW I filed a similar issue at ingorichter/de.richter.brackets.jsonlint#4

@stephenkeep
Copy link
Owner

hey @ficristo im happy to accept any pull requests

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