Skip to content

Commit 6fb5e7f

Browse files
authored
issue-with-loc (#47)
* printer WIP * housekeeping * Don't add semicolon in for statement loop condition * exclude storageLocation property * fix lint * Wrap case in a block * fix snap
1 parent bc6b7bd commit 6fb5e7f

File tree

7 files changed

+222
-14
lines changed

7 files changed

+222
-14
lines changed

src/clean.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// eslint-disable-next-line no-unused-vars
22
const clean = (ast, newObj, parent) => {
3-
['code', 'codeStart', 'loc', 'range'].forEach(name => {
3+
['code', 'codeStart', 'loc', 'range', 'storageLocation'].forEach(name => {
44
delete newObj[name]; // eslint-disable-line no-param-reassign
55
});
66
};

src/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
const massageAstNode = require('./clean');
2+
const loc = require('./loc');
13
const parse = require('./parser');
24
const print = require('./printer');
3-
const massageAstNode = require('./clean');
45

56
// https://prettier.io/docs/en/plugins.html#languages
67
const languages = [
@@ -12,13 +13,9 @@ const languages = [
1213
];
1314

1415
// https://prettier.io/docs/en/plugins.html#parsers
16+
const parser = Object.assign({}, { astFormat: 'solidity-ast', parse }, loc);
1517
const parsers = {
16-
'solidity-parse': {
17-
astFormat: 'solidity-ast',
18-
locEnd: node => node.range[1],
19-
locStart: node => node.range[0],
20-
parse
21-
}
18+
'solidity-parse': parser
2219
};
2320

2421
function canAttachComment(node) {

src/loc.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// see: https://github.com/prettier/prettier/blob/master/src/language-js/loc.js
2+
3+
const getRange = (index, node) => {
4+
if (node.range) {
5+
return node.range[index];
6+
}
7+
if (node.expression.range) {
8+
return node.expression.range[index];
9+
}
10+
return null;
11+
};
12+
13+
module.exports = {
14+
locEnd: node => getRange(1, node),
15+
locStart: node => getRange(0, node)
16+
};

src/printer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const prettier = require('prettier');
1+
/* eslint-disable no-nested-ternary, operator-linebreak */
22

33
const {
44
doc: {
55
builders: { concat, group, hardline, indent, join, line, softline }
66
},
77
util: { isNextLineEmptyAfterIndex }
8-
} = prettier;
8+
} = require('prettier');
99

1010
function printPreservingEmptyLines(path, key, options, print) {
1111
const parts = [];
@@ -160,11 +160,13 @@ function genericPrint(path, options, print) {
160160
');'
161161
]);
162162

163-
case 'ExpressionStatement':
163+
case 'ExpressionStatement': {
164+
const addSemicolon = path.getParentNode().type !== 'ForStatement';
164165
return concat([
165166
node.expression ? path.call(print, 'expression') : '',
166-
';'
167+
addSemicolon ? ';' : ''
167168
]);
169+
}
168170
case 'FunctionCall':
169171
if (node.names && node.names.length > 0) {
170172
doc = concat([
@@ -215,12 +217,12 @@ function genericPrint(path, options, print) {
215217
return concat([
216218
'for (',
217219
node.initExpression ? path.call(print, 'initExpression') : '',
220+
' ',
218221
node.conditionExpression ? path.call(print, 'conditionExpression') : '',
219222
'; ',
220223
path.call(print, 'loopExpression'),
221224
') ',
222-
path.call(print, 'body'),
223-
'}'
225+
path.call(print, 'body')
224226
]);
225227
case 'EmitStatement':
226228
return concat(['emit ', path.call(print, 'eventCall'), ';']);

tests/IndexOf/IndexOf.sol

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
contract IndexOf {
2+
3+
address creator;
4+
5+
function IndexOf()
6+
{
7+
creator = msg.sender;
8+
}
9+
10+
int whatwastheval = -10; // -2 = not yet tested, as separate from -1, tested but error
11+
12+
function indexOf(string _a, string _b) returns (int) // _a = string to search, _b = string we want to find
13+
{
14+
bytes memory a = bytes(_a);
15+
bytes memory b = bytes(_b);
16+
17+
if(a.length < 1 || b.length < 1 || (b.length > a.length))
18+
{
19+
whatwastheval = -1;
20+
return -1;
21+
}
22+
else if(a.length > (2**128 -1)) // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1)
23+
{
24+
whatwastheval = -1;
25+
return -1;
26+
}
27+
else
28+
{
29+
uint subindex = 0;
30+
for (uint i = 0; i < a.length; i ++)
31+
{
32+
if (a[i] == b[0]) // found the first char of b
33+
{
34+
subindex = 1;
35+
while(subindex < b.length && (i + subindex) < a.length && a[i + subindex] == b[subindex]) // search until the chars don't match or until we reach the end of a or b
36+
{
37+
subindex++;
38+
}
39+
if(subindex == b.length)
40+
{
41+
whatwastheval = int(i);
42+
return int(i);
43+
}
44+
}
45+
}
46+
whatwastheval = -1;
47+
return -1;
48+
}
49+
}
50+
51+
function whatWasTheVal() constant returns (int)
52+
{
53+
return whatwastheval;
54+
}
55+
56+
/**********
57+
Standard kill() function to recover funds
58+
**********/
59+
60+
function kill()
61+
{
62+
if (msg.sender == creator)
63+
{
64+
suicide(creator); // kills this contract and sends remaining funds back to creator
65+
}
66+
}
67+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`IndexOf.sol 1`] = `
4+
contract IndexOf {
5+
6+
address creator;
7+
8+
function IndexOf()
9+
{
10+
creator = msg.sender;
11+
}
12+
13+
int whatwastheval = -10; // -2 = not yet tested, as separate from -1, tested but error
14+
15+
function indexOf(string _a, string _b) returns (int) // _a = string to search, _b = string we want to find
16+
{
17+
bytes memory a = bytes(_a);
18+
bytes memory b = bytes(_b);
19+
20+
if(a.length < 1 || b.length < 1 || (b.length > a.length))
21+
{
22+
whatwastheval = -1;
23+
return -1;
24+
}
25+
else if(a.length > (2**128 -1)) // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1)
26+
{
27+
whatwastheval = -1;
28+
return -1;
29+
}
30+
else
31+
{
32+
uint subindex = 0;
33+
for (uint i = 0; i < a.length; i ++)
34+
{
35+
if (a[i] == b[0]) // found the first char of b
36+
{
37+
subindex = 1;
38+
while(subindex < b.length && (i + subindex) < a.length && a[i + subindex] == b[subindex]) // search until the chars don't match or until we reach the end of a or b
39+
{
40+
subindex++;
41+
}
42+
if(subindex == b.length)
43+
{
44+
whatwastheval = int(i);
45+
return int(i);
46+
}
47+
}
48+
}
49+
whatwastheval = -1;
50+
return -1;
51+
}
52+
}
53+
54+
function whatWasTheVal() constant returns (int)
55+
{
56+
return whatwastheval;
57+
}
58+
59+
/**********
60+
Standard kill() function to recover funds
61+
**********/
62+
63+
function kill()
64+
{
65+
if (msg.sender == creator)
66+
{
67+
suicide(creator); // kills this contract and sends remaining funds back to creator
68+
}
69+
}
70+
}
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
contract IndexOf {
73+
address creator;
74+
75+
function IndexOf() {
76+
creator = msg.sender;
77+
}
78+
79+
int whatwastheval = -10; // -2 = not yet tested, as separate from -1, tested but error
80+
81+
function indexOf(string _a, string _b) returns(int) { // _a = string to search, _b = string we want to find
82+
bytes a = bytes(_a);
83+
bytes b = bytes(_b);
84+
85+
if (a.length < 1 || b.length < 1 || (b.length > a.length)) {
86+
whatwastheval = -1;
87+
return -1;
88+
} else if (a.length > (2 ** 128 - 1)) { // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1)
89+
whatwastheval = -1;
90+
return -1;
91+
} else {
92+
uint subindex = 0;
93+
for (uint i = 0; i < a.length; i++) {
94+
if (a[i] == b[0]) { // found the first char of b
95+
subindex = 1;
96+
while (subindex < b.length && (i + subindex) < a.length && a[i + subindex] == b[subindex]) { // search until the chars don't match or until we reach the end of a or b
97+
subindex++;
98+
}
99+
if (subindex == b.length) {
100+
whatwastheval = int(i);
101+
return int(i);
102+
}
103+
}
104+
}
105+
whatwastheval = -1;
106+
return -1;
107+
}
108+
}
109+
110+
function whatWasTheVal() constant returns(int) {
111+
return whatwastheval;
112+
}
113+
114+
/**********
115+
Standard kill() function to recover funds
116+
**********/
117+
118+
function kill() {
119+
if (msg.sender == creator) {
120+
suicide(creator); // kills this contract and sends remaining funds back to creator
121+
}
122+
}
123+
}
124+
125+
`;

tests/IndexOf/jsfmt.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname);

0 commit comments

Comments
 (0)