UNPKG

3.31 kBJavaScriptView Raw
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,
14 7,
15 8,
16 9,
17 10,
18 11,
19 12,
20 13
21];
22
23/**
24 * Get the latest ECMAScript version supported by Espree.
25 * @returns {number} The latest ECMAScript version.
26 */
27export function getLatestEcmaVersion() {
28 return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1];
29}
30
31/**
32 * Get the list of ECMAScript versions supported by Espree.
33 * @returns {number[]} An array containing the supported ECMAScript versions.
34 */
35export function getSupportedEcmaVersions() {
36 return [...SUPPORTED_VERSIONS];
37}
38
39/**
40 * Normalize ECMAScript version from the initial config
41 * @param {(number|"latest")} ecmaVersion ECMAScript version from the initial config
42 * @throws {Error} throws an error if the ecmaVersion is invalid.
43 * @returns {number} normalized ECMAScript version
44 */
45function normalizeEcmaVersion(ecmaVersion = 5) {
46
47 let version = ecmaVersion === "latest" ? getLatestEcmaVersion() : ecmaVersion;
48
49 if (typeof version !== "number") {
50 throw new Error(`ecmaVersion must be a number or "latest". Received value of type ${typeof ecmaVersion} instead.`);
51 }
52
53 // Calculate ECMAScript edition number from official year version starting with
54 // ES2015, which corresponds with ES6 (or a difference of 2009).
55 if (version >= 2015) {
56 version -= 2009;
57 }
58
59 if (!SUPPORTED_VERSIONS.includes(version)) {
60 throw new Error("Invalid ecmaVersion.");
61 }
62
63 return version;
64}
65
66/**
67 * Normalize sourceType from the initial config
68 * @param {string} sourceType to normalize
69 * @throws {Error} throw an error if sourceType is invalid
70 * @returns {string} normalized sourceType
71 */
72function normalizeSourceType(sourceType = "script") {
73 if (sourceType === "script" || sourceType === "module") {
74 return sourceType;
75 }
76
77 if (sourceType === "commonjs") {
78 return "script";
79 }
80
81 throw new Error("Invalid sourceType.");
82}
83
84/**
85 * Normalize parserOptions
86 * @param {Object} options the parser options to normalize
87 * @throws {Error} throw an error if found invalid option.
88 * @returns {Object} normalized options
89 */
90export function normalizeOptions(options) {
91 const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
92 const sourceType = normalizeSourceType(options.sourceType);
93 const ranges = options.range === true;
94 const locations = options.loc === true;
95 const allowReserved = ecmaVersion === 3 ? "never" : false;
96 const ecmaFeatures = options.ecmaFeatures || {};
97 const allowReturnOutsideFunction = options.sourceType === "commonjs" ||
98 Boolean(ecmaFeatures.globalReturn);
99
100 if (sourceType === "module" && ecmaVersion < 6) {
101 throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options.");
102 }
103
104 return Object.assign({}, options, {
105 ecmaVersion,
106 sourceType,
107 ranges,
108 locations,
109 allowReserved,
110 allowReturnOutsideFunction
111 });
112}