Skip to content

Commit

Permalink
feat: install prettier and husky (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dristpunk authored Oct 26, 2023
1 parent c0116f5 commit 720bd4e
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- name: Update version
run: yarn version --new-version "0.0.0-${GITHUB_SHA::8}" --no-git-tag-version

- name: Publish
run: npm publish --access public --tag canary
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
cache: "yarn"

- name: Install dependencies
run: yarn --frozen-lockfile

- name: 'Create env file'
- name: "Create env file"
run: |
touch .env
echo MAINNET_RPC="${{ secrets.MAINNET_RPC }}" >> .env
Expand Down
12 changes: 6 additions & 6 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require": ["ts-node/register"],
"extension": [".ts"],
"spec": "test/unit/**/*.spec.ts",
"timeout": "10000",
"exit": true
}
"require": ["ts-node/register"],
"extension": [".ts"],
"spec": "test/unit/**/*.spec.ts",
"timeout": "10000",
"exit": true
}
22 changes: 22 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"overrides": [
{
"files": ["**.ts", "**.js"],
"options": {
"printWidth": 145,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"useTabs": false,
"endOfLine": "auto"
}
},
{
"files": "**.json",
"options": {
"tabWidth": 2,
"printWidth": 200
}
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ vm.allowCheatcodes(address(mock_myContract));
```

3. Enjoy of easy mock calls

```JavaScript
/// Mock myFuncName function, when called with `arg1`, `arg2` to return `return1`
mock_myContract.mock_call_myFuncName(arg1, arg2, return1);
Expand Down
2 changes: 0 additions & 2 deletions copy-templates.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const fs = require('fs');
const path = require('path');

// prettier-ignore

/**
* Copies the mockContractTemplate.hbs file from the src/templates folder to the dist/templates folder
*/
Expand Down
15 changes: 3 additions & 12 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"ignore": [
"node_modules",
".git",
"lib",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.sol"
],
"watch": [
"src/**/*"
],
"ignore": ["node_modules", ".git", "lib", "**/*.test.ts", "**/*.spec.ts", "**/*.sol"],
"watch": ["src/**/*"],
"ext": "ts",
"exec": "yarn mock-gen --contractsDir ./solidity/contracts --outDir ./out"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"@defi-wonderland/solidity-utils": "0.0.0-4298c6c6",
"handlebars": "4.7.7",
"prettier": "^3.0.3",
"yargs": "17.7.2"
},
"devDependencies": {
Expand All @@ -51,7 +52,7 @@
"chai-as-promised": "7.1.1",
"cross-env": "7.0.3",
"eslint": "8.40.0",
"husky": "8.0.3",
"husky": "^8.0.3",
"lint-staged": "10",
"mocha": "10.2.0",
"nodemon": "2.0.22",
Expand Down
32 changes: 10 additions & 22 deletions src/get-constructor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ContractDefinitionNode,
FunctionDefinitionNode,
VariableDeclarationNode,
} from './types';
import { ContractDefinitionNode, FunctionDefinitionNode, VariableDeclarationNode } from './types';

/**
* Return the constructor of the contract
Expand All @@ -12,23 +8,18 @@ import {
export const getConstructor = (contractNode: ContractDefinitionNode): string => {
// Get the contract's name
const contractName: string = contractNode.name;

// Filter the nodes and keep only the FunctionDefinition related ones
const functionNodes = contractNode.nodes.filter(
(node) => node.nodeType === 'FunctionDefinition'
) as FunctionDefinitionNode[];
if(!functionNodes) return;
const functionNodes = contractNode.nodes.filter((node) => node.nodeType === 'FunctionDefinition') as FunctionDefinitionNode[];
if (!functionNodes) return;

// Find the node from the functionNodes that is the constructor
const constructorNode = functionNodes.find(
node => node.kind === 'constructor'
) as FunctionDefinitionNode;
const constructorNode = functionNodes.find((node) => node.kind === 'constructor') as FunctionDefinitionNode;
if (!constructorNode || constructorNode.kind !== 'constructor') return;

// Get the parameters of the constructor, if there are no parameters then we use an empty array
const parameters: VariableDeclarationNode[] =
constructorNode.parameters.parameters ? constructorNode.parameters.parameters : [];

const parameters: VariableDeclarationNode[] = constructorNode.parameters.parameters ? constructorNode.parameters.parameters : [];

// We save the parameters in an array with their types and storage location
const mockConstructorParameters: string[] = [];
// We save the parameters names in an other array
Expand All @@ -37,11 +28,8 @@ export const getConstructor = (contractNode: ContractDefinitionNode): string =>
parameters.forEach((parameter) => {
// If the storage location is memory or calldata then we keep it
const storageLocation =
parameter.storageLocation === 'memory' ||
parameter.storageLocation === 'calldata'
? `${parameter.storageLocation} `
: '';

parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata' ? `${parameter.storageLocation} ` : '';

// We remove the 'contract ' string from the type name if it exists
const typeName: string = parameter.typeDescriptions.typeString.replace(/contract |struct |enum /g, '');

Expand Down
23 changes: 5 additions & 18 deletions src/get-external-functions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
ContractDefinitionNode,
FunctionDefinitionNode,
VariableDeclarationNode,
ExternalFunctionOptions,
} from './types';
import { ContractDefinitionNode, FunctionDefinitionNode, VariableDeclarationNode, ExternalFunctionOptions } from './types';

/**
* Returns the infomration of the external function for the mock contract
Expand All @@ -12,9 +7,7 @@ import {
*/
export const getExternalMockFunctions = (contractNode: ContractDefinitionNode): ExternalFunctionOptions[] => {
// Filter the nodes and keep only the FunctionDefinition related ones
const functionNodes = contractNode.nodes.filter(
(node) => node.nodeType === 'FunctionDefinition'
) as FunctionDefinitionNode[];
const functionNodes = contractNode.nodes.filter((node) => node.nodeType === 'FunctionDefinition') as FunctionDefinitionNode[];

const externalFunctions: ExternalFunctionOptions[] = [];
// Loop through the function nodes
Expand All @@ -37,9 +30,7 @@ export const getExternalMockFunctions = (contractNode: ContractDefinitionNode):
parameters.forEach((parameter: VariableDeclarationNode) => {
// If the storage location is memory or calldata then we keep it
const storageLocation =
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata'
? `${parameter.storageLocation} `
: '';
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata' ? `${parameter.storageLocation} ` : '';

// We remove the 'contract ' string from the type name if it exists
const typeName: string = parameter.typeDescriptions.typeString.replace(/contract |struct |enum /g, '');
Expand All @@ -56,9 +47,7 @@ export const getExternalMockFunctions = (contractNode: ContractDefinitionNode):

const signature = parameterTypes ? `${funcNode.name}(${parameterTypes.join(',')})` : `${funcNode.name}()`;

const returnParameters: VariableDeclarationNode[] = funcNode.returnParameters.parameters
? funcNode.returnParameters.parameters
: [];
const returnParameters: VariableDeclarationNode[] = funcNode.returnParameters.parameters ? funcNode.returnParameters.parameters : [];

// We save the return parameters in an array with their types and storage location
const functionReturnParameters: string[] = [];
Expand All @@ -68,9 +57,7 @@ export const getExternalMockFunctions = (contractNode: ContractDefinitionNode):
returnParameters.forEach((parameter: VariableDeclarationNode) => {
// If the storage location is memory or calldata then we keep it
const storageLocation =
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata'
? `${parameter.storageLocation} `
: '';
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata' ? `${parameter.storageLocation} ` : '';

// We remove the 'contract ' string from the type name if it exists
const typeName: string = parameter.typeDescriptions.typeString.replace(/contract |struct |enum /g, '');
Expand Down
8 changes: 2 additions & 6 deletions src/get-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { Ast, ImportDirectiveNode } from './types';
*/
export const getImports = (ast: Ast): string[] => {
// Filter the nodes and keep only the ImportDirective related ones
const importNodes = ast.nodes.filter(
node => node.nodeType === 'ImportDirective'
) as ImportDirectiveNode[];
const importNodes = ast.nodes.filter((node) => node.nodeType === 'ImportDirective') as ImportDirectiveNode[];

// Create the import code for every node and save it in the importStatements array
const importStatements: string[] = importNodes.map((importDirective) => {
Expand All @@ -20,9 +18,7 @@ export const getImports = (ast: Ast): string[] => {
return `import '${absolutePath}';`;
}
// Get the names of the named imports
const imports = symbolAliases.map(
(symbolAlias) => symbolAlias.foreign.name
);
const imports = symbolAliases.map((symbolAlias) => symbolAlias.foreign.name);

return `import {${imports.join(', ')}} from '${absolutePath}';`;
});
Expand Down
23 changes: 5 additions & 18 deletions src/get-internal-functions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
ContractDefinitionNode,
FunctionDefinitionNode,
VariableDeclarationNode,
InternalFunctionOptions,
} from './types';
import { ContractDefinitionNode, FunctionDefinitionNode, VariableDeclarationNode, InternalFunctionOptions } from './types';

/**
* Returns the infomration of the internal function for the mock contract
Expand All @@ -12,9 +7,7 @@ import {
*/
export const getInternalMockFunctions = (contractNode: ContractDefinitionNode): InternalFunctionOptions[] => {
// Filter the nodes and keep only the FunctionDefinition related ones
const functionNodes = contractNode.nodes.filter(
(node) => node.nodeType === 'FunctionDefinition'
) as FunctionDefinitionNode[];
const functionNodes = contractNode.nodes.filter((node) => node.nodeType === 'FunctionDefinition') as FunctionDefinitionNode[];

const internalFunctions: InternalFunctionOptions[] = [];
// Loop through the function nodes
Expand All @@ -37,9 +30,7 @@ export const getInternalMockFunctions = (contractNode: ContractDefinitionNode):
parameters.forEach((parameter: VariableDeclarationNode) => {
// If the storage location is memory or calldata then we keep it
const storageLocation =
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata'
? `${parameter.storageLocation} `
: '';
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata' ? `${parameter.storageLocation} ` : '';

// We remove the 'contract ' string from the type name if it exists
// We remove '[]' at the end of array types
Expand All @@ -57,9 +48,7 @@ export const getInternalMockFunctions = (contractNode: ContractDefinitionNode):

const signature = parameterTypes ? `${funcNode.name}(${parameterTypes.join(',')})` : `${funcNode.name}()`;

const returnParameters: VariableDeclarationNode[] = funcNode.returnParameters.parameters
? funcNode.returnParameters.parameters
: [];
const returnParameters: VariableDeclarationNode[] = funcNode.returnParameters.parameters ? funcNode.returnParameters.parameters : [];

// We save the return parameters in an array with their types and storage location
const functionReturnParameters: string[] = [];
Expand All @@ -72,9 +61,7 @@ export const getInternalMockFunctions = (contractNode: ContractDefinitionNode):
returnParameters.forEach((parameter: VariableDeclarationNode) => {
// If the storage location is memory or calldata then we keep it
const storageLocation =
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata'
? `${parameter.storageLocation} `
: '';
parameter.storageLocation === 'memory' || parameter.storageLocation === 'calldata' ? `${parameter.storageLocation} ` : '';

// We remove the 'contract ' string from the type name if it exists
const typeName: string = parameter.typeDescriptions.typeString.replace(/contract |struct |enum /g, '');
Expand Down
27 changes: 6 additions & 21 deletions src/get-variables-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import { typeFix } from './utils';
*/
export const getStateVariables = (contractNode: ContractDefinitionNode): StateVariablesOptions => {
// Filter the nodes and keep only the VariableDeclaration related ones
const stateVariableNodes = contractNode.nodes.filter(
(node) => node.nodeType === 'VariableDeclaration'
) as VariableDeclarationNode[];
const stateVariableNodes = contractNode.nodes.filter((node) => node.nodeType === 'VariableDeclaration') as VariableDeclarationNode[];

// Define arrays to save our data
const mappingFunctions: MappingStateVariableOptions[] = [];
Expand Down Expand Up @@ -45,8 +43,7 @@ export const getStateVariables = (contractNode: ContractDefinitionNode): StateVa
const arrayMockFunction: ArrayStateVariableOptions = getArrayFunction(stateVariableNode);
arrayFunctions.push(arrayMockFunction);
} else {
const basicStateVariableMockFunction: BasicStateVariableOptions =
getBasicStateVariableFunction(stateVariableNode);
const basicStateVariableMockFunction: BasicStateVariableOptions = getBasicStateVariableFunction(stateVariableNode);
basicStateVariableFunctions.push(basicStateVariableMockFunction);
}
});
Expand Down Expand Up @@ -78,10 +75,7 @@ function getArrayFunction(arrayNode: VariableDeclarationNode): ArrayStateVariabl
const isStruct: boolean = arrayNode.typeName.baseType.typeDescriptions.typeString.includes('struct ');

// compile base type
const baseType: string = typeFix(arrayNode.typeName.baseType.typeDescriptions.typeString).replace(
/contract |struct |enum /g,
''
);
const baseType: string = typeFix(arrayNode.typeName.baseType.typeDescriptions.typeString).replace(/contract |struct |enum /g, '');

// If the array is internal we don't create mockCall for it
const isInternal: boolean = arrayNode.visibility == 'internal';
Expand Down Expand Up @@ -115,16 +109,10 @@ function getMappingFunction(mappingNode: VariableDeclarationNode): MappingStateV
// Name of the mapping
const mappingName: string = mappingNode.name;
// Type name
const keyType: string = typeFix(mappingNode.typeName.keyType.typeDescriptions.typeString).replace(
/contract |struct |enum /g,
''
);
const keyType: string = typeFix(mappingNode.typeName.keyType.typeDescriptions.typeString).replace(/contract |struct |enum /g, '');

// Value type
const valueType: string = typeFix(mappingNode.typeName.valueType.typeDescriptions.typeString).replace(
/contract |struct |enum /g,
''
);
const valueType: string = typeFix(mappingNode.typeName.valueType.typeDescriptions.typeString).replace(/contract |struct |enum /g, '');
// If the mapping is internal we don't create mockCall for it
const isInternal: boolean = mappingNode.visibility == 'internal';

Expand Down Expand Up @@ -157,10 +145,7 @@ function getBasicStateVariableFunction(variableNode: VariableDeclarationNode): B
const variableName: string = variableNode.name;

// remove spec type leading string
const variableType: string = typeFix(variableNode.typeDescriptions.typeString).replace(
/contract |struct |enum /g,
''
);
const variableType: string = typeFix(variableNode.typeDescriptions.typeString).replace(/contract |struct |enum /g, '');

// If the variable is internal we don't create mockCall for it
const isInternal: boolean = variableNode.visibility == 'internal';
Expand Down
Loading

0 comments on commit 720bd4e

Please sign in to comment.