source: imaps-frontend/node_modules/espree/lib/options.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/**
2 * @fileoverview A collection of methods for processing Espree's options.
3 * @author Kai Cataldo
4 */
5
6//------------------------------------------------------------------------------
7// Helpers
8//------------------------------------------------------------------------------
9
10const SUPPORTED_VERSIONS = [
11 3,
12 5,
13 6, // 2015
14 7, // 2016
15 8, // 2017
16 9, // 2018
17 10, // 2019
18 11, // 2020
19 12, // 2021
20 13, // 2022
21 14, // 2023
22 15 // 2024
23];
24
25/**
26 * Get the latest ECMAScript version supported by Espree.
27 * @returns {number} The latest ECMAScript version.
28 */
29export function getLatestEcmaVersion() {
30 return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1];
31}
32
33/**
34 * Get the list of ECMAScript versions supported by Espree.
35 * @returns {number[]} An array containing the supported ECMAScript versions.
36 */
37export function getSupportedEcmaVersions() {
38 return [...SUPPORTED_VERSIONS];
39}
40
41/**
42 * Normalize ECMAScript version from the initial config
43 * @param {(number|"latest")} ecmaVersion ECMAScript version from the initial config
44 * @throws {Error} throws an error if the ecmaVersion is invalid.
45 * @returns {number} normalized ECMAScript version
46 */
47function normalizeEcmaVersion(ecmaVersion = 5) {
48
49 let version = ecmaVersion === "latest" ? getLatestEcmaVersion() : ecmaVersion;
50
51 if (typeof version !== "number") {
52 throw new Error(`ecmaVersion must be a number or "latest". Received value of type ${typeof ecmaVersion} instead.`);
53 }
54
55 // Calculate ECMAScript edition number from official year version starting with
56 // ES2015, which corresponds with ES6 (or a difference of 2009).
57 if (version >= 2015) {
58 version -= 2009;
59 }
60
61 if (!SUPPORTED_VERSIONS.includes(version)) {
62 throw new Error("Invalid ecmaVersion.");
63 }
64
65 return version;
66}
67
68/**
69 * Normalize sourceType from the initial config
70 * @param {string} sourceType to normalize
71 * @throws {Error} throw an error if sourceType is invalid
72 * @returns {string} normalized sourceType
73 */
74function normalizeSourceType(sourceType = "script") {
75 if (sourceType === "script" || sourceType === "module") {
76 return sourceType;
77 }
78
79 if (sourceType === "commonjs") {
80 return "script";
81 }
82
83 throw new Error("Invalid sourceType.");
84}
85
86/**
87 * Normalize parserOptions
88 * @param {Object} options the parser options to normalize
89 * @throws {Error} throw an error if found invalid option.
90 * @returns {Object} normalized options
91 */
92export function normalizeOptions(options) {
93 const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
94 const sourceType = normalizeSourceType(options.sourceType);
95 const ranges = options.range === true;
96 const locations = options.loc === true;
97
98 if (ecmaVersion !== 3 && options.allowReserved) {
99
100 // a value of `false` is intentionally allowed here, so a shared config can overwrite it when needed
101 throw new Error("`allowReserved` is only supported when ecmaVersion is 3");
102 }
103 if (typeof options.allowReserved !== "undefined" && typeof options.allowReserved !== "boolean") {
104 throw new Error("`allowReserved`, when present, must be `true` or `false`");
105 }
106 const allowReserved = ecmaVersion === 3 ? (options.allowReserved || "never") : false;
107 const ecmaFeatures = options.ecmaFeatures || {};
108 const allowReturnOutsideFunction = options.sourceType === "commonjs" ||
109 Boolean(ecmaFeatures.globalReturn);
110
111 if (sourceType === "module" && ecmaVersion < 6) {
112 throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options.");
113 }
114
115 return Object.assign({}, options, {
116 ecmaVersion,
117 sourceType,
118 ranges,
119 locations,
120 allowReserved,
121 allowReturnOutsideFunction
122 });
123}
Note: See TracBrowser for help on using the repository browser.