@@ -36,7 +36,7 @@ function keysFromParser(parserPath, parserInstance, parsedResult) {
36
36
if ( typeof parserPath === 'string' && ( / .* b a b e l - e s l i n t .* / ) . test ( parserPath ) ) {
37
37
return getBabelEslintVisitorKeys ( parserPath ) ;
38
38
}
39
- // The espree parser doesn't have the `parseForESLint` function, so we don't ended up with a
39
+ // The espree parser doesn't have the `parseForESLint` function, so we don't end up with a
40
40
// `parsedResult` here, but it does expose the visitor keys on the parser instance that we can use.
41
41
if ( parserInstance && parserInstance . VisitorKeys ) {
42
42
return parserInstance . VisitorKeys ;
@@ -113,7 +113,8 @@ exports.default = function parse(path, content, context) {
113
113
if ( context == null ) { throw new Error ( 'need context to parse properly' ) ; }
114
114
115
115
// ESLint in "flat" mode only sets context.languageOptions.parserOptions
116
- let parserOptions = context . languageOptions && context . languageOptions . parserOptions || context . parserOptions ;
116
+ const languageOptions = context . languageOptions ;
117
+ let parserOptions = languageOptions && languageOptions . parserOptions || context . parserOptions ;
117
118
const parserOrPath = getParser ( path , context ) ;
118
119
119
120
if ( ! parserOrPath ) { throw new Error ( 'parserPath or languageOptions.parser is required!' ) ; }
@@ -144,6 +145,17 @@ exports.default = function parse(path, content, context) {
144
145
delete parserOptions . project ;
145
146
delete parserOptions . projects ;
146
147
148
+ // If this is a flat config, we need to add ecmaVersion and sourceType (if present) from languageOptions
149
+ if ( languageOptions && languageOptions . ecmaVersion ) {
150
+ parserOptions . ecmaVersion = languageOptions . ecmaVersion ;
151
+ }
152
+ if ( languageOptions && languageOptions . sourceType ) {
153
+ // @ts -expect-error languageOptions is from the flatConfig Linter type in 8.57 while parserOptions is not.
154
+ // Non-flat config parserOptions.sourceType doesn't have "commonjs" in the type. Once upgraded to v9 types,
155
+ // they'll be the same and this expect-error should be removed.
156
+ parserOptions . sourceType = languageOptions . sourceType ;
157
+ }
158
+
147
159
// require the parser relative to the main module (i.e., ESLint)
148
160
const parser = typeof parserOrPath === 'string' ? moduleRequire ( parserOrPath ) : parserOrPath ;
149
161
0 commit comments