Sha256: 2c4dabfdf94bfe70e2ef4548a37ede264001b65eba02694cd0685f4f4efcb7ea

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

var esprima = require('esprima');

module.exports = function (src, file) {
    if (typeof src !== 'string') src = String(src);
    
    try {
        Function(src);
        return;
    }
    catch (err) {
        if (err.constructor.name !== 'SyntaxError') throw err;
        return errorInfo(src, file);
    }
};

function errorInfo (src, file) {
    try {
        esprima.parse(src);
        return;
    }
    catch (err) {
        return new ParseError(err, src, file);
    }
}

function ParseError (err, src, file) {
    SyntaxError.call(this);
    
    this.message = err.message.replace(/^Line \d+: /, '');
    
    this.line = err.lineNumber;
    this.column = err.column;
    
    this.annotated = '\n'
        + (file || '(anonymous file)')
        + ':' + this.line
        + '\n'
        + src.split('\n')[this.line - 1]
        + '\n'
        + Array(this.column).join(' ') + '^'
        + '\n'
        + 'ParseError: ' + this.message
    ;
}

ParseError.prototype = new SyntaxError;

ParseError.prototype.toString = function () {
    return this.annotated;
};

ParseError.prototype.inspect = function () {
    return this.annotated;
};

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
snowball-0.1.22 node_modules/browserify/node_modules/syntax-error/index.js
sprockets-browserify-0.1.2 node_modules/browserify/node_modules/syntax-error/index.js
sprockets-browserify-0.1.0 node_modules/browserify/node_modules/syntax-error/index.js