Skip to content

Commit

Permalink
fix #966
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Feb 21, 2023
1 parent ce76a6d commit 8947d43
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/info/langs/language.tera
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn __stats_loc(language_type: &tokei::LanguageType, stats: &tokei::CodeStats) ->
{%- set line_types = attrs.line_types | default(value=['code']) -%}
tokei::LanguageType::{{ language }} => stats.{{ line_types.0 }}{% for line_type in line_types | slice(start=1) %} + stats.{{ line_type }}{% endfor %},
{% endfor %}
_ => unimplemented!("Language Type {:?}", language_type),
_ => 0, // Tokei doesn't filter children languages according to the input config

This comment has been minimized.

Copy link
@spenserblack

spenserblack Feb 21, 2023

Collaborator

Actually, even if we don't support the language, since it's a nested language, would we still want to count the LOC?

In fact, revisiting this, instead of creating a branch for every language, perhaps it could just be simplified to

{% for language, attrs in languages -%}
{%- if attrs.line_types -%}
tokei::LanguageType::{{ language }} => stats.{{ line_types.0 }}{% for line_type in line_types | slice(start=1) %} + stats.{{ line_type }}{% endfor %}
{% endif %}
{% endfor %}
_ => stats.code

So that, instead of the generated code being this this:

match language_type {
    tokei::LanguageType::Ada => stats.code,  // Language with no override
    // many more branches
    tokei::LanguageType::Markdown => stats.code + stats.comment
    // many more branches
    _ => 0
}

The generated code is this:

match language_type {
    tokei::LanguageType::Markdown => stats.code + stats.comment
    _ => stats.code
}

For example, for a Vue file, we would probably want to count the internal Stylus lines, wouldn't we? In fact, right now, I think Markdown inside Jupyter and Markdown inside Markdown are the only nested languages that need an override.

This comment has been minimized.

Copy link
@o2sh

o2sh Feb 21, 2023

Author Owner

You're right, thanks for the heads up 👍

aa0412f

This comment has been minimized.

Copy link
@spenserblack

spenserblack Feb 21, 2023

Collaborator

Even though the generated code doesn't show up in our coverage report, you've technically massively boosted the test coverage % by reducing the number of uncovered branches 😆

}
}

Expand Down
29 changes: 29 additions & 0 deletions src/info/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ mod test {
assert_eq!(loc_by_language[&Language::Jupyter], 21);
}

// https://github.com/o2sh/onefetch/issues/966
#[test]
fn get_loc_by_language_should_not_panic_when_children_language_is_not_supported() {
let mut stylus_code_stats = tokei::CodeStats::new();
stylus_code_stats.code = 10;
stylus_code_stats.blanks = 2;
stylus_code_stats.comments = 4;

let mut stylus_report = tokei::Report::new("/tmp/file.vue".into());
stylus_report.stats = stylus_code_stats;

let mut vue = tokei::Language {
blanks: 50,
comments: 200,
code: 100,
..Default::default()
};

vue.children
.insert(tokei::LanguageType::Stylus, vec![stylus_report]);

let mut languages = tokei::Languages::new();
languages.insert(tokei::LanguageType::Vue, vue);

let loc_by_language = get_loc_by_language(&languages).unwrap();

assert_eq!(loc_by_language[&Language::Vue], 100);
}

#[test]
fn test_get_loc_by_language_sorted() {
let mut map = HashMap::new();
Expand Down

0 comments on commit 8947d43

Please sign in to comment.