Skip to content

Commit

Permalink
refactor regexp and account for whitespace betwene bracket and literal
Browse files Browse the repository at this point in the history
  • Loading branch information
duaraghav8 committed Feb 3, 2019
1 parent e716e51 commit 7e1f272
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
17 changes: 10 additions & 7 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 + "$" + "|^\\(\\" + quote + ".*\\" + quote + "\\)$");
const selectedQuoteStyleLiteralRegExp = new RegExp("^(\\(\\s*)?\\" + quote + ".*\\" + quote + "(\\s*\\))?$");


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

if (!selectedQuoteStyleLiteralRegExp.test(nodeText)) {
let fixedString = quote + jsStringEscape(node.value) + quote;
if(nodeText[0] == "(" && nodeText[nodeText.length - 1] == ")")
fixedString = "(" + fixedString + ")";
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
10 changes: 10 additions & 0 deletions test/lib/rules/quotes/double-full.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ contract Foo {
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(12);
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(12);
fixed.fixesApplied.length.should.equal(17);

Solium.reset();
done();
Expand Down
10 changes: 10 additions & 0 deletions test/lib/rules/quotes/single-full.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ contract Foo {
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 7e1f272

Please sign in to comment.