Skip to content

Commit 1e48cff

Browse files
Janthermattiaerre
andauthored
receive and fallback support (#244)
* receive and fallback support * 2 more cases * A more explicit return of 'fallback' and 'function' * use a map Co-authored-by: mattia richetto <mattia.richetto@gmail.com>
1 parent 32cac96 commit 1e48cff

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/nodes/FunctionDefinition.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ const {
66

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

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

1526
const parameters = (parametersType, node, path, print) =>
@@ -49,9 +60,9 @@ const signatureEnd = node => (node.body ? dedent(line) : ';');
4960
const body = (node, path, print) => (node.body ? path.call(print, 'body') : '');
5061

5162
const FunctionDefinition = {
52-
print: ({ node, path, print }) =>
63+
print: ({ node, path, print, options }) =>
5364
concat([
54-
functionName(node),
65+
functionName(node, options),
5566
'(',
5667
parameters('parameters', node, path, print),
5768
')',

tests/FunctionDefinitions/FunctionDefinitions.sol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ interface FunctionInterfaces {
3737
}
3838

3939
contract FunctionDefinitions {
40+
function () external {}
41+
fallback () external {}
42+
43+
function () external payable {}
44+
fallback () external payable {}
45+
receive () external payable {}
46+
4047
function noParamsNoModifiersNoReturns() {
4148
a = 1;
4249
}

tests/FunctionDefinitions/__snapshots__/jsfmt.spec.js.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ interface FunctionInterfaces {
4040
}
4141
4242
contract FunctionDefinitions {
43+
function () external {}
44+
fallback () external {}
45+
46+
function () external payable {}
47+
fallback () external payable {}
48+
receive () external payable {}
49+
4350
function noParamsNoModifiersNoReturns() {
4451
a = 1;
4552
}
@@ -323,6 +330,16 @@ interface FunctionInterfaces {
323330
324331
325332
contract FunctionDefinitions {
333+
function() external {}
334+
335+
fallback() external {}
336+
337+
function() external payable {}
338+
339+
fallback() external payable {}
340+
341+
receive() external payable {}
342+
326343
function noParamsNoModifiersNoReturns() {
327344
a = 1;
328345
}

0 commit comments

Comments
 (0)