Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merge internal functions templates #75

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export function internalFunctionContext(node: FunctionDefinition): InternalFunct
}

// Check if the function is view or pure
const isView = node.stateMutability === FunctionStateMutability.View;
const isPure = node.stateMutability === FunctionStateMutability.Pure;
const isView = node.stateMutability === FunctionStateMutability.View || isPure;

// Save the internal function information
return {
Expand Down
22 changes: 20 additions & 2 deletions src/templates/partials/internal-function.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function mock_call_{{functionName}}({{parameters}}) public {
);
}

function {{functionName}}({{inputs}}) internal override {{#if outputs}}returns ({{outputs}}){{/if}} {
(bool _success, bytes memory _data) = address(this).call(abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}}));
function {{functionName}}{{#if isPure}}Helper{{/if}}({{inputs}}) internal {{#if isView}}view{{/if}} {{#unless isPure}}override{{/unless}} {{#if outputs}}returns ({{outputs}}){{/if}} {
(bool _success, bytes memory _data) = address(this).{{#if isView}}static{{/if}}call(abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}}));

if (_success) return abi.decode(_data, ({{#each outputTypes}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}));

Expand All @@ -23,3 +23,21 @@ function call_{{functionName}}({{inputs}}) public {{#if outputs}}returns ({{outp
function expectCall_{{functionName}}({{#if inputs}}{{inputs}}{{/if}}) public {
vm.expectCall(address(this), abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}}));
}

{{#if isPure}}

function _{{functionName}}CastToPure(function({{#each inputTypes}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}) internal view {{#if outputs}}returns ({{explicitOutputTypes}}){{/if}} fnIn)
internal
pure
returns (function({{#each inputTypes}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}) internal pure {{#if outputs}}returns ({{explicitOutputTypes}}){{/if}} fnOut)
{
assembly {
fnOut := fnIn
}
}

function {{functionName}}({{inputs}}) internal pure override {{#if outputs}}returns ({{outputs}}){{/if}} {
return _{{functionName}}CastToPure({{functionName}}Helper)({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}});
}

{{/if}}
43 changes: 0 additions & 43 deletions src/templates/partials/internal-view-function.hbs

This file was deleted.

7 changes: 1 addition & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
* @param templatePath The path of the template (if it's nested)
* @returns The compiled template
*/
export function compileTemplate(templateName: string, templatePath?: string[]): HandlebarsTemplateDelegate<any> {

Check warning on line 77 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Unexpected any. Specify a different type
const templateContent = readTemplate(templateName, templatePath);
return Handlebars.compile(templateContent, { noEscape: true });
}
Expand All @@ -83,7 +83,7 @@
* Gets the base contract template
* @returns The contract template
*/
export function getContractTemplate(): HandlebarsTemplateDelegate<any> {

Check warning on line 86 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Unexpected any. Specify a different type
return compileTemplate('contract-template');
}

Expand All @@ -91,7 +91,7 @@
* Gets the smock helper template
* @returns The helper template
*/
export function getSmockHelperTemplate(): HandlebarsTemplateDelegate<any> {

Check warning on line 94 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Unexpected any. Specify a different type
return compileTemplate('helper-template');
}

Expand Down Expand Up @@ -192,7 +192,6 @@
constructor: constructorContext,
'external-or-public-function': externalOrPublicFunctionContext,
'internal-function': internalFunctionContext,
'internal-view-function': internalFunctionContext,
import: importContext,
};

Expand Down Expand Up @@ -222,11 +221,7 @@
} else if (node.visibility === 'external' || node.visibility === 'public') {
return 'external-or-public-function';
} else if (node.visibility === 'internal' && node.virtual) {
if (node.stateMutability === 'view' || node.stateMutability === 'pure') {
return 'internal-view-function';
} else {
return 'internal-function';
}
return 'internal-function';
}
} else if (node instanceof ImportDirective) {
return 'import';
Expand Down Expand Up @@ -269,7 +264,7 @@
const regex = /remappings[\s|\n]*=[\s\n]*\[(?<remappings>[^\]]+)]/;
const matches = foundryConfigContent.match(regex);
if (matches) {
return matches

Check warning on line 267 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Forbidden non-null assertion
.groups!.remappings.split(',')
.map((line) => line.trim())
.map((line) => line.replace(/["']/g, ''))
Expand Down
3 changes: 2 additions & 1 deletion test/unit/context/internalFunctionContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ describe('internalFunctionContext', () => {
it('determines whether the function is view or not', () => {
for (const stateMutability in FunctionStateMutability) {
const isView = FunctionStateMutability[stateMutability] === FunctionStateMutability.View;
const isPure = FunctionStateMutability[stateMutability] === FunctionStateMutability.Pure;
const node = mockFunctionDefinition({ ...defaultAttributes, stateMutability: FunctionStateMutability[stateMutability] });
const context = internalFunctionContext(node);
expect(context.isView).to.be.equal(isView);
expect(context.isView).to.be.equal(isView || isPure);
}
});

Expand Down
Loading