Skip to content

Suggestion to parse the error #4

@ficristo

Description

@ficristo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions