diff --git a/docs/rules.md b/docs/rules.md index 00b344b9..1f88d8a4 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -29,7 +29,7 @@ title: "Rule Index of Solhint" | Rule Id | Error | Recommended | Deprecated | | --------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---------- | | [comprehensive-interface](./rules/miscellaneous/comprehensive-interface.md) | Check that all public or external functions are override. This is iseful to make sure that the whole API is extracted in an interface. | | | -| [quotes](./rules/miscellaneous/quotes.md) | Use double quotes for string literals. Values must be 'single' or 'double'. | $~~~~~~~~$✔️ | | +| [quotes](./rules/miscellaneous/quotes.md) | Enforces the use of double or simple quotes as configured for string literals. Values must be 'single' or 'double'. | $~~~~~~~~$✔️ | | ## Style Guide Rules diff --git a/docs/rules/miscellaneous/quotes.md b/docs/rules/miscellaneous/quotes.md index e260f159..d2a0134b 100644 --- a/docs/rules/miscellaneous/quotes.md +++ b/docs/rules/miscellaneous/quotes.md @@ -12,7 +12,7 @@ title: "quotes | Solhint" ## Description -Use double quotes for string literals. Values must be 'single' or 'double'. +Enforces the use of double or simple quotes as configured for string literals. Values must be 'single' or 'double'. ## Options This rule accepts an array of options: @@ -32,11 +32,13 @@ This rule accepts an array of options: } ``` +### Notes +- This rule allows to put a double quote inside single quote string and viceversa ## Examples ### 👍 Examples of **correct** code for this rule -#### String with double quotes +#### Configured with double quotes ```solidity @@ -49,9 +51,47 @@ This rule accepts an array of options: ``` +#### Configured with single quotes + +```solidity + + pragma solidity 0.4.4; + + + contract A { + string private a = 'test'; + } + +``` + +#### Configured with double quotes + +```solidity +string private constant STR = "You shall 'pass' !"; +``` + +#### Configured with single quotes + +```solidity +string private constant STR = 'You shall "pass" !'; +``` + ### 👎 Examples of **incorrect** code for this rule -#### String with single quotes +#### Configured with single quotes + +```solidity + + pragma solidity 0.4.4; + + + contract A { + string private a = "test"; + } + +``` + +#### Configured with double quotes ```solidity diff --git a/lib/rules/miscellaneous/quotes.js b/lib/rules/miscellaneous/quotes.js index 4212ea38..5eff88d5 100644 --- a/lib/rules/miscellaneous/quotes.js +++ b/lib/rules/miscellaneous/quotes.js @@ -9,7 +9,7 @@ const meta = { type: 'miscellaneous', docs: { - description: `Use double quotes for string literals. Values must be 'single' or 'double'.`, + description: `Enforces the use of double or simple quotes as configured for string literals. Values must be 'single' or 'double'.`, category: 'Miscellaneous', options: [ { @@ -24,17 +24,38 @@ const meta = { examples: { good: [ { - description: 'String with double quotes', + description: 'Configured with double quotes', code: require('../../../test/fixtures/miscellaneous/string-with-double-quotes'), }, + { + description: 'Configured with single quotes', + code: require('../../../test/fixtures/miscellaneous/string-with-single-quotes'), + }, + { + description: 'Configured with double quotes', + code: 'string private constant STR = "You shall \'pass\' !";', + }, + { + description: 'Configured with single quotes', + code: 'string private constant STR = \'You shall "pass" !\';', + }, ], bad: [ { - description: 'String with single quotes', + description: 'Configured with single quotes', + code: require('../../../test/fixtures/miscellaneous/string-with-double-quotes'), + }, + { + description: 'Configured with double quotes', code: require('../../../test/fixtures/miscellaneous/string-with-single-quotes'), }, ], }, + notes: [ + { + note: 'This rule allows to put a double quote inside single quote string and viceversa', + }, + ], }, isDefault: false, @@ -64,7 +85,6 @@ class QuotesChecker extends BaseChecker { token.loc.start.line === node.loc.start.line && token.loc.start.column === node.loc.start.column ) - if (token && !this.alreadyVisited(token)) { this.addVisitedNode(token) this.validateQuotes(token) diff --git a/lib/rules/security/func-visibility.js b/lib/rules/security/func-visibility.js index 4f125df0..ea7b3632 100644 --- a/lib/rules/security/func-visibility.js +++ b/lib/rules/security/func-visibility.js @@ -70,11 +70,13 @@ class FuncVisibilityChecker extends BaseChecker { } FunctionDefinition(node) { + if (node.isConstructor && this.ignoreConstructors) { return } if (this.isContract) { + console.log("aaaaa"); if (node.visibility === 'default') { this.warn( node, diff --git a/package.json b/package.json index 7c53d289..a0d0b55e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "solhint", - "version": "3.6.1", + "version": "3.6.2", "description": "Solidity Code Linter", "main": "lib/index.js", "keywords": [ diff --git a/test/rules/miscellaneous/quotes.js b/test/rules/miscellaneous/quotes.js index 13bcc36f..bb1da18e 100644 --- a/test/rules/miscellaneous/quotes.js +++ b/test/rules/miscellaneous/quotes.js @@ -90,4 +90,34 @@ describe('Linter - quotes', () => { assertErrorCount(reports[0], 1) }) + + describe('Double quotes inside single and viceversa', () => { + it('should not raise error when configured as single and there are double quotes inside', function test() { + const code = contractWith('string private constant STR = \'You shall "pass" !\';') + + const report = linter.processStr(code, { rules: { quotes: ['error', 'single'] } }) + assertNoErrors(report) + }) + + it('Should raise error when configured as single and string is with double quotes despite content', function test() { + const code = contractWith('string private constant STR = "You shall \'NOT\' pass";') + + const report = linter.processStr(code, { rules: { quotes: ['error', 'single'] } }) + assertErrorCount(report, 1) + }) + + it('should not raise error when configured as double and there are single quotes inside', function test() { + const code = contractWith('string private constant STR = "You shall \'pass\' !";') + + const report = linter.processStr(code, { rules: { quotes: ['error', 'double'] } }) + assertNoErrors(report) + }) + + it('Should raise error when configured as double and string is with single quotes despite content', function test() { + const code = contractWith('string private constant STR = \'You shall "pass" !\';') + + const report = linter.processStr(code, { rules: { quotes: ['error', 'double'] } }) + assertErrorCount(report, 1) + }) + }) })