Skip to content

Commit

Permalink
fix: overridden functions (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent authored Jan 16, 2024
1 parent 44f8e46 commit bd1c2a2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
15 changes: 14 additions & 1 deletion sample-data/BasicSample.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity =0.8.19;

contract BasicSample {
abstract contract AbstractBasic {
function overriddenFunction() internal pure virtual returns (uint256 _returned);
}

contract BasicSample is AbstractBasic {
/**
* @notice Some notice of the struct
*/
Expand Down Expand Up @@ -72,6 +76,15 @@ contract BasicSample {
return (true, 111);
}

/**
* @notice This function should have an inheritdoc tag
*/
function overriddenFunction() internal pure override returns (uint256 _returned) {
return 1;
}

function virtualFunction() public pure virtual returns (uint256 _returned) {}

/**
* @notice Modifier notice
*/
Expand Down
4 changes: 2 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export class Validator {
_requiresInheritdoc ||=
node instanceof FunctionDefinition && (node.visibility === 'external' || node.visibility === 'public') && !node.isConstructor;

// Internal virtual function
_requiresInheritdoc ||= node instanceof FunctionDefinition && node.visibility === 'internal' && node.virtual;
// Overridden internal functions
_requiresInheritdoc ||= node instanceof FunctionDefinition && node.visibility === 'internal' && !!node.vOverrideSpecifier;

// Public variable
_requiresInheritdoc ||= node instanceof VariableDeclaration && node.visibility === 'public';
Expand Down
47 changes: 43 additions & 4 deletions test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import { ContractDefinition } from 'solc-typed-ast';
import { Validator } from '../src/validator';
import { getFileCompiledSource } from './utils';
import { Config, NodeToProcess } from '../src/types';
import { before } from 'node:test';

describe('Validator', () => {
let contract: ContractDefinition;
let node: NodeToProcess;

const config: Config = {
let config: Config = {
root: '.',
include: './sample-data',
exclude: [],
enforceInheritdoc: false,
constructorNatspec: false,
};

const validator: Validator = new Validator(config);
let validator: Validator = new Validator(config);

beforeAll(async () => {
const compileResult = await getFileCompiledSource('sample-data/BasicSample.sol');
contract = compileResult.vContracts[0];
node = contract.vFunctions.find(({ name }) => name === 'externalSimple')!;
contract = compileResult.vContracts.find(({ name }) => name === 'BasicSample')!;
});

let natspec = {
Expand Down Expand Up @@ -57,11 +57,14 @@ describe('Validator', () => {
};

it('should validate proper natspec', () => {
node = contract.vFunctions.find(({ name }) => name === 'externalSimple')!;

const result = validator.validate(node, natspec);
expect(result).toEqual([]);
});

it('should reveal missing natspec for parameters', () => {
node = contract.vFunctions.find(({ name }) => name === 'externalSimple')!;
const paramName = '_magicNumber';
let natspec = {
tags: [
Expand Down Expand Up @@ -239,4 +242,40 @@ describe('Validator', () => {
expect(result).toContainEqual(`@param ${paramName1} is missing`);
expect(result).toContainEqual(`@param ${paramName2} is missing`);
});

describe('with enforced inheritdoc', () => {
beforeAll(async () => {
config = {
root: '.',
include: './sample-data',
exclude: [],
enforceInheritdoc: true,
constructorNatspec: false,
};

validator = new Validator(config);
});

it('should reveal missing inheritdoc for an overridden function', () => {
node = contract.vFunctions.find(({ name }) => name === 'overriddenFunction')!;
natspec = {
tags: [],
params: [],
returns: [],
};
const result = validator.validate(node, natspec);
expect(result).toContainEqual(`@inheritdoc is missing`);
});

it('should reveal missing inheritdoc for a virtual function', () => {
node = contract.vFunctions.find(({ name }) => name === 'virtualFunction')!;
natspec = {
tags: [],
params: [],
returns: [],
};
const result = validator.validate(node, natspec);
expect(result).toContainEqual(`@inheritdoc is missing`);
});
});
});
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"esModuleInterop": true,
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"sourceMap": true,
"resolveJsonModule": true,
"target": "es2020"
Expand Down

0 comments on commit bd1c2a2

Please sign in to comment.