@@ -10,12 +10,14 @@ const STATE = Symbol("espree's internal state");
10
10
const ESPRIMA_FINISH_NODE = Symbol ( "espree's esprimaFinishNode" ) ;
11
11
const tokTypes = Object . assign ( { } , acorn . tokTypes , jsx . tokTypes ) ;
12
12
13
+
13
14
/**
14
15
* Normalize ECMAScript version from the initial config
15
16
* @param {number } ecmaVersion ECMAScript version from the initial config
17
+ * @throws {Error } throws an error if the ecmaVersion is invalid.
16
18
* @returns {number } normalized ECMAScript version
17
19
*/
18
- function normalizeEcmaVersion ( ecmaVersion ) {
20
+ function normalizeEcmaVersion ( ecmaVersion = DEFAULT_ECMA_VERSION ) {
19
21
if ( typeof ecmaVersion === "number" ) {
20
22
let version = ecmaVersion ;
21
23
@@ -35,12 +37,42 @@ function normalizeEcmaVersion(ecmaVersion) {
35
37
case 10 :
36
38
return version ;
37
39
38
- default :
39
- throw new Error ( "Invalid ecmaVersion." ) ;
40
+ // no default
40
41
}
41
- } else {
42
- return DEFAULT_ECMA_VERSION ;
43
42
}
43
+
44
+ throw new Error ( "Invalid ecmaVersion." ) ;
45
+ }
46
+
47
+ /**
48
+ * Normalize sourceType from the initial config
49
+ * @param {string } sourceType to normalize
50
+ * @throws {Error } throw an error if sourceType is invalid
51
+ * @returns {string } normalized sourceType
52
+ */
53
+ function normalizeSourceType ( sourceType = "script" ) {
54
+ if ( sourceType === "script" || sourceType === "module" ) {
55
+ return sourceType ;
56
+ }
57
+ throw new Error ( "Invalid sourceType." ) ;
58
+ }
59
+
60
+ /**
61
+ * Normalize parserOptions
62
+ * @param {Object } options the parser options to normalize
63
+ * @throws {Error } throw an error if found invalid option.
64
+ * @returns {Object } normalized options
65
+ */
66
+ function normalizeOptions ( options ) {
67
+ const ecmaVersion = normalizeEcmaVersion ( options . ecmaVersion ) ;
68
+ const sourceType = normalizeSourceType ( options . sourceType ) ;
69
+ const ranges = options . range === true ;
70
+ const locations = options . loc === true ;
71
+
72
+ if ( sourceType === "module" && ecmaVersion < 6 ) {
73
+ throw new Error ( "sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options." ) ;
74
+ }
75
+ return Object . assign ( { } , options , { ecmaVersion, sourceType, ranges, locations } ) ;
44
76
}
45
77
46
78
/**
@@ -77,28 +109,29 @@ function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc,
77
109
}
78
110
79
111
module . exports = ( ) => Parser => class Espree extends Parser {
80
- constructor ( options , code ) {
81
- if ( typeof options !== "object" || options === null ) {
82
- options = { } ;
112
+ constructor ( opts , code ) {
113
+ if ( typeof opts !== "object" || opts === null ) {
114
+ opts = { } ;
83
115
}
84
116
if ( typeof code !== "string" && ! ( code instanceof String ) ) {
85
117
code = String ( code ) ;
86
118
}
87
119
120
+ const options = normalizeOptions ( opts ) ;
88
121
const ecmaFeatures = options . ecmaFeatures || { } ;
89
- const ecmaVersion = normalizeEcmaVersion ( options . ecmaVersion ) ;
90
- const isModule = options . sourceType === "module" ;
91
122
const tokenTranslator =
92
123
options . tokens === true
93
124
? new TokenTranslator ( tokTypes , code )
94
125
: null ;
95
126
96
127
// Initialize acorn parser.
97
128
super ( {
98
- ecmaVersion : isModule ? Math . max ( 6 , ecmaVersion ) : ecmaVersion ,
99
- sourceType : isModule ? "module" : "script" ,
100
- ranges : options . range === true ,
101
- locations : options . loc === true ,
129
+
130
+ // TODO: use {...options} when spread is supported(Node.js >= 8.3.0).
131
+ ecmaVersion : options . ecmaVersion ,
132
+ sourceType : options . sourceType ,
133
+ ranges : options . ranges ,
134
+ locations : options . locations ,
102
135
103
136
// Truthy value is true for backward compatibility.
104
137
allowReturnOutsideFunction : Boolean ( ecmaFeatures . globalReturn ) ,
0 commit comments