@@ -18,9 +18,7 @@ use ruff_python_ast::{Mod, PySourceType};
18
18
use ruff_python_codegen:: Stylist ;
19
19
use ruff_python_formatter:: { format_module_ast, pretty_comments, PyFormatContext , QuoteStyle } ;
20
20
use ruff_python_index:: Indexer ;
21
- use ruff_python_parser:: {
22
- parse, parse_unchecked, parse_unchecked_source, Mode , ParseOptions , Parsed ,
23
- } ;
21
+ use ruff_python_parser:: { parse, parse_unchecked, Mode , ParseOptions , Parsed } ;
24
22
use ruff_python_trivia:: CommentRanges ;
25
23
use ruff_source_file:: SourceLocation ;
26
24
use ruff_text_size:: Ranged ;
@@ -163,8 +161,14 @@ impl Workspace {
163
161
// TODO(dhruvmanila): Support Jupyter Notebooks
164
162
let source_kind = SourceKind :: Python ( contents. to_string ( ) ) ;
165
163
164
+ // Use the unresolved version because we don't have a file path.
165
+ let target_version = self . settings . linter . unresolved_target_version ;
166
+
166
167
// Parse once.
167
- let parsed = parse_unchecked_source ( source_kind. source_code ( ) , source_type) ;
168
+ let options = ParseOptions :: from ( source_type) . with_target_version ( target_version) ;
169
+ let parsed = parse_unchecked ( source_kind. source_code ( ) , options)
170
+ . try_into_module ( )
171
+ . expect ( "`PySourceType` always parses to a `ModModule`." ) ;
168
172
169
173
// Map row and column locations to byte slices (lazily).
170
174
let locator = Locator :: new ( contents) ;
@@ -196,7 +200,7 @@ impl Workspace {
196
200
& source_kind,
197
201
source_type,
198
202
& parsed,
199
- self . settings . linter . unresolved_target_version ,
203
+ target_version ,
200
204
) ;
201
205
202
206
let source_code = locator. to_source_code ( ) ;
@@ -238,6 +242,18 @@ impl Workspace {
238
242
fix : None ,
239
243
}
240
244
} ) )
245
+ . chain ( parsed. unsupported_syntax_errors ( ) . iter ( ) . map ( |error| {
246
+ let start_location = source_code. source_location ( error. range . start ( ) ) ;
247
+ let end_location = source_code. source_location ( error. range . end ( ) ) ;
248
+
249
+ ExpandedMessage {
250
+ code : None ,
251
+ message : format ! ( "SyntaxError: {error}" ) ,
252
+ location : start_location,
253
+ end_location,
254
+ fix : None ,
255
+ }
256
+ } ) )
241
257
. collect ( ) ;
242
258
243
259
serde_wasm_bindgen:: to_value ( & messages) . map_err ( into_error)
0 commit comments