Skip to content

Commit

Permalink
Merge pull request #256 from Aniket-Engg/fix/#240
Browse files Browse the repository at this point in the history
Quotes in return() string can be linted and fixed
  • Loading branch information
Raghav Dua authored Feb 3, 2019
2 parents 3b95a04 + 7e1f272 commit 0ae7665
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
15 changes: 10 additions & 5 deletions lib/rules/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"use strict";

let jsStringEscape = require("js-string-escape");
const jsStringEscape = require("js-string-escape");

/**
* Determine whether the provided literal is in Hex Notation
Expand Down Expand Up @@ -51,7 +51,7 @@ module.exports = {
quoteStyle = "single";
}

const selectedQuoteStyleLiteralRegExp = new RegExp("^\\" + quote + ".*\\" + quote + "$");
const selectedQuoteStyleLiteralRegExp = new RegExp("^(\\(\\s*)?\\" + quote + ".*\\" + quote + "(\\s*\\))?$");


function inspectLiteral(emitted) {
Expand All @@ -63,13 +63,18 @@ module.exports = {
}

if (!selectedQuoteStyleLiteralRegExp.test(nodeText)) {
let fixedString = quote + jsStringEscape(node.value) + quote;
const errorObject = {
node,
fix(fixer) {
return fixer.replaceText(node, fixedString);
const fixedString = quote + jsStringEscape(node.value) + quote,
currentQuote = (quote === "'" ? "\"" : "'");
const openingQuoteI = nodeText.indexOf(currentQuote),
closingQuoteI = nodeText.lastIndexOf(currentQuote);
const fixedNodeText = nodeText.slice(0, openingQuoteI) + fixedString + nodeText.slice(closingQuoteI+1);

return fixer.replaceText(node, fixedNodeText);
},
message: `'${node.value}': String literal must be quoted with ${quoteStyle} quotes.`
message: `String literal must be quoted with ${quoteStyle} quotes.`
};

context.report(errorObject);
Expand Down
13 changes: 12 additions & 1 deletion test/lib/rules/quotes/double-full.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ import "../myContract.sol" as MyContract;

contract Foo {
string fucker = "hello wordl";
function f () {
function f () returns (string){
var foobar = "Hello world";
string fuu = "chumma";

var x = ( "hello world" );
var x = ( "hello world" );
var x = ( "hello world");
var x = ("hello world" );

return ("Hello World");

return(
"Lorem Ipsum"
);
}
}
4 changes: 2 additions & 2 deletions test/lib/rules/quotes/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe("[RULE] quotes: Fix when double quotes are mandatory", function() {
fixed.errorMessages.should.be.Array();
fixed.errorMessages.length.should.equal(0);
fixed.fixesApplied.should.be.Array();
fixed.fixesApplied.length.should.equal(11);
fixed.fixesApplied.length.should.equal(17);

Solium.reset();
done();
Expand Down Expand Up @@ -189,7 +189,7 @@ describe("[RULE] quotes: Fix when single quotes are mandatory", function() {
fixed.errorMessages.should.be.Array();
fixed.errorMessages.length.should.equal(0);
fixed.fixesApplied.should.be.Array();
fixed.fixesApplied.length.should.equal(11);
fixed.fixesApplied.length.should.equal(17);

Solium.reset();
done();
Expand Down
13 changes: 12 additions & 1 deletion test/lib/rules/quotes/single-full.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ import '../myContract.sol' as MyContract;

contract Foo {
string fucker = 'hello wordl';
function f () {
function f () returns (string){
var foobar = 'Hello world';
string fuu = 'chumma';

var x = ( 'hello world' );
var x = ( 'hello world' );
var x = ( 'hello world');
var x = ('hello world' );

return ('Hello World');

return(
'Lorem Ipsum'
);
}
}

0 comments on commit 0ae7665

Please sign in to comment.