diff --git a/lib/rules/quotes.js b/lib/rules/quotes.js index 1f8fa34..8283349 100644 --- a/lib/rules/quotes.js +++ b/lib/rules/quotes.js @@ -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 @@ -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) { @@ -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); diff --git a/test/lib/rules/quotes/double-full.sol b/test/lib/rules/quotes/double-full.sol index 65c8814..44431db 100644 --- a/test/lib/rules/quotes/double-full.sol +++ b/test/lib/rules/quotes/double-full.sol @@ -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" + ); } } \ No newline at end of file diff --git a/test/lib/rules/quotes/quotes.js b/test/lib/rules/quotes/quotes.js index 42bbdb9..a4b81c2 100644 --- a/test/lib/rules/quotes/quotes.js +++ b/test/lib/rules/quotes/quotes.js @@ -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(); @@ -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(); diff --git a/test/lib/rules/quotes/single-full.sol b/test/lib/rules/quotes/single-full.sol index 34e8dce..3f60c3f 100644 --- a/test/lib/rules/quotes/single-full.sol +++ b/test/lib/rules/quotes/single-full.sol @@ -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' + ); } } \ No newline at end of file