Skip to content

Commit

Permalink
refactor: simplify validateContractNatspec a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Feb 20, 2024
1 parent cc916b9 commit e224bbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
15 changes: 4 additions & 11 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class Validator {
}

validate(node: NodeToProcess, natspec: Natspec): string[] {
// Proccess contract-level natspec
// Process contract-level natspec
if (node instanceof ContractDefinition) {
return this.config.contractNatspec ? this.validateContractNatspec(node, natspec) : [];
return this.config.contractNatspec ? this.validateContractNatspec(natspec) : [];
}

// Ignore fallback and receive
Expand Down Expand Up @@ -141,17 +141,10 @@ export class Validator {

/**
* Validate the natspec of a contract
* @param {ContractDefinition} node - The contract node
* @param {Natspec} natspec - The natspec of the contract
* @returns {string[]} - The list of alerts
*/
private validateContractNatspec(node: ContractDefinition, natspec: Natspec): string[] {
let alerts: string[] = [];

if (!natspec.tags.map((t) => t.name).includes('notice')) {
alerts.push(`Contract @notice is missing`);
}

return alerts;
private validateContractNatspec(natspec: Natspec): string[] {
return natspec.tags.some((t) => t.name === 'notice') ? [] : [`@notice is missing`];
}
}
19 changes: 11 additions & 8 deletions test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,32 +420,35 @@ describe('Validator', () => {
});
});

describe('with enforeced contract-level natspec', () => {
describe('with enforced contract-level natspec', () => {
beforeAll(() => {
validator = new Validator(mockConfig({ contractNatspec: true }));
node = contract;
});

it('should reveal missing natspec for a contract if enabled', () => {
const result = validator.validate(node, mockNatspec({}));
expect(result).toContainEqual(`Contract @notice is missing`);
const result = validator.validate(contract, mockNatspec({}));
expect(result).toContainEqual(`@notice is missing`);
});

it('should pay attention only to the @notice tag', () => {
const result = validator.validate(node, mockNatspec({ tags: [{ name: 'author', content: 'Some author' }] }));
expect(result).toContainEqual(`Contract @notice is missing`);
const result = validator.validate(
contract,
mockNatspec({
tags: [{ name: 'author', content: 'Some author' }],
})
);
expect(result).toContainEqual(`@notice is missing`);
expect(result.length).toBe(1);
});
});

describe('with disabled contract-level natspec', () => {
beforeAll(async () => {
validator = new Validator(mockConfig({ contractNatspec: false }));
node = contract;
});

it('should ignore missing natspec for a contract if disabled (by default)', () => {
const result = validator.validate(node, mockNatspec({}));
const result = validator.validate(contract, mockNatspec({}));
expect(result).toEqual([]);
});
});
Expand Down

0 comments on commit e224bbc

Please sign in to comment.