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