Skip to content

Commit 5732ee0

Browse files
authored
[private] modules are excluded from --list output (#2889)
1 parent f028de5 commit 5732ee0

8 files changed

Lines changed: 58 additions & 10 deletions

File tree

src/analyzer.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ impl<'run, 'src> Analyzer<'run, 'src> {
2020
loaded: &[PathBuf],
2121
name: Option<Name<'src>>,
2222
paths: &HashMap<PathBuf, PathBuf>,
23+
private: bool,
2324
root: &Path,
2425
) -> RunResult<'src, Justfile<'src>> {
25-
Self::default().justfile(asts, config, doc, groups, loaded, name, paths, root)
26+
Self::default().justfile(
27+
asts, config, doc, groups, loaded, name, paths, root, private,
28+
)
2629
}
2730

2831
fn justfile(
@@ -35,6 +38,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
3538
name: Option<Name<'src>>,
3639
paths: &HashMap<PathBuf, PathBuf>,
3740
root: &Path,
41+
private: bool,
3842
) -> RunResult<'src, Justfile<'src>> {
3943
let mut definitions = HashMap::new();
4044
let mut imports = HashSet::new();
@@ -69,6 +73,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
6973
doc,
7074
groups,
7175
name,
76+
private,
7277
..
7378
} => {
7479
if let Some(absolute) = absolute {
@@ -81,6 +86,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
8186
loaded,
8287
Some(*name),
8388
paths,
89+
*private,
8490
absolute,
8591
)?);
8692
}
@@ -231,6 +237,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
231237
module_path: ast.module_path.clone(),
232238
modules: self.modules,
233239
name,
240+
private,
234241
recipes,
235242
settings,
236243
source,

src/compiler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Compiler {
101101
asts.insert(current.path, ast.clone());
102102
}
103103

104-
let justfile = Analyzer::analyze(&asts, config, None, &[], &loaded, None, &paths, root)?;
104+
let justfile = Analyzer::analyze(&asts, config, None, &[], &loaded, None, &paths, false, root)?;
105105

106106
Ok(Compilation {
107107
asts,
@@ -234,6 +234,7 @@ impl Compiler {
234234
&[],
235235
None,
236236
&paths,
237+
false,
237238
&root,
238239
)
239240
}

src/item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub(crate) enum Item<'src> {
1717
groups: Vec<StringLiteral<'src>>,
1818
name: Name<'src>,
1919
optional: bool,
20+
private: bool,
2021
relative: Option<StringLiteral<'src>>,
2122
},
2223
Recipe(UnresolvedRecipe<'src>),

src/justfile.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub(crate) struct Justfile<'src> {
1515
pub(crate) modules: Table<'src, Justfile<'src>>,
1616
#[serde(skip)]
1717
pub(crate) name: Option<Name<'src>>,
18+
#[serde(skip)]
19+
pub(crate) private: bool,
1820
pub(crate) recipes: Table<'src, Arc<Recipe<'src>>>,
1921
pub(crate) settings: Settings,
2022
pub(crate) source: PathBuf,
@@ -391,8 +393,12 @@ impl<'src> Justfile<'src> {
391393
Ok(())
392394
}
393395

394-
pub(crate) fn modules(&self, config: &Config) -> Vec<&Justfile> {
395-
let mut modules = self.modules.values().collect::<Vec<&Justfile>>();
396+
pub(crate) fn public_modules(&self, config: &Config) -> Vec<&Justfile> {
397+
let mut modules = self
398+
.modules
399+
.values()
400+
.filter(|module| !module.private)
401+
.collect::<Vec<&Justfile>>();
396402

397403
if config.unsorted {
398404
modules.sort_by_key(|module| {
@@ -440,7 +446,7 @@ impl<'src> Justfile<'src> {
440446
}
441447
}
442448

443-
for submodule in self.modules.values() {
449+
for submodule in self.public_modules(config) {
444450
for group in submodule.groups() {
445451
groups.push((&[], submodule.name.unwrap().offset, group.to_string()));
446452
}

src/parser.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,11 @@ impl<'run, 'src> Parser<'run, 'src> {
413413
attributes.ensure_valid_attributes(
414414
"Module",
415415
*name,
416-
&[AttributeDiscriminant::Doc, AttributeDiscriminant::Group],
416+
&[
417+
AttributeDiscriminant::Doc,
418+
AttributeDiscriminant::Group,
419+
AttributeDiscriminant::Private,
420+
],
417421
)?;
418422

419423
let doc = match attributes.get(AttributeDiscriminant::Doc) {
@@ -423,6 +427,8 @@ impl<'run, 'src> Parser<'run, 'src> {
423427
_ => unreachable!(),
424428
};
425429

430+
let private = attributes.contains(AttributeDiscriminant::Private);
431+
426432
let mut groups = Vec::new();
427433
for attribute in attributes {
428434
if let Attribute::Group(group) = attribute {
@@ -431,11 +437,12 @@ impl<'run, 'src> Parser<'run, 'src> {
431437
}
432438

433439
items.push(Item::Module {
434-
groups,
435440
absolute: None,
436441
doc,
442+
groups,
437443
name,
438444
optional,
445+
private,
439446
relative,
440447
});
441448
}

src/subcommand.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ impl Subcommand {
551551
}
552552
}
553553
if !config.list_submodules {
554-
for (name, _) in &module.modules {
554+
for submodule in module.public_modules(config) {
555+
let name = submodule.name();
555556
signature_widths.insert(name, UnicodeWidthStr::width(format!("{name} ...").as_str()));
556557
}
557558
}
@@ -589,7 +590,7 @@ impl Subcommand {
589590

590591
let submodule_groups = {
591592
let mut groups = BTreeMap::<Option<String>, Vec<&Justfile>>::new();
592-
for submodule in module.modules(config) {
593+
for submodule in module.public_modules(config) {
593594
let submodule_groups = submodule.groups();
594595
if submodule_groups.is_empty() {
595596
groups.entry(None).or_default().push(submodule);
@@ -752,7 +753,8 @@ impl Subcommand {
752753
*printed += 1;
753754
}
754755

755-
for (name, module) in &justfile.modules {
756+
for module in justfile.public_modules(config) {
757+
let name = module.name();
756758
components.push(name);
757759
Self::summary_recursive(config, components, printed, module);
758760
components.pop();

src/testing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub(crate) fn analysis_error(
7777
&[],
7878
None,
7979
&paths,
80+
false,
8081
&root,
8182
) {
8283
Ok(_) => panic!("Analysis unexpectedly succeeded"),

tests/private.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ fn private_attribute_for_alias() {
3939
.run();
4040
}
4141

42+
#[test]
43+
fn private_attribute_for_module() {
44+
Test::new()
45+
.write("foo.just", "bar:")
46+
.justfile(
47+
r"
48+
[private]
49+
mod foo
50+
51+
baz:
52+
",
53+
)
54+
.test_round_trip(false)
55+
.arg("--list")
56+
.stdout(
57+
"
58+
Available recipes:
59+
baz
60+
",
61+
)
62+
.run();
63+
}
64+
4265
#[test]
4366
fn private_variables_are_not_listed() {
4467
Test::new()

0 commit comments

Comments
 (0)