Skip to content

Commit

Permalink
add no-full-stop rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Markham committed Sep 25, 2024
1 parent 1c4d0bc commit be5f5c0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/rules/no-full-stop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { switchOrSeveritySchema } from '../schemas'
import Schema from '../schema'
import Rule from '../rule'
import { RawSchema, AcceptedSchema } from '../types'
import Document from '../document'
import Line from '../line'

export default class NoFullStop implements Rule {
public readonly name: string = 'no-full-stop'

public readonly acceptedSchema: AcceptedSchema = switchOrSeveritySchema

public readonly schema: Schema

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

public async run(document: Document): Promise<void> {
document.lines.forEach((line: Line, index: number): void => {
const trimmed = line.text.trimEnd()
if (trimmed[trimmed.length - 1] === '.') {
document.addError(this.name, `Line ends with a full stop.`, {
line: index + 1,
column: (line.keyword + trimmed).length,
})
}
})
}

public async fix(document: Document): Promise<void> {
document.lines.forEach((line: Line, index: number): void => {
const trimmed = line.text.trimEnd()
if (trimmed[trimmed.length - 1] === '.') {
document.lines[index].text = trimmed.substring(0, trimmed.length - 1)
}
})

await document.regenerate()
}
}
46 changes: 46 additions & 0 deletions tests/acceptance/features/no-full-stop.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Feature: No Full Stop

Scenario: Contains Full Stop
Given the following feature file
"""
Feature: Some feature
When I do something
Then something has happened.
"""
When Gherklin is ran with the following configuration
| rules |
| {"no-full-stop": "error"} |
Then there is 1 files with errors
And the errors are
| location | severity | rule | message |
| {"line": 3, "column": 28} | error | no-full-stop | Line ends with a full stop. |

Scenario: Contains Full Stop with Trailing Spaces

If editing this file, be aware that line 26 is supposed to have 3 trailing spaces

Given the following feature file
"""
Feature: Some feature
When I do something
Then something has happened.
"""
When Gherklin is ran with the following configuration
| rules |
| {"no-full-stop": "error"} |
Then there is 1 files with errors
And the errors are
| location | severity | rule | message |
| {"line": 3, "column": 28} | error | no-full-stop | Line ends with a full stop. |

Scenario: Auto fix
Given the following feature file
"""
Feature: Some feature
When I do something
Then something has happened.
"""
When Gherklin is ran with the following configuration
| rules | fix |
| {"no-full-stop": "error"} | true |
Then there are 0 files with errors

0 comments on commit be5f5c0

Please sign in to comment.