Skip to content

Commit

Permalink
Omit semicolon in for statements init and loop expressions (#69)
Browse files Browse the repository at this point in the history
* Omit semicolon in for statements init and loop expressions

* fix snap

* use omitSemicolon on VariableDeclarationStatement

* bump version
  • Loading branch information
Franco Victorio authored and mattiaerre committed Oct 31, 2018
1 parent e7b81dd commit 11363ca
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-solidity",
"version": "1.0.0-alpha.9",
"version": "1.0.0-alpha.10",
"description": "prettier plugin for solidity",
"main": "src",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ const parser = require('solidity-parser-antlr');
const parse = text => {
const parsed = parser.parse(text, { loc: true, range: true });
parsed.comments = extract(text);

parser.visit(parsed, {
ForStatement(ctx) {
if (ctx.initExpression) {
ctx.initExpression.omitSemicolon = true;
}
if (ctx.loopExpression) {
ctx.loopExpression.omitSemicolon = true;
}
}
});

return parsed;
};

Expand Down
6 changes: 2 additions & 4 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ function genericPrint(path, options, print) {
]);

case 'ExpressionStatement': {
const addSemicolon = path.getParentNode().type !== 'ForStatement';
return concat([
node.expression ? path.call(print, 'expression') : '',
addSemicolon ? ';' : ''
node.omitSemicolon ? '' : ';'
]);
}
case 'FunctionCall':
Expand Down Expand Up @@ -245,8 +244,7 @@ function genericPrint(path, options, print) {
if (node.initialValue) {
doc = concat([doc, ' = ', path.call(print, 'initialValue')]);
}
const addSemicolon = path.getParentNode().type !== 'ForStatement';
return concat([doc, addSemicolon ? ';' : '']);
return concat([doc, node.omitSemicolon ? '' : ';']);
}
case 'StateVariableDeclaration':
doc = concat(
Expand Down
6 changes: 6 additions & 0 deletions tests/Etc/Etc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ contract Contract {
function ifBlockInOneLine(uint a) returns (uint b) {
if (a < 0) b = 0x67; else if (a == 0) b = 0x12; else b = 0x78;
}

function forWithoutBlock() {
uint i;
uint sum;
for ( i = 0; i < 10; i++ ) sum += i;
}
}
12 changes: 12 additions & 0 deletions tests/Etc/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ contract Contract {
function ifBlockInOneLine(uint a) returns (uint b) {
if (a < 0) b = 0x67; else if (a == 0) b = 0x12; else b = 0x78;
}
function forWithoutBlock() {
uint i;
uint sum;
for ( i = 0; i < 10; i++ ) sum += i;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pragma solidity ^0.4.24;
Expand Down Expand Up @@ -61,6 +67,12 @@ contract Contract {
else if (a == 0) b = 0x12;
else b = 0x78;
}
function forWithoutBlock() {
uint i;
uint sum;
for (i = 0; i < 10; i++) sum += i;
}
}
`;

0 comments on commit 11363ca

Please sign in to comment.