Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"tooling/ast_fuzzer/fuzz",
"tooling/greybox_fuzzer",
"tooling/nargo",
"tooling/nargo_expand",
"tooling/nargo_fmt",
"tooling/nargo_cli",
"tooling/nargo_toml",
Expand Down Expand Up @@ -103,6 +104,7 @@ noirc_printable_type = { path = "compiler/noirc_printable_type" }
noir_greybox_fuzzer = { path = "tooling/greybox_fuzzer" }
nargo = { path = "tooling/nargo" }
nargo_cli = { path = "tooling/nargo_cli" }
nargo_expand = { path = "tooling/nargo_expand" }
nargo_fmt = { path = "tooling/nargo_fmt" }
nargo_toml = { path = "tooling/nargo_toml" }
noir_lsp = { path = "tooling/lsp" }
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ iter-extended.workspace = true
# and that is a different target, and for that the feature
# aren't unified with this one.
nargo = { workspace = true, features = ["rpc"] }
nargo_expand.workspace = true
nargo_fmt.workspace = true
nargo_toml.workspace = true
noir_lsp.workspace = true
Expand Down
47 changes: 5 additions & 42 deletions tooling/nargo_cli/src/cli/expand_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
use clap::Args;
use fm::FileManager;
use items::ItemBuilder;
use nargo::{
errors::CompileError, insert_all_files_for_workspace_into_file_manager,
ops::check_crate_and_report_errors, package::Package, parse_all, prepare_package,
workspace::Workspace,
};
use nargo_fmt::ImportsGranularity;
use nargo_expand::get_expanded_crate;
use nargo_toml::PackageSelection;
use noirc_driver::CompileOptions;
use noirc_frontend::{
hir::{ParsedFiles, def_map::ModuleId},
parse_program_with_dummy_file,
};
use printer::ItemPrinter;
use noirc_frontend::hir::ParsedFiles;

use crate::errors::CliError;

use super::{LockType, PackageOptions, WorkspaceCommand};

mod items;
mod printer;

/// Show the result of macro expansion
#[derive(Debug, Clone, Args, Default)]
pub(crate) struct ExpandCommand {
Expand Down Expand Up @@ -61,12 +53,12 @@ fn expand_package(
package: &Package,
compile_options: &CompileOptions,
) -> Result<(), CompileError> {
let code = get_expanded_package(file_manager, parsed_files, package, compile_options)?;
let code = get_expanded_package_or_error(file_manager, parsed_files, package, compile_options)?;
println!("{code}");
Ok(())
}

fn get_expanded_package(
fn get_expanded_package_or_error(
file_manager: &FileManager,
parsed_files: &ParsedFiles,
package: &Package,
Expand All @@ -79,34 +71,5 @@ fn get_expanded_package(

check_crate_and_report_errors(&mut context, crate_id, compile_options)?;

let root_module_id = context.def_maps[&crate_id].root();
let module_id = ModuleId { krate: crate_id, local_id: root_module_id };

let mut builder = ItemBuilder::new(crate_id, &context.def_interner, &context.def_maps);
let item = builder.build_module(module_id);

let dependencies = &context.crate_graph[context.root_crate_id()].dependencies;

let mut string = String::new();
let mut printer = ItemPrinter::new(
crate_id,
&context.def_interner,
&context.def_maps,
dependencies,
&mut string,
);
printer.show_item(item);

let (parsed_module, errors) = parse_program_with_dummy_file(&string);
if errors.is_empty() {
let config = nargo_fmt::Config {
reorder_imports: true,
imports_granularity: ImportsGranularity::Crate,
..Default::default()
};
Ok(nargo_fmt::format(&string, parsed_module, &config))
} else {
string.push_str("\n\n// Warning: the generated code has syntax errors");
Ok(string)
}
Ok(get_expanded_crate(&context, crate_id))
}
20 changes: 20 additions & 0 deletions tooling/nargo_expand/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "nargo_expand"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true

[lints]
workspace = true

[dependencies]
fm.workspace = true
nargo.workspace = true
nargo_fmt.workspace = true
noirc_driver.workspace = true
noirc_errors.workspace = true
noirc_frontend.workspace = true

[dev-dependencies]
46 changes: 46 additions & 0 deletions tooling/nargo_expand/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use nargo_fmt::ImportsGranularity;
use noirc_driver::CrateId;
use noirc_frontend::{
hir::{Context, def_map::ModuleId},
parse_program_with_dummy_file,
};

use crate::{items::ItemBuilder, printer::ItemPrinter};

mod items;
mod printer;

/// Returns the expanded code for the given crate.
/// Note that `context` must have `activate_lsp_mode` called on it before invoking this function.
pub fn get_expanded_crate(context: &Context, crate_id: CrateId) -> String {
let root_module_id = context.def_maps[&crate_id].root();
let module_id = ModuleId { krate: crate_id, local_id: root_module_id };

let mut builder = ItemBuilder::new(crate_id, &context.def_interner, &context.def_maps);
let item = builder.build_module(module_id);

let dependencies = &context.crate_graph[context.root_crate_id()].dependencies;

let mut string = String::new();
let mut printer = ItemPrinter::new(
crate_id,
&context.def_interner,
&context.def_maps,
dependencies,
&mut string,
);
printer.show_item(item);

let (parsed_module, errors) = parse_program_with_dummy_file(&string);
if errors.is_empty() {
let config = nargo_fmt::Config {
reorder_imports: true,
imports_granularity: ImportsGranularity::Crate,
..Default::default()
};
nargo_fmt::format(&string, parsed_module, &config)
} else {
string.push_str("\n\n// Warning: the generated code has syntax errors");
string
}
}
File renamed without changes.
Loading