Skip to content

Commit

Permalink
refine parse mode by media type
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Sep 20, 2024
1 parent e165747 commit 2107f08
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ enum ParseMode {
Script,
}

fn refine_parse_mode(
parse_mode: ParseMode,
media_type: MediaType,
) -> ParseMode {
match (parse_mode, media_type) {
(ParseMode::Program, MediaType::Cjs) => ParseMode::Script,
(ParseMode::Program, MediaType::Cts) => ParseMode::Script,
(ParseMode::Program, MediaType::Mjs) => ParseMode::Module,
(ParseMode::Program, MediaType::Mts) => ParseMode::Module,
(parse_mode, _) => parse_mode,
}
}

fn parse(
params: ParseParams,
parse_mode: ParseMode,
Expand All @@ -148,6 +161,7 @@ fn parse(
let syntax = params
.maybe_syntax
.unwrap_or_else(|| get_syntax(media_type));
let parse_mode = refine_parse_mode(parse_mode, media_type);
let (comments, program, tokens, errors) =
parse_string_input(input, syntax, params.capture_tokens, parse_mode)
.map_err(|err| {
Expand Down

0 comments on commit 2107f08

Please sign in to comment.