Files
lucisv/crates/vlog_linter/src/rules/one_module_per_file.rs
T
lucioandClaude Fable 5 5e5813a2a9 Linter expansion, wave 2: per-rule options end to end, options on the naming rules, and eighteen Style rules
TODO section 34 (36 -> 54 rules).
Options: OptionSpec/OptionKind/OptionValue/RuleOptions in vlog_linter
with one validator behind both front ends - [lint.<rule>] tables in
lucisv.toml (accepted now; stored raw by lucisv_core::config, resolved
per file by the CLI with <config>:<line> on the first bad entry) and
the editor's lucisv.lint.options object (bad entries logged and
dropped) - precedence editor > toml > default key by key, --rules-doc
listing every option. A small anchored regex engine of our own: a
Thompson-NFA simulation, linear in pattern x name by construction
(QA's first cut found the backtracking matcher exponential on (a*)*b),
unknown escapes refused by name through both doors, a 71-row table
verified against CPython. The four naming rules take Verible's option
spellings with defaults that keep today's verdicts byte-identical.
Eighteen Style rules, one file each with true positives and near-
misses: line-length (the formatter's width), posix-eof (safe fix),
forbid-consecutive-null-statements (safe fix), generate-label and
generate-label-prefix, package-filename, one-module-per-file (off),
signal/interface/macro/constraint/parameter-type-name-style,
port-name-suffix (off), banned-declared-name-patterns (off),
typedef-enums, typedef-structs-unions (allow_anonymous_nested defaults
true here - opentitan's 3 159 findings were all generated register
packages, and its own config disables the rule), always-comb (unsafe
fix), explicit-begin (off, Verible's eleven enables). Four rules ask
parsed_cleanly() first after a census showed error-recovered UVM files
rebound as generate blocks. package.json regenerated (drift test).
3 549 tests / 48 binaries; corpus 814/820 byte-identical; naming-rule
finding lists identical to 0.36.0; opentitan check timing unchanged.

docs/features/linter-expansion-2/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-09-04 03:36:59 -05:00

135 lines
4.6 KiB
Rust

//! `one-module-per-file` — one module declaration per file.
use crate::context::LintContext;
use crate::registry::Rule;
use crate::severity::Severity;
use crate::violation::Violation;
use vlog_parser::ast::ItemKind;
/// See [`Rule::doc`].
pub struct OneModulePerFile;
const NAME: &str = "one-module-per-file";
impl Rule for OneModulePerFile {
fn name(&self) -> &'static str {
NAME
}
fn summary(&self) -> &'static str {
"declare one module per file"
}
fn severity(&self) -> Severity {
Severity::Style
}
fn doc(&self) -> &'static str {
"Every tool that finds a module by searching a library directory looks \
for one named `<module>.sv`, and `module-filename` ties the file to the \
module it declares — a claim that only means something when there is \
exactly one. Verible's `one-module-per-file` is **off** by default and \
so is this one: plenty of real RTL keeps a handful of small helper \
modules beside the one the file is named for, and that is a project's \
choice rather than a mistake.\n\n\
Only the file's own `module` declarations count. An `interface`, a \
`program` and a `package` are not modules — a package of types beside \
the module that uses them is the common shape this rule must not \
punish.\n\n\
Silent in a file that does not fully parse: error recovery unwinds to \
file scope, so a `module` written inside something the parser could \
not finish reappears as a second top-level declaration — an accusation \
about text the author never wrote that way."
}
fn default_on(&self) -> bool {
false
}
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
if !ctx.parsed_cleanly() {
return Vec::new();
}
let modules: Vec<_> = ctx
.file()
.items
.iter()
.filter_map(|item| match &item.kind {
ItemKind::Module(module) if module.kw == "module" => Some(module),
_ => None,
})
.collect();
let Some(first) = modules.first() else { return Vec::new() };
modules
.iter()
.skip(1)
.map(|module| {
Violation::new(
NAME,
format!(
"module `{}` is the file's second declaration; `{}` is the first",
module.name, first.name
),
module.name_span,
)
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn violations(src: &str) -> Vec<Violation> {
OneModulePerFile.check(&LintContext::new(src, None))
}
#[test]
fn flags_every_module_after_the_first() {
let src = "module a;\nendmodule\nmodule b;\nendmodule\nmodule c;\nendmodule\n";
let found = violations(src);
assert_eq!(found.len(), 2);
assert!(found[0].message.contains("`b`"), "{}", found[0].message);
assert!(found[0].message.contains("`a` is the first"), "{}", found[0].message);
assert_eq!(&src[found[1].span.as_range()], "c");
}
#[test]
fn one_module_is_clean() {
assert!(violations("module a;\nendmodule\n").is_empty());
assert!(violations("package p;\nendpackage\n").is_empty());
}
#[test]
fn a_package_or_an_interface_beside_a_module_is_not_a_second_module() {
let src = "package p;\nendpackage\nmodule a;\nendmodule\ninterface i;\nendinterface\n";
assert!(violations(src).is_empty(), "{:?}", violations(src));
}
/// The everyday shape this rule must not punish: one module, and the
/// package of types it uses, in the same file.
#[test]
fn a_package_and_a_program_beside_one_module_are_clean() {
let src = "package p;\n typedef logic w_t;\nendpackage\n\
module a;\nendmodule\n\
program t;\nendprogram\n";
assert!(violations(src).is_empty(), "{:?}", violations(src));
}
#[test]
fn a_file_that_does_not_parse_is_left_alone() {
// Recovery can promote a module the author nested inside something
// else to file scope; this rule must not accuse them of it.
let src = "module a;\n module inner;\n endmodule\nendmodule\n";
let ctx = LintContext::new(src, None);
assert!(!ctx.parsed_cleanly(), "a nested module does not parse");
assert!(OneModulePerFile.check(&ctx).is_empty());
}
#[test]
fn it_is_off_by_default() {
assert!(!OneModulePerFile.default_on());
}
}