1/** 2 * @fileoverview A collection of methods for processing Espree's options. 3 * @author Kai Cataldo 4 */ 5 6"use strict"; 7 8//------------------------------------------------------------------------------ 9// Helpers 10//------------------------------------------------------------------------------ 11 12const DEFAULT_ECMA_VERSION = 5; 13const SUPPORTED_VERSIONS = [ 14 3, 15 5, 16 6, 17 7, 18 8, 19 9, 20 10, 21 11, 22 12 23]; 24 25/** 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 30 */ 31function 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.`); 34 } 35 36 let version = ecmaVersion; 37 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) { 41 version -= 2009; 42 } 43 44 if (!SUPPORTED_VERSIONS.includes(version)) { 45 throw new Error("Invalid ecmaVersion."); 46 } 47 48 return version; 49} 50 51/** 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 56 */ 57function normalizeSourceType(sourceType = "script") { 58 if (sourceType === "script" || sourceType === "module") { 59 return sourceType; 60 } 61 throw new Error("Invalid sourceType."); 62} 63 64/** 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 69 */ 70function 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; 75 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."); 78 } 79 return Object.assign({}, options, { ecmaVersion, sourceType, ranges, locations }); 80} 81 82/** 83 * Get the latest ECMAScript version supported by Espree. 84 * @returns {number} The latest ECMAScript version. 85 */ 86function getLatestEcmaVersion() { 87 return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1]; 88} 89 90/** 91 * Get the list of ECMAScript versions supported by Espree. 92 * @returns {number[]} An array containing the supported ECMAScript versions. 93 */ 94function getSupportedEcmaVersions() { 95 return [...SUPPORTED_VERSIONS]; 96} 97 98//------------------------------------------------------------------------------ 99// Public 100//------------------------------------------------------------------------------ 101 102module.exports = { 103 normalizeOptions, 104 getLatestEcmaVersion, 105 getSupportedEcmaVersions 106}; 107