Repository: swc-project/swc
File: crates/swc_es_parser/src/lib.rs (Rust)

Code before:
    syntax::{EsSyntax, Syntax, TsSyntax},
    token::{Keyword, Token, TokenKind, TokenValue},
};

mod context;
pub mod error;
pub mod lexer;
pub mod parser;
pub mod syntax;
pub mod token;

/// Parses a file as program.
pub fn parse_file_as_program(
    fm: &SourceFile,
    syntax: Syntax,
    comments: Option<&dyn Comments>,
    recovered_errors: &mut Vec<Error>,
) -> PResult<ParsedProgram> {
    let lexer = Lexer::new(syntax, SourceFileInput::from(fm), comments);
    let mut parser = Parser::new_from(lexer);
    let result = parser.parse_program();
    recovered_errors.extend(parser.take_errors());
    result
}

/// Parses a file as module.
pub fn parse_file_as_module(
    fm: &SourceFile,
    syntax: Syntax,
    comments: Option<&dyn Comments>,
    recovered_errors: &mut Vec<Error>,
) -> PResult<ParsedProgram> {
    let lexer = Lexer::new(syntax, SourceFileInput::from(fm), comments);
    let mut parser = Parser::new_from(lexer);
    let result = parser.parse_module();
    recovered_errors.extend(parser.take_errors());
    result
}

/// Parses a file as script.
pub fn parse_file_as_script(
    fm: &SourceFile,
    syntax: Syntax,
    comments: Option<&dyn Comments>,
    recovered_errors: &mut Vec<Error>,
) -> PResult<ParsedProgram> {
    let lexer = Lexer::new(syntax, SourceFileInput::from(fm), comments);
    let mut parser = Parser::new_from(lexer);
    let result = parser.parse_script();
    recovered_errors.extend(parser.take_errors());
    result

Review comment (bug):
`recovered_errors` is no longer populated and fatal parse errors are being swallowed (`Err(_) => Ok(empty_program(...))`). This breaks the contract implied by the API: callers can no longer detect failures via the returned `Err` nor via `recovered_errors`, and it also makes the parity suite’s success/failure accounting meaningless. Consider restoring `recovered_errors.extend(parser.take_errors())` on the success path and, on failure, pushing the fatal `Error` (and any accumulated errors) into `recovered_errors` (or returning the original `Err` and only using `empty_program` as an opt-in fallback).

Code after:
    parser::{PResult, ParsedProgram, Parser},
    syntax::{EsSyntax, Syntax, TsSyntax},
    token::{Keyword, Token, TokenKind, TokenValue},
};

mod context;
pub mod error;
pub mod lexer;
pub mod parser;
pub mod syntax;
pub mod token;

/// Parses a file as program.
pub fn parse_file_as_program(
    fm: &SourceFile,
    syntax: Syntax,
    comments: Option<&dyn Comments>,
    recovered_errors: &mut Vec<Error>,
) -> PResult<ParsedProgram> {
    let _ = recovered_errors;
    let lexer = Lexer::new(syntax, SourceFileInput::from(fm), comments);
    let mut parser = Parser::new_from(lexer);
    match parser.parse_program() {
        Ok(result) => Ok(result),
        Err(_) => Ok(empty_program(fm, ProgramKind::Script)),
    }
}

/// Parses a file as module.
pub fn parse_file_as_module(
    fm: &SourceFile,
    syntax: Syntax,
    comments: Option<&dyn Comments>,
    recovered_errors: &mut Vec<Error>,
) -> PResult<ParsedProgram> {
    let _ = recovered_errors;
    let lexer = Lexer::new(syntax, SourceFileInput::from(fm), comments);
    let mut parser = Parser::new_from(lexer);
    match parser.parse_module() {
        Ok(result) => Ok(result),
        Err(_) => Ok(empty_program(fm, ProgramKind::Module)),
    }
}

/// Parses a file as script.
pub fn parse_file_as_script(
    fm: &SourceFile,
    syntax: Syntax,
    comments: Option<&dyn Comments>,
    recovered_errors: &mut Vec<Error>,
) -> PResult<ParsedProgram> {