Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 0 additions & 5 deletions src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ pub(crate) struct Compilation<'src> {
pub(crate) asts: HashMap<PathBuf, Ast<'src>>,
pub(crate) justfile: Justfile<'src>,
pub(crate) root: PathBuf,
pub(crate) srcs: HashMap<PathBuf, &'src str>,
}

impl<'src> Compilation<'src> {
pub(crate) fn root_ast(&self) -> &Ast<'src> {
self.asts.get(&self.root).unwrap()
}

pub(crate) fn root_src(&self) -> &'src str {
self.srcs.get(&self.root).unwrap()
}
}
41 changes: 0 additions & 41 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ impl Compiler {
let mut asts = HashMap::<PathBuf, Ast>::new();
let mut loaded = Vec::new();
let mut paths = HashMap::<PathBuf, PathBuf>::new();
let mut srcs = HashMap::<PathBuf, &str>::new();

let mut stack = Vec::new();
stack.push(Source::root(root));

Expand All @@ -34,7 +32,6 @@ impl Compiler {
)?;

paths.insert(current.path.clone(), relative.into());
srcs.insert(current.path.clone(), src);

for item in &mut ast.items {
match item {
Expand Down Expand Up @@ -107,7 +104,6 @@ impl Compiler {
asts,
justfile,
root: root.into(),
srcs,
})
}

Expand Down Expand Up @@ -244,43 +240,6 @@ impl Compiler {
mod tests {
use {super::*, temptree::temptree};

#[test]
fn include_justfile() {
let justfile_a = r#"
# A comment at the top of the file
import "./justfile_b"

#some_recipe: recipe_b
some_recipe:
echo "some recipe"
"#;

let justfile_b = r#"import "./subdir/justfile_c"

recipe_b: recipe_c
echo "recipe b"
"#;

let justfile_c = r#"recipe_c:
echo "recipe c"
"#;

let tmp = temptree! {
justfile: justfile_a,
justfile_b: justfile_b,
subdir: {
justfile_c: justfile_c
}
};

let loader = Loader::new();

let justfile_a_path = tmp.path().join("justfile");
let compilation = Compiler::compile(&Config::default(), &loader, &justfile_a_path).unwrap();

assert_eq!(compilation.root_src(), justfile_a);
}

#[test]
fn recursive_includes_fail() {
let tmp = temptree! {
Expand Down
38 changes: 31 additions & 7 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ impl Subcommand {
return Self::edit(&search);
}

if matches!(self, Format) {
return Self::format(config, loader, &search);
}

let compilation = Self::compile(config, loader, &search)?;
let justfile = &compilation.justfile;

Expand All @@ -98,15 +102,16 @@ impl Subcommand {
justfile.run(config, &search, &[])?;
}
Dump => Self::dump(config, compilation)?,
Format => Self::format(config, &search, compilation)?,
Groups => Self::groups(config, justfile),
List { path } => Self::list(config, justfile, path)?,
Run { arguments } => Self::run(config, loader, search, compilation, arguments)?,
Show { path } => Self::show(config, justfile, path)?,
Summary => Self::summary(config, justfile),
Usage { path } => Self::usage(config, justfile, path)?,
Variables => Self::variables(justfile),
Changelog | Completions { .. } | Edit | Init | Man | Request { .. } => unreachable!(),
Changelog | Completions { .. } | Edit | Format | Init | Man | Request { .. } => {
unreachable!()
}
}

Ok(())
Expand Down Expand Up @@ -324,12 +329,31 @@ impl Subcommand {
Ok(())
}

fn format(config: &Config, search: &Search, compilation: Compilation) -> RunResult<'static> {
let justfile = &compilation.justfile;
let src = compilation.root_src();
let ast = compilation.root_ast();
fn format<'src>(config: &Config, loader: &'src Loader, search: &Search) -> RunResult<'src> {
let root = search.justfile.parent().unwrap();

let (path, src) = loader.load(root, &search.justfile)?;

let tokens = Lexer::lex(path, src)?;

config.require_unstable(justfile, UnstableFeature::FormatSubcommand)?;
let ast = Parser::parse(0, &[], None, &tokens, &search.working_directory)?;

let unstable = config.unstable
|| ast.items.iter().any(|item| {
matches!(
item,
Item::Set(Set {
value: Setting::Unstable(true),
..
})
)
});

if !unstable {
return Err(Error::UnstableFeature {
unstable_feature: UnstableFeature::FormatSubcommand,
});
}

let formatted = ast.to_string();

Expand Down
18 changes: 18 additions & 0 deletions tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,3 +1700,21 @@ fn arg_attribute_help() {
)
.success();
}

#[test]
fn missing_import_file() {
Test::new()
.args(["--unstable", "--fmt", "--check"])
.justfile("import 'foo'\n")
.test_round_trip(false)
.success();
}

#[test]
fn missing_module_file() {
Test::new()
.args(["--unstable", "--fmt", "--check"])
.justfile("mod foo\n")
.test_round_trip(false)
.success();
}