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

receive and fallback support #244

Merged
merged 4 commits into from
Feb 29, 2020
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
19 changes: 15 additions & 4 deletions src/nodes/FunctionDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ const {

const printList = require('./print-list');

const functionName = node => {
const functionName = (node, options) => {
if (node.isConstructor && !node.name) return 'constructor';
if (node.name) return `function ${node.name}`;
return 'function';
if (node.isReceiveEther) return 'receive';
// The parser doesn't give us any information about the keyword used for the
// fallback.
// Using the originalText is the next best option.
// A neat idea would be to rely on the pragma and enforce it but for the
// moment this will do.
const names = { fallback: 'fallback', function: 'function' };
const name = options.originalText.slice(
options.locStart(node),
options.locStart(node) + 8
);
return names[name];
};

const parameters = (parametersType, node, path, print) =>
Expand Down Expand Up @@ -68,9 +79,9 @@ const body = (node, path, print) => {
};

const FunctionDefinition = {
print: ({ node, path, print }) =>
print: ({ node, path, print, options }) =>
concat([
functionName(node),
functionName(node, options),
'(',
parameters('parameters', node, path, print),
')',
Expand Down
7 changes: 7 additions & 0 deletions tests/FunctionDefinitions/FunctionDefinitions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ interface FunctionInterfaces {
}

contract FunctionDefinitions {
function () external {}
fallback () external {}

function () external payable {}
fallback () external payable {}
receive () external payable {}

function noParamsNoModifiersNoReturns() {
a = 1;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/FunctionDefinitions/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ interface FunctionInterfaces {
}

contract FunctionDefinitions {
function () external {}
fallback () external {}

function () external payable {}
fallback () external payable {}
receive () external payable {}

function noParamsNoModifiersNoReturns() {
a = 1;
}
Expand Down Expand Up @@ -323,6 +330,16 @@ interface FunctionInterfaces {


contract FunctionDefinitions {
function() external {}

fallback() external {}

function() external payable {}

fallback() external payable {}

receive() external payable {}

function noParamsNoModifiersNoReturns() {
a = 1;
}
Expand Down