Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow enforcing natspec on specific items #42

Merged
merged 12 commits into from
Feb 24, 2025
Next Next commit
fix(variable): raise error on missing natspec
beeb committed Feb 24, 2025

Verified

This commit was signed with the committer’s verified signature.
beeb Valentin B.
commit 099d65c3e3d7308397e306dc35ed93f62238c980
26 changes: 15 additions & 11 deletions src/definitions/variable.rs
Original file line number Diff line number Diff line change
@@ -87,16 +87,19 @@ impl Validate for VariableDeclaration {
span: self.span(),
diags: vec![],
};
// raise error if no NatSpec is available (unless we don't enforce inheritdoc)
// raise error if no NatSpec is available
let Some(natspec) = &self.natspec else {
if !options.inheritdoc || !self.requires_inheritdoc() {
return out;
if options.inheritdoc && self.requires_inheritdoc() {
out.diags.push(Diagnostic {
span: self.span(),
message: "@inheritdoc is missing".to_string(),
});
} else {
out.diags.push(Diagnostic {
span: self.span(),
message: "missing NatSpec".to_string(),
});
}
// we require natspec for inheritdoc
out.diags.push(Diagnostic {
span: self.span(),
message: "missing NatSpec".to_string(),
});
return out;
};
// if there is `inheritdoc`, no further validation is required
@@ -155,13 +158,13 @@ mod tests {
}

#[test]
fn test_variable_no_natspec() {
fn test_variable_no_natspec_inheritdoc() {
let contents = "contract Test {
uint256 public a;
}";
let res = parse_file(contents).validate(&OPTIONS);
assert_eq!(res.diags.len(), 1);
assert_eq!(res.diags[0].message, "missing NatSpec");
assert_eq!(res.diags[0].message, "@inheritdoc is missing");
}

#[test]
@@ -215,6 +218,7 @@ mod tests {
.enum_params(false)
.build(),
);
assert!(res.diags.is_empty(), "{:#?}", res.diags);
assert_eq!(res.diags.len(), 1);
assert_eq!(res.diags[0].message, "missing NatSpec");
}
}
4 changes: 4 additions & 0 deletions tests/snapshots/tests_parser_test__basic.snap
Original file line number Diff line number Diff line change
@@ -16,6 +16,10 @@ event IParserTest.SimpleEvent
function IParserTest.SOME_CONSTANT
@return _returned is missing

./test-data/ParserTest.sol:135:1
variable ParserTestFunny.SOME_CONSTANT
missing NatSpec

./test-data/ParserTest.sol:145:3
function ParserTestFunny.viewFunctionWithParams
missing NatSpec
4 changes: 4 additions & 0 deletions tests/snapshots/tests_parser_test__constructor.snap
Original file line number Diff line number Diff line change
@@ -16,6 +16,10 @@ event IParserTest.SimpleEvent
function IParserTest.SOME_CONSTANT
@return _returned is missing

./test-data/ParserTest.sol:135:1
variable ParserTestFunny.SOME_CONSTANT
missing NatSpec

./test-data/ParserTest.sol:145:3
function ParserTestFunny.viewFunctionWithParams
missing NatSpec
4 changes: 4 additions & 0 deletions tests/snapshots/tests_parser_test__enum.snap
Original file line number Diff line number Diff line change
@@ -22,6 +22,10 @@ enum IParserTest.SimpleEnum
function IParserTest.SOME_CONSTANT
@return _returned is missing

./test-data/ParserTest.sol:135:1
variable ParserTestFunny.SOME_CONSTANT
missing NatSpec

./test-data/ParserTest.sol:145:3
function ParserTestFunny.viewFunctionWithParams
missing NatSpec
2 changes: 1 addition & 1 deletion tests/snapshots/tests_parser_test__inheritdoc.snap
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ function IParserTest.SOME_CONSTANT

./test-data/ParserTest.sol:135:1
variable ParserTestFunny.SOME_CONSTANT
missing NatSpec
@inheritdoc is missing

./test-data/ParserTest.sol:145:3
function ParserTestFunny.viewFunctionWithParams
4 changes: 4 additions & 0 deletions tests/snapshots/tests_parser_test__struct.snap
Original file line number Diff line number Diff line change
@@ -20,6 +20,10 @@ function IParserTest.SOME_CONSTANT
struct ParserTestFunny.SimpleStruct
missing NatSpec

./test-data/ParserTest.sol:135:1
variable ParserTestFunny.SOME_CONSTANT
missing NatSpec

./test-data/ParserTest.sol:145:3
function ParserTestFunny.viewFunctionWithParams
missing NatSpec