From a4e2151db80f044fe73be6b6f206040587bd4b61 Mon Sep 17 00:00:00 2001 From: Ivan Mushketyk Date: Thu, 5 Apr 2018 21:35:19 +0100 Subject: [PATCH 1/2] The "pragma-on-top" rule now can fix location of "pragma experimental" statements --- lib/rules/pragma-on-top.js | 12 ++++---- test/lib/rules/pragma-on-top/pragma-on-top.js | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/rules/pragma-on-top.js b/lib/rules/pragma-on-top.js index 76ebbe5..0bb1082 100644 --- a/lib/rules/pragma-on-top.js +++ b/lib/rules/pragma-on-top.js @@ -86,14 +86,16 @@ module.exports = { return; } - // The moment we find 1 node not allowed above exp. pragma, report and exit. - // TODO: write fix() for this issue: - // - Remove exp. pragma from current position - // - Place it right before childNode.start + // We found the first node not allowed above experimental pragma, report and exit. + const pragmaCode = context.getSourceCode().getText(node); if (nodesAllowedAbove.indexOf(childNode.type) < 0) { return context.report({ node, - message: "Experimental Pragma must precede everything except Solidity Pragma." + message: "Experimental Pragma must precede everything except Solidity Pragma.", + fix: function(fixer) { + return [fixer.remove(node), + fixer.insertTextBefore(childNode, pragmaCode + "\n")]; + } }); } } diff --git a/test/lib/rules/pragma-on-top/pragma-on-top.js b/test/lib/rules/pragma-on-top/pragma-on-top.js index b7c72f6..9a55d07 100644 --- a/test/lib/rules/pragma-on-top/pragma-on-top.js +++ b/test/lib/rules/pragma-on-top/pragma-on-top.js @@ -188,4 +188,32 @@ describe("[RULE] pragma-on-top: Fixes", function() { done(); }); + it("should move an existing experimental pragma statement to top of file (above all code) when fix is enabled", function(done) { + let config = { + "rules": { + "pragma-on-top": "error" + } + }; + + let unfixedCode = "pragma solidity ^0.4.0;\ncontract Foo {}\npragma experimental \"^0.5.0\";"; + + let fixedCode = "pragma solidity ^0.4.0;\npragma experimental \"^0.5.0\";\ncontract Foo {}\n"; + + let fixed = Solium.lintAndFix(unfixedCode, config); + + fixed.should.be.type("object"); + fixed.should.have.ownProperty("fixedSourceCode"); + fixed.should.have.ownProperty("errorMessages"); + fixed.should.have.ownProperty("fixesApplied"); + + fixed.fixedSourceCode.should.equal(fixedCode); + fixed.errorMessages.should.be.Array(); + fixed.errorMessages.length.should.equal(0); + fixed.fixesApplied.should.be.Array(); + fixed.fixesApplied.length.should.equal(1); + + Solium.reset(); + done(); + }); + }); From 916ce362d315512c60584245a691619b863f2a9e Mon Sep 17 00:00:00 2001 From: duaraghav8 Date: Sat, 14 Apr 2018 20:58:15 +0530 Subject: [PATCH 2/2] refactor --- lib/rules/pragma-on-top.js | 19 ++++++++++++------- test/lib/rules/pragma-on-top/pragma-on-top.js | 16 ++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/rules/pragma-on-top.js b/lib/rules/pragma-on-top.js index 0bb1082..7adb168 100644 --- a/lib/rules/pragma-on-top.js +++ b/lib/rules/pragma-on-top.js @@ -5,6 +5,8 @@ "use strict"; +const { EOL } = require("os"); + module.exports = { meta: { @@ -62,7 +64,7 @@ module.exports = { message: `"${pragmaCode}" should be at the top of the file.`, fix: function(fixer) { return [fixer.remove(node), - fixer.insertTextBefore(pragmaParent.body [0], pragmaCode + "\n")]; + fixer.insertTextBefore(pragmaParent.body [0], `${pragmaCode}${EOL}`)]; } }); @@ -88,15 +90,18 @@ module.exports = { // We found the first node not allowed above experimental pragma, report and exit. const pragmaCode = context.getSourceCode().getText(node); + if (nodesAllowedAbove.indexOf(childNode.type) < 0) { - return context.report({ + const errObject = { node, - message: "Experimental Pragma must precede everything except Solidity Pragma.", - fix: function(fixer) { + fix(fixer) { return [fixer.remove(node), - fixer.insertTextBefore(childNode, pragmaCode + "\n")]; - } - }); + fixer.insertTextBefore(childNode, `${pragmaCode}${EOL}`)]; + }, + message: "Experimental Pragma must precede everything except Solidity Pragma." + }; + + return context.report(errObject); } } } diff --git a/test/lib/rules/pragma-on-top/pragma-on-top.js b/test/lib/rules/pragma-on-top/pragma-on-top.js index 9a55d07..f906917 100644 --- a/test/lib/rules/pragma-on-top/pragma-on-top.js +++ b/test/lib/rules/pragma-on-top/pragma-on-top.js @@ -5,7 +5,8 @@ "use strict"; -let Solium = require("../../../../lib/solium"); +const { EOL } = require("os"); +const Solium = require("../../../../lib/solium"); let userConfig = { "custom-rules-filename": null, @@ -188,16 +189,15 @@ describe("[RULE] pragma-on-top: Fixes", function() { done(); }); - it("should move an existing experimental pragma statement to top of file (above all code) when fix is enabled", function(done) { - let config = { + it("should move an existing experimental pragma statement to top of file (above all code) when fix is enabled", done => { + const config = { "rules": { "pragma-on-top": "error" } }; - let unfixedCode = "pragma solidity ^0.4.0;\ncontract Foo {}\npragma experimental \"^0.5.0\";"; - - let fixedCode = "pragma solidity ^0.4.0;\npragma experimental \"^0.5.0\";\ncontract Foo {}\n"; + const unfixedCode = `pragma solidity ^0.4.0;${EOL}contract Foo {}${EOL}pragma experimental \"^0.5.0\";`, + fixedCode = `pragma solidity ^0.4.0;${EOL}pragma experimental \"^0.5.0\";${EOL}contract Foo {}${EOL}`; let fixed = Solium.lintAndFix(unfixedCode, config); @@ -208,9 +208,9 @@ describe("[RULE] pragma-on-top: Fixes", function() { fixed.fixedSourceCode.should.equal(fixedCode); fixed.errorMessages.should.be.Array(); - fixed.errorMessages.length.should.equal(0); + fixed.errorMessages.should.be.empty(); fixed.fixesApplied.should.be.Array(); - fixed.fixesApplied.length.should.equal(1); + fixed.fixesApplied.should.have.size(1); Solium.reset(); done();