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

New: options.recoverableErrors (eslint/rfcs#19) #417

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ function tokenize(code, options) {
*/
function parse(code, options) {
const Parser = parsers.get(options);
const parser = new Parser(options, code);
const ast = parser.parse();
const recoverableErrors = parser.recoverableErrors;

return new Parser(options, code).parse();
return { ast, recoverableErrors };
}

//------------------------------------------------------------------------------
Expand All @@ -141,7 +144,9 @@ exports.version = require("./package.json").version;

exports.tokenize = tokenize;

exports.parse = parse;
exports.parseForESLint = parse;

exports.parse = (code, options) => parse(code, options).ast;

// Deep copy.
/* istanbul ignore next */
Expand Down
44 changes: 35 additions & 9 deletions lib/espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,36 @@ function normalizeOptions(options) {
const sourceType = normalizeSourceType(options.sourceType);
const ranges = options.range === true;
const locations = options.loc === true;
const recoverableErrors = options.recoverableErrors === true;

if (sourceType === "module" && ecmaVersion < 6) {
throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options.");
}
return Object.assign({}, options, { ecmaVersion, sourceType, ranges, locations });
return Object.assign(
{},
options,
{ ecmaVersion, sourceType, ranges, locations, recoverableErrors }
);
}

/**
* Create a new syntax error object.
* @param {string} input The source code.
* @param {number} pos The location the error happened.
* @param {string} message The error message.
* @param {SyntaxError[]} recoverableErrors The recovered errors.
* @returns {SyntaxError} The syntax error object.
*/
function createSyntaxError(input, pos, message, recoverableErrors) {
const loc = acorn.getLineInfo(input, pos);
const err = new SyntaxError(message);

err.index = pos;
err.lineNumber = loc.line;
err.column = loc.column + 1; // acorn uses 0-based columns
err.recoverableErrors = recoverableErrors;

return err;
}

/**
Expand Down Expand Up @@ -167,6 +192,9 @@ module.exports = () => Parser => class Espree extends Parser {
jsxAttrValueToken: false,
lastToken: null
};

// Public.
this.recoverableErrors = options.recoverableErrors ? [] : void 0;
}

tokenize() {
Expand Down Expand Up @@ -243,13 +271,7 @@ module.exports = () => Parser => class Espree extends Parser {
* @returns {void}
*/
raise(pos, message) {
const loc = acorn.getLineInfo(this.input, pos);
const err = new SyntaxError(message);

err.index = pos;
err.lineNumber = loc.line;
err.column = loc.column + 1; // acorn uses 0-based columns
throw err;
throw createSyntaxError(this.input, pos, message, this.recoverableErrors);
}

/**
Expand All @@ -260,7 +282,11 @@ module.exports = () => Parser => class Espree extends Parser {
* @returns {void}
*/
raiseRecoverable(pos, message) {
this.raise(pos, message);
if (this.recoverableErrors) {
this.recoverableErrors.push(createSyntaxError(this.input, pos, message));
} else {
this.raise(pos, message);
}
}

/**
Expand Down