Skip to content

Commit

Permalink
chore: review suggestions
Browse files Browse the repository at this point in the history
1. always normalize ecmaversion
2. add a test: default ecmaversion is 5
  • Loading branch information
aladdin-add committed Jun 3, 2021
1 parent 807ac44 commit 0c4cf30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
16 changes: 7 additions & 9 deletions lib/linter/linter.js
Expand Up @@ -434,17 +434,15 @@ function getDirectiveComments(filename, ast, ruleMapper, warnInlineConfig) {
/**
* Normalize ECMAScript version from the initial config
* @param {number} ecmaVersion ECMAScript version from the initial config
* @param {string} parserName the parser's name
* @returns {number} normalized ECMAScript version
*/
function normalizeEcmaVersion(ecmaVersion, parserName) {
if (parserName === DEFAULT_PARSER_NAME || parserName === void 0) {
if (ecmaVersion === "latest") {
return espree.latestEcmaVersion;
}
if (ecmaVersion === void 0) {
return DEFAULT_ECMA_VERSION;
}
function normalizeEcmaVersion(ecmaVersion) {
if (ecmaVersion === void 0) {
return DEFAULT_ECMA_VERSION;
}

if (ecmaVersion === "latest") {
return espree.latestEcmaVersion;
}

/*
Expand Down
30 changes: 18 additions & 12 deletions tests/lib/linter/linter.js
Expand Up @@ -3425,6 +3425,22 @@ var a = "test2";
});

describe("ecmaVersion", () => {
it("the default ECMAScript version is 5", () => {
const messages = linter.verify("let x = 0;", {});

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

it("supports ECMAScript version 'latest'", () => {
const messages = linter.verify("let x = 5 ** 7;", {
parserOptions: {
ecmaVersion: "latest"
}
});

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

describe("it should properly parse let declaration when", () => {
it("the ECMAScript version number is 6", () => {
const messages = linter.verify("let x = 5;", {
Expand All @@ -3445,16 +3461,6 @@ var a = "test2";

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

it("the ECMAScript version is 'latest'", () => {
const messages = linter.verify("let x = 5 ** 7;", {
parserOptions: {
ecmaVersion: "latest"
}
});

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

it("should fail to parse exponentiation operator when the ECMAScript version number is 2015", () => {
Expand Down Expand Up @@ -5396,11 +5402,11 @@ var a = "test2";
});
});

it("should not pass any default parserOptions to the parser", () => {
it("should pass default ecmaVersion parserOptions to the parser", () => {
linter.defineParser("throws-with-options", testParsers.throwsWithOptions);
const messages = linter.verify(";", { parser: "throws-with-options" }, "filename");

assert.strictEqual(messages.length, 0);
assert.strictEqual(messages.length, 1);
});
});
});

0 comments on commit 0c4cf30

Please sign in to comment.