1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const 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 |
|
25 |
|
26 |
|
27 | export function getLatestEcmaVersion() {
|
28 | return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1];
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export function getSupportedEcmaVersions() {
|
36 | return [...SUPPORTED_VERSIONS];
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function 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 |
|
54 |
|
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 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function 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 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | export 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 | }
|