Skip to content

Commit 11363ca

Browse files
Franco Victoriomattiaerre
authored andcommitted
Omit semicolon in for statements init and loop expressions (#69)
* Omit semicolon in for statements init and loop expressions * fix snap * use omitSemicolon on VariableDeclarationStatement * bump version
1 parent e7b81dd commit 11363ca

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prettier-plugin-solidity",
3-
"version": "1.0.0-alpha.9",
3+
"version": "1.0.0-alpha.10",
44
"description": "prettier plugin for solidity",
55
"main": "src",
66
"scripts": {

src/parser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ const parser = require('solidity-parser-antlr');
55
const parse = text => {
66
const parsed = parser.parse(text, { loc: true, range: true });
77
parsed.comments = extract(text);
8+
9+
parser.visit(parsed, {
10+
ForStatement(ctx) {
11+
if (ctx.initExpression) {
12+
ctx.initExpression.omitSemicolon = true;
13+
}
14+
if (ctx.loopExpression) {
15+
ctx.loopExpression.omitSemicolon = true;
16+
}
17+
}
18+
});
19+
820
return parsed;
921
};
1022

src/printer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ function genericPrint(path, options, print) {
162162
]);
163163

164164
case 'ExpressionStatement': {
165-
const addSemicolon = path.getParentNode().type !== 'ForStatement';
166165
return concat([
167166
node.expression ? path.call(print, 'expression') : '',
168-
addSemicolon ? ';' : ''
167+
node.omitSemicolon ? '' : ';'
169168
]);
170169
}
171170
case 'FunctionCall':
@@ -245,8 +244,7 @@ function genericPrint(path, options, print) {
245244
if (node.initialValue) {
246245
doc = concat([doc, ' = ', path.call(print, 'initialValue')]);
247246
}
248-
const addSemicolon = path.getParentNode().type !== 'ForStatement';
249-
return concat([doc, addSemicolon ? ';' : '']);
247+
return concat([doc, node.omitSemicolon ? '' : ';']);
250248
}
251249
case 'StateVariableDeclaration':
252250
doc = concat(

tests/Etc/Etc.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ contract Contract {
2626
function ifBlockInOneLine(uint a) returns (uint b) {
2727
if (a < 0) b = 0x67; else if (a == 0) b = 0x12; else b = 0x78;
2828
}
29+
30+
function forWithoutBlock() {
31+
uint i;
32+
uint sum;
33+
for ( i = 0; i < 10; i++ ) sum += i;
34+
}
2935
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ contract Contract {
2929
function ifBlockInOneLine(uint a) returns (uint b) {
3030
if (a < 0) b = 0x67; else if (a == 0) b = 0x12; else b = 0x78;
3131
}
32+
33+
function forWithoutBlock() {
34+
uint i;
35+
uint sum;
36+
for ( i = 0; i < 10; i++ ) sum += i;
37+
}
3238
}
3339
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3440
pragma solidity ^0.4.24;
@@ -61,6 +67,12 @@ contract Contract {
6167
else if (a == 0) b = 0x12;
6268
else b = 0x78;
6369
}
70+
71+
function forWithoutBlock() {
72+
uint i;
73+
uint sum;
74+
for (i = 0; i < 10; i++) sum += i;
75+
}
6476
}
6577
6678
`;

0 commit comments

Comments
 (0)