Skip to content

Commit

Permalink
fix: last fixes before first release (#16)
Browse files Browse the repository at this point in the history
* fix: added final touches

* refactor: types

* fix: don't warn about missing inheritdoc in interfaces

* fix: remove empty warnings from

* fix: do not require inheritdoc for constructor

---------

Co-authored-by: Gas One Cent <[email protected]>
  • Loading branch information
0xGorilla and gas1cent authored Jan 15, 2024
1 parent 8904313 commit 44f8e46
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 78 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Want to quickly check if your natspec smells?
Just run:

```
npx @defi-wonderland/natspec-smells --include src --exclude test --exclude scripts
npx @defi-wonderland/natspec-smells --include solidity --exclude solidity/(test|scripts)/**/*.sol
```

## Recommended setup
Expand All @@ -35,13 +35,13 @@ npx @defi-wonderland/natspec-smells --include src --exclude test --exclude scrip

```javascript
/**
* For full explanation of each supported config, make sure to check the Config type below
* List of supported options: https://github.com/defi-wonderland/natspec-smells?tab=readme-ov-file#options
*/

/** @type {import('@defi-wonderland/natspec-smells').Config} */
module.exports = {
include: "src",
exclude: ["tests", "scripts"],
include: "solidity",
exclude: ["solidity/(test|scripts)/**/*.sol"],
};
```

Expand Down Expand Up @@ -71,3 +71,4 @@ Natspec Smells was built with ❤️ by [Wonderland](https://defi.sucks).
Wonderland the largest core development group in web3. Our commitment is to a financial future that's open, decentralized, and accessible to all.

[DeFi sucks](https://defi.sucks), but Wonderland is here to make it better.
``
9 changes: 0 additions & 9 deletions example-natspec-smells.config.js

This file was deleted.

1 change: 0 additions & 1 deletion src/index.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { hideBin } from 'yargs/helpers';
import { globSync } from 'fast-glob';
import { getProjectCompiledSources } from './utils';
import { Processor } from './processor';
import { Config } from './types/config';
import { Config } from './types';
import { Validator } from './validator';

(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs/promises';
import { Validator } from './validator';
import { SourceUnit, FunctionDefinition, ContractDefinition } from 'solc-typed-ast';
import { NodeToProcess } from './types/solc-typed-ast.d';
import { NodeToProcess } from './types';
import { getLineNumberFromSrc, parseNodeNatspec } from './utils';

interface IWarning {
Expand All @@ -27,7 +27,7 @@ export class Processor {
for (const node of nodes) {
// Find warning messages of the natspec of the node
const messages = this.validateNatspec(node);
if (messages) {
if (messages.length) {
// Add the warning messages to the list together with the natspec location
warnings.push({
location: this.formatLocation(sourceUnit.absolutePath, fileContent, contract, node),
Expand Down
51 changes: 51 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
EnumDefinition,
ErrorDefinition,
EventDefinition,
FunctionDefinition,
ModifierDefinition,
StructDefinition,
VariableDeclaration,
} from 'solc-typed-ast';

export interface Config {
include: string; // Required: Glob pattern of files to process.
exclude: string[]; // Optional: Glob patterns of files to exclude.
root: string; // Optional: Project root directory.
enforceInheritdoc: boolean; // Optional: True if all external and public functions should have @inheritdoc.
constructorNatspec: boolean; // Optional: True if the constructor should have natspec.
}

export interface NatspecDefinition {
name?: string;
content: string;
}

export interface Natspec {
inheritdoc?: NatspecDefinition;
tags: NatspecDefinition[];
params: NatspecDefinition[];
returns: NatspecDefinition[];
}

export interface ASTNodeRawDocumentation {
id: number;
nodeType: string;
src: string;
text: string;
}

export interface ASTNodeRaw {
name: string;
kind: string;
documentation?: ASTNodeRawDocumentation;
}

export type NodeToProcess =
| FunctionDefinition
| EnumDefinition
| ErrorDefinition
| EventDefinition
| ModifierDefinition
| VariableDeclaration
| StructDefinition;
7 changes: 0 additions & 7 deletions src/types/config.d.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/types/natspec.d.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/types/solc-typed-ast.d.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'fs/promises';
import path from 'path';
import { ASTKind, ASTReader, SourceUnit, compileSol } from 'solc-typed-ast';
import { Natspec, NatspecDefinition } from './types/natspec.d';
import { NodeToProcess } from './types/solc-typed-ast.d';
import { Natspec, NatspecDefinition, NodeToProcess } from './types';

export async function getSolidityFiles(dir: string): Promise<string[]> {
let files = await fs.readdir(dir, { withFileTypes: true });
Expand Down
11 changes: 7 additions & 4 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Config } from './types/config';
import { Natspec } from './types/natspec';
import { NodeToProcess } from './types/solc-typed-ast';
import { Config, Natspec, NodeToProcess } from './types';
import {
EnumDefinition,
ErrorDefinition,
Expand All @@ -9,6 +7,7 @@ import {
ModifierDefinition,
StructDefinition,
VariableDeclaration,
ContractDefinition,
} from 'solc-typed-ast';

export class Validator {
Expand Down Expand Up @@ -97,14 +96,18 @@ export class Validator {
let _requiresInheritdoc: boolean = false;

// External or public function
_requiresInheritdoc ||= node instanceof FunctionDefinition && (node.visibility === 'external' || node.visibility === 'public');
_requiresInheritdoc ||=
node instanceof FunctionDefinition && (node.visibility === 'external' || node.visibility === 'public') && !node.isConstructor;

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

// Public variable
_requiresInheritdoc ||= node instanceof VariableDeclaration && node.visibility === 'public';

// The node is in a contract
_requiresInheritdoc &&= node.parent instanceof ContractDefinition && node.parent.kind === 'contract';

return _requiresInheritdoc;
}
}
3 changes: 1 addition & 2 deletions test/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Natspec } from '../src/types/natspec.d';
import { SourceUnit, ContractDefinition, FunctionDefinition } from 'solc-typed-ast';
import { NodeToProcess } from '../src/types/solc-typed-ast.d';
import { Natspec, NodeToProcess } from '../src/types';

export function mockNatspec(mockNatspec: Partial<Natspec>): Natspec {
const natspec: Natspec = {
Expand Down
2 changes: 1 addition & 1 deletion test/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from '../src/types/config';
import { ContractDefinition, FunctionDefinition, UserDefinedType, UsingForDirective } from 'solc-typed-ast';
import { Config } from '../src/types';
import { getFileCompiledSource } from './utils';
import { Processor } from '../src/processor';
import { Validator } from '../src/validator';
Expand Down
5 changes: 2 additions & 3 deletions test/validator.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ContractDefinition } from 'solc-typed-ast';
import { Validator } from '../src/validator';
import { getFileCompiledSource } from './utils';
import { NodeToProcess } from '../src/types/solc-typed-ast.d';
import { ContractDefinition } from 'solc-typed-ast';
import { Config } from '../src/types/config';
import { Config, NodeToProcess } from '../src/types';

describe('Validator', () => {
let contract: ContractDefinition;
Expand Down

0 comments on commit 44f8e46

Please sign in to comment.