2 * @fileoverview A collection of methods for processing Espree's options.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const DEFAULT_ECMA_VERSION = 5;
13 const SUPPORTED_VERSIONS = [
26 * Normalize ECMAScript version from the initial config
27 * @param {number} ecmaVersion ECMAScript version from the initial config
28 * @throws {Error} throws an error if the ecmaVersion is invalid.
29 * @returns {number} normalized ECMAScript version
31 function normalizeEcmaVersion(ecmaVersion = DEFAULT_ECMA_VERSION) {
32 if (typeof ecmaVersion !== "number") {
33 throw new Error(`ecmaVersion must be a number. Received value of type ${typeof ecmaVersion} instead.`);
36 let version = ecmaVersion;
38 // Calculate ECMAScript edition number from official year version starting with
39 // ES2015, which corresponds with ES6 (or a difference of 2009).
40 if (version >= 2015) {
44 if (!SUPPORTED_VERSIONS.includes(version)) {
45 throw new Error("Invalid ecmaVersion.");
52 * Normalize sourceType from the initial config
53 * @param {string} sourceType to normalize
54 * @throws {Error} throw an error if sourceType is invalid
55 * @returns {string} normalized sourceType
57 function normalizeSourceType(sourceType = "script") {
58 if (sourceType === "script" || sourceType === "module") {
61 throw new Error("Invalid sourceType.");
65 * Normalize parserOptions
66 * @param {Object} options the parser options to normalize
67 * @throws {Error} throw an error if found invalid option.
68 * @returns {Object} normalized options
70 function normalizeOptions(options) {
71 const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
72 const sourceType = normalizeSourceType(options.sourceType);
73 const ranges = options.range === true;
74 const locations = options.loc === true;
76 if (sourceType === "module" && ecmaVersion < 6) {
77 throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options.");
79 return Object.assign({}, options, { ecmaVersion, sourceType, ranges, locations });
83 * Get the latest ECMAScript version supported by Espree.
84 * @returns {number} The latest ECMAScript version.
86 function getLatestEcmaVersion() {
87 return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1];
91 * Get the list of ECMAScript versions supported by Espree.
92 * @returns {number[]} An array containing the supported ECMAScript versions.
94 function getSupportedEcmaVersions() {
95 return [...SUPPORTED_VERSIONS];
98 //------------------------------------------------------------------------------
100 //------------------------------------------------------------------------------
104 getLatestEcmaVersion,
105 getSupportedEcmaVersions