Skip to content

Commit

Permalink
Allow expanding erroneous code
Browse files Browse the repository at this point in the history
commit-id:8156f337
  • Loading branch information
maciektr committed Jun 13, 2024
1 parent 8ea3887 commit 7b92e31
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
20 changes: 3 additions & 17 deletions scarb/src/ops/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::compiler::{CairoCompilationUnit, CompilationUnit, CompilationUnitAttr
use crate::core::{Package, TargetKind, Workspace};
use crate::ops;
use crate::ops::{get_test_package_ids, FeaturesOpts};
use anyhow::{anyhow, bail, Context, Result};
use cairo_lang_compiler::diagnostics::DiagnosticsError;
use anyhow::{bail, Context, Result};
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{LanguageElementId, ModuleId, ModuleItemId};
use cairo_lang_defs::patcher::PatchBuilder;
Expand Down Expand Up @@ -150,17 +149,8 @@ fn do_expand(
) -> Result<()> {
let ScarbDatabase { db, .. } = build_scarb_root_database(compilation_unit, ws)?;
let mut compiler_config = build_compiler_config(compilation_unit, ws);
compiler_config
.diagnostics_reporter
.ensure(&db)
.map_err(|err| err.into())
.map_err(|err| {
if !suppress_error(&err) {
ws.config().ui().anyhow(&err);
}

anyhow!("could not check due to previous error")
})?;
// Report diagnostics, but do not fail.
let _ = compiler_config.diagnostics_reporter.check(&db);
let main_crate_id = db.intern_crate(CrateLongId::Real(
compilation_unit.main_component().cairo_package_name(),
));
Expand Down Expand Up @@ -241,7 +231,3 @@ fn format_cairo(content: String) -> Option<String> {
FormatOutcome::DiffFound(diff) => diff.formatted,
})
}

fn suppress_error(err: &anyhow::Error) -> bool {
matches!(err.downcast_ref(), Some(&DiagnosticsError))
}
39 changes: 39 additions & 0 deletions scarb/tests/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,42 @@ fn can_skip_formatting() {
expanded,
);
}

#[test]
fn can_expand_erroneous_code() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("hello")
// Missing opening bracket.
.lib_cairo(indoc! {r#"
fn hello() -> felt252 {
0
"#})
.build(&t);
Scarb::quick_snapbox()
.arg("expand")
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
error: Missing token TerminalRBrace.
--> [..]lib.cairo:2:6
0
^
"#});
assert_eq!(t.child("target").files(), vec!["CACHEDIR.TAG", "dev"]);
assert_eq!(t.child("target/dev").files(), vec!["hello.expanded.cairo"]);
let expanded = t.child("target/dev/hello.expanded.cairo").read_to_string();
snapbox::assert_eq(
indoc! {r#"
mod hello {
fn hello() -> felt252 {
0
}
"#},
expanded,
);
}

0 comments on commit 7b92e31

Please sign in to comment.