-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Update: add latestEcmaVersion & supportedEcmaVersions #430
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* @fileoverview A collection of methods for processing Espree's options. | ||
* @author Kai Cataldo | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
const DEFAULT_ECMA_VERSION = 5; | ||
const SUPPORTED_VERSIONS = [ | ||
3, | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
10, | ||
11 | ||
]; | ||
|
||
/** | ||
* 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 = DEFAULT_ECMA_VERSION) { | ||
if (typeof ecmaVersion !== "number") { | ||
throw new Error(`ecmaVersion must be a number. Received value of type ${typeof ecmaVersion} instead.`); | ||
} | ||
|
||
let version = ecmaVersion; | ||
|
||
// Calculate ECMAScript edition number from official year version starting with | ||
// ES2015, which corresponds with ES6 (or a difference of 2009). | ||
if (version >= 2015) { | ||
version -= 2009; | ||
} | ||
|
||
if (!SUPPORTED_VERSIONS.includes(version)) { | ||
throw new Error("Invalid ecmaVersion."); | ||
} | ||
|
||
return version; | ||
} | ||
|
||
/** | ||
* 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 < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options."); | ||
} | ||
return Object.assign({}, options, { ecmaVersion, sourceType, ranges, locations }); | ||
} | ||
|
||
/** | ||
* Get the latest ECMAScript version supported by Espree. | ||
* @returns {number} The latest ECMAScript version. | ||
*/ | ||
function latestEcmaVersion() { | ||
return Math.max(...SUPPORTED_VERSIONS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can avoid a calculation here by using SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1] |
||
} | ||
|
||
/** | ||
* Get the list of ECMAScript versions supported by Espree. | ||
* @returns {number[]} An array containing the supported ECMAScript versions. | ||
*/ | ||
function supportedEcmaVersions() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, getSupportedEcmaVersions. |
||
return [...SUPPORTED_VERSIONS]; | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Public | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
normalizeOptions, | ||
latestEcmaVersion, | ||
supportedEcmaVersions | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @fileoverview Tests for latestEcmaVersion() & supportedEcmaVersions(). | ||
* @author Kai Cataldo | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const assert = require("assert"), | ||
espree = require("../../espree"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
describe("latestEcmaVersion()", () => { | ||
it("should return the latest supported ecmaVersion", () => { | ||
assert.strictEqual(espree.latestEcmaVersion(), 11); | ||
}); | ||
}); | ||
|
||
describe("supportedEcmaVersions()", () => { | ||
it("should return an array of all supported versions", () => { | ||
assert.deepStrictEqual( | ||
espree.supportedEcmaVersions(), | ||
[3, 5, 6, 7, 8, 9, 10, 11] | ||
); | ||
}); | ||
|
||
it("the array of supported versions should not be mutable by reference", () => { | ||
const supportedVersions = espree.supportedEcmaVersions(); | ||
const originalValue = [...supportedVersions]; | ||
|
||
supportedVersions.push("a", "b", "c"); | ||
assert.deepStrictEqual(espree.supportedEcmaVersions(), originalValue); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d suggest calling this getLatestEcmaVersion to indicate a value is being retrieved (verb as opposed to noun).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! I was initially thinking of it being more of a static property (like
version
), but that makes more sense with this iteration 👍