-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: ignoring some node types * test: added processor tests
- Loading branch information
Showing
4 changed files
with
81 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity =0.8.19; | ||
|
||
library StringUtils { | ||
function nothing(string memory input) public pure returns (string memory) { | ||
return input; | ||
} | ||
} | ||
|
||
contract BasicSample { | ||
using StringUtils for string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { parseNodeNatspec } from "./parser"; | ||
import { SolcContractNode } from "./types/solc-typed-ast.t"; | ||
import { validate } from "./validator"; | ||
|
||
interface IWarning { | ||
location: string; | ||
messages: string[]; | ||
} | ||
|
||
// TODO: better check all the options here | ||
const ignoredNodeTypes = ['UsingForDirective']; | ||
|
||
export async function processSources(sources: any): Promise<IWarning[]> { | ||
let warnings: IWarning[] = []; | ||
|
||
for (const [fileName, source] of Object.entries(sources)) { | ||
if (fileName.startsWith('node_modules') || fileName.startsWith('lib')) continue; | ||
|
||
const fileContracts = (source as any).ast.nodes.filter((node: any) => node.nodeType === 'ContractDefinition'); | ||
fileContracts.forEach((contract: any) => { | ||
const nodes = contract.nodes as SolcContractNode[]; | ||
|
||
nodes | ||
.filter(node => !ignoredNodeTypes.includes(node.nodeType)) | ||
.forEach(node => { | ||
const nodeNatspec = parseNodeNatspec(node); | ||
const validationMessages = validate(node, nodeNatspec); | ||
const nodeName = node.name || node.kind; | ||
|
||
if (validationMessages.length) { | ||
warnings.push({ | ||
location: `${fileName}:${contract.name}:${nodeName}`, | ||
messages: validationMessages, | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
return warnings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { processSources } from '../src/processor'; | ||
import { parseSolidityFile } from './test-utils'; | ||
|
||
describe('processSources', () => { | ||
|
||
describe('LibrarySample.sol', () => { | ||
|
||
it('should return warnings only for the library method empty natspec', async () => { | ||
const sources = (await parseSolidityFile('sample-data/LibrarySample.sol')).data.sources; | ||
const warnings = await processSources(sources); | ||
expect(warnings).toEqual([ | ||
{ | ||
location: 'sample-data/LibrarySample.sol:StringUtils:nothing', | ||
messages: ['Natspec is missing'] | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |