Skip to content

Commit

Permalink
quotes inside quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
dbale-altoros committed Aug 16, 2023
1 parent 0b396de commit 1454912
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 43 additions & 3 deletions docs/rules/miscellaneous/quotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
28 changes: 24 additions & 4 deletions lib/rules/miscellaneous/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/security/func-visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solhint",
"version": "3.6.1",
"version": "3.6.2",
"description": "Solidity Code Linter",
"main": "lib/index.js",
"keywords": [
Expand Down
30 changes: 30 additions & 0 deletions test/rules/miscellaneous/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 1454912

Please sign in to comment.