Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-inconsistent-quotes rule #66

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ If a rule does not specify a severity, it will default to `warn`.
* [no-typographer-quotes](#no-typographer-quotes)
* [background-setup-only](#background-setup-only)
* [given-after-background](#given-after-background)
* [no-inconsistent-quotes](#no-inconsistent-quotes)

### Allowed Tags

Expand Down Expand Up @@ -554,3 +555,40 @@ export default {
}
}
```

### No Inconsistent Quotes

Prefer consistency with quotes used.

**Examples**

Enable the rule

```typescript
export default {
rules: {
'no-inconsistent-quotes': 'on',
}
}
```


Enable the rule and set severity
```typescript
export default {
rules: {
'no-inconsistent-quotes': 'error',
}
}
```

Enable the rule and set argument.

*The argument is used as a replacement when the `fix` option is specified.*
```typescript
export default {
rules: {
'no-inconsistent-quotes': ['error', "\""],
}
}
```
62 changes: 62 additions & 0 deletions src/rules/no-inconsistent-quotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { switchOrSeverityorSeverityAndStringSchema } from '../schemas'
import Schema from '../schema'
import Rule from '../rule'
import { RawSchema, AcceptedSchema, Location } from '../types'
import Document from '../document'
import Line from '../line'

export default class NoInconsistentQuotes implements Rule {
public readonly name: string = 'no-inconsistent-quotes'

public readonly acceptedSchema: AcceptedSchema = switchOrSeverityorSeverityAndStringSchema

public readonly schema: Schema

public constructor(rawSchema: RawSchema) {
this.schema = new Schema(rawSchema)
}

public async run(document: Document): Promise<void> {
const quotesUsed: Map<string, Array<Location>> = new Map()
quotesUsed.set(`"`, [])
quotesUsed.set(`'`, [])

document.lines.forEach((line: Line, index: number): void => {
const singleIndex = line.text.indexOf(`'`)
if (singleIndex > -1) {
quotesUsed.set(`'`, [
...quotesUsed.get(`'`),
{ line: index + 1, column: line.indentation + line.keyword.length + singleIndex + 1 },
])
}
const doubleIndex = line.text.indexOf(`"`)
if (doubleIndex > -1) {
quotesUsed.set(`"`, [
...quotesUsed.get(`"`),
{ line: index + 1, column: line.indentation + line.keyword.length + doubleIndex + 1 },
])
}
})

if (quotesUsed.get(`'`).length > 0 && quotesUsed.get(`"`).length > 0) {
quotesUsed.forEach((locations: Array<Location>) => {
locations.forEach((location: Location): void => {
document.addError(this.name, 'Found a mix of single and double quotes.', location)
})
})
}
}

public async fix(document: Document): Promise<void> {
let replacer = `'`
if (this.schema.args) {
replacer = this.schema.args as string
}

document.lines.forEach((line: Line, index: number): void => {
document.lines[index].text = line.text.replaceAll(/['"]/g, replacer)
})

await document.regenerate()
}
}
76 changes: 76 additions & 0 deletions tests/acceptance/features/no-inconsistent-quotes.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Feature: No Inconsistent Quotes

Scenario: ' mixed with "
Given the following feature file
"""
Feature: Invalid
Scenario: One
When I do "something"
Then 'something' has happened
"""
When Gherklin is ran with the following configuration
| rules |
| {"no-inconsistent-quotes": "on"} |
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 3, "column": 15} | warn | no-inconsistent-quotes | Found a mix of single and double quotes. |
| {"line": 4, "column": 10} | warn | no-inconsistent-quotes | Found a mix of single and double quotes. |

Scenario: Only ' used
Given the following feature file
"""
Feature: Invalid
Scenario: One
When I do 'something'
Then 'something' has happened
"""
When Gherklin is ran with the following configuration
| rules |
| {"no-inconsistent-quotes": "on"} |
Then there are 0 files with errors

Scenario: Only " used
Given the following feature file
"""
Feature: Invalid
Scenario: One
When I do "something"
Then "something" has happened
"""
When Gherklin is ran with the following configuration
| rules |
| {"no-inconsistent-quotes": "on"} |
Then there are 0 files with errors

Scenario: Auto Fix
Given the following feature file
"""
Feature: Invalid
Scenario: One
When I do "something"
Then 'something' has happened
"""
When Gherklin is ran with the following configuration
| rules | fix |
| {"no-inconsistent-quotes": "on"} | true |
Then there are 0 files with errors
And the file has the content
| line | content |
| 3 | When I do 'something' |

Scenario: Auto Fix with Argument
Given the following feature file
"""
Feature: Invalid
Scenario: One
When I do "something"
Then 'something' has happened
"""
When Gherklin is ran with the following configuration
| rules | fix |
| {"no-inconsistent-quotes": ["warn", "\""]} | true |
Then there are 0 files with errors
And the file has the content
| line | content |
| 3 | When I do "something" |