Skip to content

Commit

Permalink
Update: add support for ecmaVersion 20xx (fixes #6750) (#6907)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo authored and btmills committed Aug 16, 2016
1 parent d8b770c commit 1563808
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
32 changes: 26 additions & 6 deletions lib/eslint.js
Expand Up @@ -406,6 +406,28 @@ function isDisabledByReportingConfig(reportingConfig, ruleId, location) {
return false;
}

/**
* Normalize ECMAScript version from the initial config
* @param {number} ecmaVersion ECMAScript version from the initial config
* @param {boolean} isModule Whether the source type is module or not
* @returns {number} normalized ECMAScript version
*/
function normalizeEcmaVersion(ecmaVersion, isModule) {

// Need at least ES6 for modules
if (isModule && (!ecmaVersion || ecmaVersion < 6)) {
ecmaVersion = 6;
}

// Calculate ECMAScript edition number from official year version starting with
// ES2015, which corresponds with ES6 (or a difference of 2009).
if (ecmaVersion >= 2015) {
ecmaVersion -= 2009;
}

return ecmaVersion;
}

/**
* Process initial config to make it safe to extend by file comment config
* @param {Object} config Initial config
Expand Down Expand Up @@ -453,21 +475,19 @@ function prepareConfig(config) {
settings: ConfigOps.merge({}, config.settings || {}),
parserOptions: ConfigOps.merge(parserOptions, config.parserOptions || {})
};
const isModule = preparedConfig.parserOptions.sourceType === "module";

if (preparedConfig.parserOptions.sourceType === "module") {
if (isModule) {
if (!preparedConfig.parserOptions.ecmaFeatures) {
preparedConfig.parserOptions.ecmaFeatures = {};
}

// can't have global return inside of modules
preparedConfig.parserOptions.ecmaFeatures.globalReturn = false;

// also need at least ES6 for modules
if (!preparedConfig.parserOptions.ecmaVersion || preparedConfig.parserOptions.ecmaVersion < 6) {
preparedConfig.parserOptions.ecmaVersion = 6;
}
}

preparedConfig.parserOptions.ecmaVersion = normalizeEcmaVersion(preparedConfig.parserOptions.ecmaVersion, isModule);

return preparedConfig;
}

Expand Down
63 changes: 52 additions & 11 deletions tests/lib/eslint.js
Expand Up @@ -3052,7 +3052,6 @@ describe("eslint", function() {
});

describe("verify()", function() {

describe("filenames", function() {
it("should allow filename to be passed on options object", function() {

Expand Down Expand Up @@ -3095,11 +3094,8 @@ describe("eslint", function() {
eslint.verify("foo;", {}, { saveState: true });
assert.equal(spy.callCount, 0);
});


});


it("should report warnings in order by line and column when called", function() {

const code = "foo()\n alert('test')";
Expand All @@ -3116,15 +3112,60 @@ describe("eslint", function() {
assert.equal(messages[2].column, 18);
});

it("should properly parse let declaration when passed ecmaVersion", function() {
describe("ecmaVersion", function() {
describe("it should properly parse let declaration when", function() {
it("the ECMAScript version number is 6", function() {
const messages = eslint.verify("let x = 5;", {
parserOptions: {
ecmaVersion: 6
}
});

const messages = eslint.verify("let x = 5;", {
parserOptions: {
ecmaVersion: 6
}
}, filename);
assert.equal(messages.length, 0);
});

assert.equal(messages.length, 0);
it("the ECMAScript version number is 2015", function() {
const messages = eslint.verify("let x = 5;", {
parserOptions: {
ecmaVersion: 2015
}
});

assert.equal(messages.length, 0);
});
});

it("should fail to parse exponentiation operator when the ECMAScript version number is 2015", function() {
const messages = eslint.verify("x ** y;", {
parserOptions: {
ecmaVersion: 2015
}
});

assert.equal(messages.length, 1);
});

describe("should properly parse exponentiation operator when", function() {
it("the ECMAScript version number is 7", function() {
const messages = eslint.verify("x ** y;", {
parserOptions: {
ecmaVersion: 7
}
});

assert.equal(messages.length, 0);
});

it("the ECMAScript version number is 2016", function() {
const messages = eslint.verify("x ** y;", {
parserOptions: {
ecmaVersion: 2016
}
});

assert.equal(messages.length, 0);
});
});
});

it("should properly parse object spread when passed ecmaFeatures", function() {
Expand Down

0 comments on commit 1563808

Please sign in to comment.