Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"pragma-on-top" rule can fix position of "pragma experimental" statements #192

Merged
merged 2 commits into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/rules/pragma-on-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"use strict";

const { EOL } = require("os");

module.exports = {

meta: {
Expand Down Expand Up @@ -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}`)];
}
});

Expand All @@ -86,15 +88,20 @@ 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({
const errObject = {
node,
fix(fixer) {
return [fixer.remove(node),
fixer.insertTextBefore(childNode, `${pragmaCode}${EOL}`)];
},
message: "Experimental Pragma must precede everything except Solidity Pragma."
});
};

return context.report(errObject);
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion test/lib/rules/pragma-on-top/pragma-on-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -188,4 +189,31 @@ 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", done => {
const config = {
"rules": {
"pragma-on-top": "error"
}
};

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);

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.should.be.empty();
fixed.fixesApplied.should.be.Array();
fixed.fixesApplied.should.have.size(1);

Solium.reset();
done();
});

});