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

Breaking: validate parser options (fixes #384) #412

Merged
merged 9 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions lib/espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const STATE = Symbol("espree's internal state");
const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
const tokTypes = Object.assign({}, acorn.tokTypes, jsx.tokTypes);


/**
* Normalize ECMAScript version from the initial config
* @param {number} ecmaVersion ECMAScript version from the initial config
* @throws {Error} throws an error if the ecmaVersion is invalid.
* @returns {number} normalized ECMAScript version
*/
function normalizeEcmaVersion(ecmaVersion) {
function normalizeEcmaVersion(ecmaVersion = DEFAULT_ECMA_VERSION) {
if (typeof ecmaVersion === "number") {
let version = ecmaVersion;

Expand All @@ -36,11 +38,41 @@ function normalizeEcmaVersion(ecmaVersion) {
return version;

default:
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Invalid ecmaVersion.");
}
} else {
return DEFAULT_ECMA_VERSION;
}

throw new Error("Invalid ecmaVersion.");
}

/**
* Normalize sourceType from the initial config
* @param {string} sourceType to normalize
* @throws {Error} throw an error if sourceType is invalid
* @returns {string} normalized sourceType
*/
function normalizeSourceType(sourceType = "script") {
if (sourceType === "script" || sourceType === "module") {
return sourceType;
}
throw new Error("Invalid sourceType.");
}

/**
* Normalize parserOptions
* @param {Object} options the parser options to normalize
* @throws {Error} throw an error if found invalid option.
* @returns {Object} normalized options
*/
function normalizeOptions(options) {
const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
const sourceType = normalizeSourceType(options.sourceType);
const ranges = options.range === true;
const locations = options.loc === true;

if (sourceType === "module" && ecmaVersion < 6) {
throw new Error("sourceType 'module' is not supported when ecmaVersion < 6.");
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
}
return Object.assign({}, options, { ecmaVersion, sourceType, ranges, locations });
}

/**
Expand Down Expand Up @@ -77,28 +109,29 @@ function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc,
}

module.exports = () => Parser => class Espree extends Parser {
constructor(options, code) {
if (typeof options !== "object" || options === null) {
options = {};
constructor(opts, code) {
if (typeof opts !== "object" || opts === null) {
opts = {};
}
if (typeof code !== "string" && !(code instanceof String)) {
code = String(code);
}

const options = normalizeOptions(opts);
const ecmaFeatures = options.ecmaFeatures || {};
const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
const isModule = options.sourceType === "module";
const tokenTranslator =
options.tokens === true
? new TokenTranslator(tokTypes, code)
: null;

// Initialize acorn parser.
super({
ecmaVersion: isModule ? Math.max(6, ecmaVersion) : ecmaVersion,
sourceType: isModule ? "module" : "script",
ranges: options.range === true,
locations: options.loc === true,

// TODO: use {...options} when spread is supported(Node.js >= 8.3.0).
ecmaVersion: options.ecmaVersion,
sourceType: options.sourceType,
ranges: options.ranges,
locations: options.locations,

// Truthy value is true for backward compatibility.
allowReturnOutsideFunction: Boolean(ecmaFeatures.globalReturn),
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/ecma-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ describe("ecmaVersion", () => {
}, /Invalid ecmaVersion/);
});

it("Should throw error when non-numeric year is provided", () => {
assert.throws(() => {
espree.parse(
"let foo = bar;", {
ecmaVersion: "2015",
comment: true,
tokens: true,
range: true,
loc: true
}
);
}, /Invalid ecmaVersion/);
});

it("Should throw error when using module in pre-ES6", () => {
assert.throws(() => {
espree.parse(
"let foo = bar;", {
ecmaVersion: 5,
sourceType: "module"
}
);
}, /sourceType 'module' is not supported when ecmaVersion < 6/);
});
});

});
2 changes: 1 addition & 1 deletion tests/lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("parse()", () => {
it("should have correct column number when strict mode error occurs", () => {

try {
espree.parse("function fn(a, a) {\n}", { sourceType: "module" });
espree.parse("function fn(a, a) {\n}", { ecmaVersion: 6, sourceType: "module" });
} catch (err) {
assert.strictEqual(err.column, 16);
}
Expand Down