{
+ got[key]
+ .as_array()
+ .unwrap_or_else(|| panic!("{key} is an array: {got}"))
+ .iter()
+ .map(|v| v.as_str().expect("string cell").to_owned())
+ .collect()
+ };
+ // The pre-ranked column ships `aria-sort="descending"`, so its
+ // first activation is a reversal to ascending.
+ assert_eq!(strs("rankedFirstClick"), ["1.0", "3.0", "7.0", "9.0"]);
+ assert_eq!(got["rankedAriaAfterFirstClick"], "ascending");
+ // Numeric sort parses through the thousands separator and keeps
+ // the blank last in both directions.
+ assert_eq!(strs("numericAscending"), ["5", "12", "1,000", ""]);
+ assert_eq!(got["ariaAfterFirstClick"], "ascending");
+ assert_eq!(strs("numericDescending"), ["1,000", "12", "5", ""]);
+ assert_eq!(got["ariaAfterSecondClick"], "descending");
+ // A text sort is lexical, and sorting one column clears the
+ // other's `aria-sort`.
+ assert_eq!(strs("textAscending"), ["a.rs", "b.rs", "c.rs", "d.rs"]);
+ assert!(got["numericAriaCleared"].is_null(), "{got}");
+ assert!(got["rankedAriaCleared"].is_null(), "{got}");
+ // Keyboard: Enter / Space on the header sort and swallow the key.
+ assert_eq!(strs("enterOnHeaderSorts"), ["d.rs", "c.rs", "b.rs", "a.rs"]);
+ assert_eq!(got["enterOnHeaderPrevented"], true);
+ assert_eq!(strs("spaceOnHeaderSorts"), ["5", "12", "1,000", ""]);
+ assert_eq!(got["spaceOnHeaderPrevented"], true);
+ // Enter on the header's documentation link neither sorts nor
+ // cancels the link's own navigation — including the `click` a
+ // browser synthesizes to activate the link, which bubbles to the
+ // header's click handler. A mouse click on the link is likewise
+ // the link's alone.
+ assert_eq!(got["enterOnLinkPrevented"], false, "{got}");
+ assert_eq!(got["enterOnLinkMovedRows"], false, "{got}");
+ assert_eq!(got["clickOnLinkMovedRows"], false, "{got}");
+ assert_eq!(got["tabPrevented"], false);
+ }
+
#[test]
fn js_numeric_sort_strips_thousands_separators() {
// Regression: numeric cells use `thousands()` to insert commas
diff --git a/big-code-analysis-cli/src/html_report/sections.rs b/big-code-analysis-cli/src/html_report/sections.rs
index 5ce46bae2..212cd4a9d 100644
--- a/big-code-analysis-cli/src/html_report/sections.rs
+++ b/big-code-analysis-cli/src/html_report/sections.rs
@@ -244,7 +244,9 @@ pub(crate) fn write_overview_table(
// One discoverability hint near the first table: the columns are
// sortable, which is otherwise invisible until a reader happens to
// click a header (issue #622).
- let _ = out.write_str("Click a column header to sort any table.
\n");
+ let _ = out.write_str(
+ "Click a column header \u{2014} or Tab to it and press Enter \u{2014} to sort any table.
\n",
+ );
let mut rows: Vec> = Vec::with_capacity(by_lang.len());
for (&lang_name, lang_summaries) in by_lang {
rows.push(lang_overview_row(lang_name, lang_summaries));
diff --git a/big-code-analysis-cli/src/html_report/styling.rs b/big-code-analysis-cli/src/html_report/styling.rs
index cabde2fd3..e9312c6a1 100644
--- a/big-code-analysis-cli/src/html_report/styling.rs
+++ b/big-code-analysis-cli/src/html_report/styling.rs
@@ -185,6 +185,8 @@ table.hotspot th:hover{background:#e5e5e5}\
table.hotspot th[aria-sort=ascending]::after{content:\" \\2191\"}\
table.hotspot th[aria-sort=descending]::after{content:\" \\2193\"}\
table.hotspot tr:nth-child(even) td{background:#fafafa}\
+table.hotspot th[data-numeric=\"1\"]{text-align:right}\
+table.hotspot th:focus-visible{outline:2px solid #3d82cc;outline-offset:-2px}\
table.hotspot td.numeric{text-align:right;font-variant-numeric:tabular-nums}\
table.hotspot td.risk-heat-0,table.hotspot td.risk-heat-1,\
table.hotspot td.risk-heat-2,table.hotspot td.risk-heat-3,\
@@ -277,35 +279,46 @@ pub(crate) fn language_palette_slug(lang_name: &str) -> &'static str {
pub(crate) const INLINE_JS: &str = "\
(function(){\
-function num(s){return s===''?Number.POSITIVE_INFINITY:parseFloat(s.replace(/,/g,''));}\
+function num(s){return s===''?NaN:parseFloat(s.replace(/,/g,''));}\
document.querySelectorAll('table.hotspot').forEach(function(table){\
var headers=table.querySelectorAll('thead th');\
headers.forEach(function(th,idx){\
-th.addEventListener('click',function(){sort(table,idx,th);});\
+th.addEventListener('click',function(e){\
+if(e.target!==th)return;\
+sort(table,idx,th);\
+});\
+th.addEventListener('keydown',function(e){\
+if(e.target!==th)return;\
+if(e.key==='Enter'||e.key===' '){e.preventDefault();sort(table,idx,th);}\
+});\
});\
});\
function sort(tbl,idx,th){\
var tbody=tbl.tBodies[0];\
if(!tbody)return;\
-var rows=Array.prototype.slice.call(tbody.rows);\
var numeric=th.dataset.numeric==='1';\
var dir=th.getAttribute('aria-sort')==='ascending'?'descending':'ascending';\
tbl.querySelectorAll('thead th').forEach(function(h){h.removeAttribute('aria-sort');});\
th.setAttribute('aria-sort',dir);\
var sign=dir==='ascending'?1:-1;\
-rows.sort(function(a,b){\
-var av=a.cells[idx].textContent;\
-var bv=b.cells[idx].textContent;\
+var keyed=Array.prototype.map.call(tbody.rows,function(r){\
+var t=r.cells[idx].textContent;\
+return {r:r,k:numeric?num(t):t};\
+});\
+keyed.sort(function(a,b){\
if(numeric){\
-var an=num(av);\
-var bn=num(bv);\
-if(anbn)return 1*sign;\
+var ae=isNaN(a.k);\
+var be=isNaN(b.k);\
+if(ae&&be)return 0;\
+if(ae)return 1;\
+if(be)return -1;\
+if(a.kb.k)return 1*sign;\
return 0;\
}\
-return av.localeCompare(bv)*sign;\
+return a.k.localeCompare(b.k)*sign;\
});\
-rows.forEach(function(r){tbody.appendChild(r);});\
+keyed.forEach(function(e){tbody.appendChild(e.r);});\
}\
})();\
";
diff --git a/big-code-analysis-cli/src/markdown_report.rs b/big-code-analysis-cli/src/markdown_report.rs
index 2d45e889d..fdf8cb72e 100644
--- a/big-code-analysis-cli/src/markdown_report.rs
+++ b/big-code-analysis-cli/src/markdown_report.rs
@@ -1063,7 +1063,8 @@ pub(crate) fn lang_overview_row(
thousands(func_count),
// An empty cell, not `n/a`, when no file had code to measure: the
// HTML table sorter reads an empty cell as "no value" and sorts it
- // last, where any other text would parse as NaN.
+ // last in either direction; any other text would parse as NaN and
+ // sort the same way, but read as a value in the cell.
avg_mi.map_or_else(String::new, |mi| format!("{mi:.1}")),
format!("{avg_cc:.1}"),
format!("{avg_cog:.1}"),
diff --git a/big-code-analysis-cli/src/snapshots/big_code_analysis_cli__html_report__tests__html_report_rich.snap b/big-code-analysis-cli/src/snapshots/big_code_analysis_cli__html_report__tests__html_report_rich.snap
index 413cbffa0..3c91a8f0c 100644
--- a/big-code-analysis-cli/src/snapshots/big_code_analysis_cli__html_report__tests__html_report_rich.snap
+++ b/big-code-analysis-cli/src/snapshots/big_code_analysis_cli__html_report__tests__html_report_rich.snap
@@ -8,7 +8,7 @@ expression: out
Code Quality Metrics Summary — big-code-analysis
-
+
Code Quality Metrics Summary
@@ -65,10 +65,10 @@ expression: out
Per-language overview
-Click a column header to sort any table.
+Click a column header — or Tab to it and press Enter — to sort any table.
-| Language | Files | SLOC | Functions | Avg MI | Avg CC | Avg Cognitive |
+| Language | Files | SLOC | Functions | Avg MI | Avg CC | Avg Cognitive |
| Python | 1 | 120 | 2 | 28.0 | 8.5 | 9.0 |
| Rust | 2 | 280 | 8 | 21.4 | 23.4 | 22.6 |
@@ -88,7 +88,7 @@ expression: out
Maintainability Index hotspots (lowest 5 by MI)
-| File | MI | SLOC | Tokens |
+| File | MI | SLOC | Tokens |
| src/main.py | 28.0 | 120 | 800 |
@@ -98,7 +98,7 @@ expression: out
Cyclomatic complexity hotspots (top 5 by CC)
-| Function | File | Line | CC | Cognitive | SLOC | Tokens |
+| Function | File | Line | CC | Cognitive | SLOC | Tokens |
| main | src/main.py | 10 | 10 | 12 | 60 | 300 |
| load_data | src/main.py | 40 | 7 | 6 | 35 | 180 |
@@ -109,7 +109,7 @@ expression: out
Cognitive complexity hotspots (top 5 by Cognitive)
-| Function | File | Line | Cognitive | CC | SLOC | Tokens |
+| Function | File | Line | Cognitive | CC | SLOC | Tokens |
| main | src/main.py | 10 | 12 | 10 | 60 | 300 |
| load_data | src/main.py | 40 | 6 | 7 | 35 | 180 |
@@ -119,7 +119,7 @@ expression: out
Halstead effort hotspots (top 5 by Effort)
-| Function | File | Line | Effort | Volume | Est. Bugs | SLOC | Tokens |
+| Function | File | Line | Effort | Volume | Est. Bugs | SLOC | Tokens |
| main | src/main.py | 10 | 2,000 | 1,000 | 0.33 | 60 | 300 |
| load_data | src/main.py | 40 | 1,200 | 600 | 0.23 | 35 | 180 |
@@ -129,7 +129,7 @@ expression: out
Function size hotspots (top 5 by SLOC)
-| Function | File | Line | SLOC | Tokens | CC | Cognitive |
+| Function | File | Line | SLOC | Tokens | CC | Cognitive |
| main | src/main.py | 10 | 60 | 300 | 10 | 12 |
| load_data | src/main.py | 40 | 35 | 180 | 7 | 6 |
@@ -139,7 +139,7 @@ expression: out
Many parameters hotspots (top 5 by Args)
-| Function | File | Line | Args | SLOC | Tokens |
+| Function | File | Line | Args | SLOC | Tokens |
| load_data | src/main.py | 40 | 4 | 35 | 180 |
@@ -148,7 +148,7 @@ expression: out
ABC magnitude hotspots (top 5 by ABC)
-| Function | File | Line | ABC | SLOC | Tokens |
+| Function | File | Line | ABC | SLOC | Tokens |
| main | src/main.py | 10 | 9.0 | 60 | 300 |
| load_data | src/main.py | 40 | 7.0 | 35 | 180 |
@@ -173,7 +173,7 @@ expression: out
Maintainability Index hotspots (lowest 5 by MI)
-| File | MI | SLOC | Tokens |
+| File | MI | SLOC | Tokens |
| src/lib.rs | 12.0 | 200 | 1,500 |
| src/util.rs | 45.0 | 80 | 600 |
@@ -184,7 +184,7 @@ expression: out
Cyclomatic complexity hotspots (top 5 by CC)
-| Function | File | Line | CC | Cognitive | SLOC | Tokens |
+| Function | File | Line | CC | Cognitive | SLOC | Tokens |
| process_request | src/lib.rs | 10 | 25 | 30 | 150 | 900 |
| validate_input | src/lib.rs | 40 | 20 | 24 | 120 | 700 |
@@ -198,7 +198,7 @@ expression: out
Cognitive complexity hotspots (top 5 by Cognitive)
-| Function | File | Line | Cognitive | CC | SLOC | Tokens |
+| Function | File | Line | Cognitive | CC | SLOC | Tokens |
| secret_internal | src/lib.rs | 220 | 80 | 99 | 300 | 2,000 |
| process_request | src/lib.rs | 10 | 30 | 25 | 150 | 900 |
@@ -211,7 +211,7 @@ expression: out
Halstead effort hotspots (top 5 by Effort)
-| Function | File | Line | Effort | Volume | Est. Bugs | SLOC | Tokens |
+| Function | File | Line | Effort | Volume | Est. Bugs | SLOC | Tokens |
| secret_internal | src/lib.rs | 220 | 20,000 | 10,000 | 3.30 | 300 | 2,000 |
| process_request | src/lib.rs | 10 | 9,000 | 4,500 | 0.83 | 150 | 900 |
@@ -224,7 +224,7 @@ expression: out
Function size hotspots (top 5 by SLOC)
-| Function | File | Line | SLOC | Tokens | CC | Cognitive |
+| Function | File | Line | SLOC | Tokens | CC | Cognitive |
| secret_internal | src/lib.rs | 220 | 300 | 2,000 | 99 | 80 |
| process_request | src/lib.rs | 10 | 150 | 900 | 25 | 30 |
@@ -237,7 +237,7 @@ expression: out
Many parameters hotspots (top 5 by Args)
-| Function | File | Line | Args | SLOC | Tokens |
+| Function | File | Line | Args | SLOC | Tokens |
| secret_internal | src/lib.rs | 220 | 8 | 300 | 2,000 |
| process_request | src/lib.rs | 10 | 6 | 150 | 900 |
@@ -249,7 +249,7 @@ expression: out
Type hotspots (top 5 by WMC)
-| Type | File | Line | WMC | Methods | NPA | NPM | SLOC | Tokens |
+| Type | File | Line | WMC | Methods | NPA | NPM | SLOC | Tokens |
| Widget | src/lib.rs | 250 | 40 | 8 | 2 | 6 | 180 | 1,100 |
@@ -258,7 +258,7 @@ expression: out
Exit points hotspots (top 5 by Exits)
-| Function | File | Line | Exits | CC | SLOC | Tokens |
+| Function | File | Line | Exits | CC | SLOC | Tokens |
| secret_internal | src/lib.rs | 220 | 10 | 99 | 300 | 2,000 |
| handle_event | src/lib.rs | 100 | 6 | 12 | 70 | 400 |
@@ -270,7 +270,7 @@ expression: out
ABC magnitude hotspots (top 5 by ABC)
-| Function | File | Line | ABC | SLOC | Tokens |
+| Function | File | Line | ABC | SLOC | Tokens |
| secret_internal | src/lib.rs | 220 | 50.0 | 300 | 2,000 |
| process_request | src/lib.rs | 10 | 22.0 | 150 | 900 |
@@ -304,6 +304,6 @@ expression: out
Comment ratioComment ratio: comment lines as a percentage of source lines (CLOC / SLOC).
-
+