diff --git a/package.json b/package.json index 89597cfe5..4677f1aa0 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/parser.js b/src/parser.js index ff2dd6e95..b66fef9b2 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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; }; diff --git a/src/printer.js b/src/printer.js index 35447e9c6..87cda58a8 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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': @@ -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( diff --git a/tests/Etc/Etc.sol b/tests/Etc/Etc.sol index 697f1ae98..9622d818e 100644 --- a/tests/Etc/Etc.sol +++ b/tests/Etc/Etc.sol @@ -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; + } } diff --git a/tests/Etc/__snapshots__/jsfmt.spec.js.snap b/tests/Etc/__snapshots__/jsfmt.spec.js.snap index 1bdfada9a..5ed9919e0 100644 --- a/tests/Etc/__snapshots__/jsfmt.spec.js.snap +++ b/tests/Etc/__snapshots__/jsfmt.spec.js.snap @@ -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; @@ -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; + } } `;