Skip to content

Commit

Permalink
Also emit swc recoverable errors (#9650)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored Apr 16, 2024
1 parent 8add915 commit f84b6dc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
47 changes: 47 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6438,6 +6438,53 @@ describe('javascript', function () {
}
});

it(`should also fail on recoverable parse errors`, async () => {
await fsFixture(overlayFS, __dirname)`
js-recoverable-parse-errors
index.js:
1 / {2}`;

const fixture = path.join(
__dirname,
'/js-recoverable-parse-errors/index.js',
);

await assert.rejects(
() =>
bundle(fixture, {
inputFS: overlayFS,
}),
{
name: 'BuildError',
diagnostics: [
{
origin: '@parcel/transformer-js',
message: 'Unexpected token `}`. Expected identifier',
hints: null,
codeFrames: [
{
filePath: fixture,
codeHighlights: [
{
message: undefined,
start: {
column: 7,
line: 1,
},
end: {
column: 7,
line: 1,
},
},
],
},
],
},
],
},
);
});

for (let shouldScopeHoist of [false, true]) {
let options = {
defaultTargetOptions: {
Expand Down
32 changes: 25 additions & 7 deletions packages/transformers/js/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ use swc_core::common::source_map::SourceMapGenConfig;
use swc_core::common::{chain, sync::Lrc, FileName, Globals, Mark, SourceMap};
use swc_core::ecma::ast::{Module, ModuleItem, Program};
use swc_core::ecma::codegen::text_writer::JsWriter;
use swc_core::ecma::parser::error::Error;
use swc_core::ecma::parser::lexer::Lexer;
use swc_core::ecma::parser::{EsConfig, PResult, Parser, StringInput, Syntax, TsConfig};
use swc_core::ecma::parser::{EsConfig, Parser, StringInput, Syntax, TsConfig};
use swc_core::ecma::preset_env::{preset_env, Mode::Entry, Targets, Version, Versions};
use swc_core::ecma::transforms::base::fixer::paren_remover;
use swc_core::ecma::transforms::base::helpers;
Expand Down Expand Up @@ -158,10 +159,12 @@ pub fn transform(
);

match module {
Err(err) => {
Err(errs) => {
let error_buffer = ErrorBuffer::default();
let handler = Handler::with_emitter(true, false, Box::new(error_buffer.clone()));
err.into_diagnostic(&handler).emit();
for err in errs {
err.into_diagnostic(&handler).emit();
}

result.diagnostics = Some(error_buffer_to_diagnostics(&error_buffer, &source_map));
Ok(result)
Expand Down Expand Up @@ -514,13 +517,15 @@ pub fn transform(
}
}

pub type ParseResult<T> = Result<T, Vec<Error>>;

fn parse(
code: &str,
project_root: &str,
filename: &str,
source_map: &Lrc<SourceMap>,
config: &Config,
) -> PResult<(Program, SingleThreadedComments)> {
) -> ParseResult<(Program, SingleThreadedComments)> {
// Attempt to convert the path to be relative to the project root.
// If outside the project root, use an absolute path so that if the project root moves the path still works.
let filename: PathBuf = if let Ok(relative) = Path::new(filename).strip_prefix(project_root) {
Expand All @@ -543,6 +548,7 @@ fn parse(
export_default_from: true,
decorators: config.decorators,
import_attributes: true,
allow_return_outside_function: true,
..Default::default()
})
};
Expand All @@ -555,10 +561,22 @@ fn parse(
);

let mut parser = Parser::new_from(lexer);
match parser.parse_program() {
Err(err) => Err(err),
Ok(module) => Ok((module, comments)),
let result = parser.parse_program();

let module = match result {
Err(err) => {
// A fatal error
return Err(vec![err]);
}
Ok(module) => module,
};
// Recoverable errors
let errors = parser.take_errors();
if !errors.is_empty() {
return Err(errors);
}

Ok((module, comments))
}

fn emit(
Expand Down

0 comments on commit f84b6dc

Please sign in to comment.