-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathno-trailing-spaces.ts
42 lines (34 loc) · 1.18 KB
/
no-trailing-spaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 NoTrailingSpaces implements Rule {
public readonly name: string = 'no-trailing-spaces'
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> {
let lineNumber = 1
document.lines.forEach((line) => {
const joined = `${line.keyword}${line.text}`
if (joined.charCodeAt(joined.length - 1) === 32) {
document.addError(this, 'Found trailing whitespace.', {
line: lineNumber,
column: joined.length,
})
}
lineNumber += 1
})
}
public async fix(document: Document): Promise<void> {
document.lines.forEach((line: Line, index: number) => {
const joined = `${line.keyword}${line.text.trimEnd()}`
document.lines[index] = new Line(joined)
})
await document.regenerate()
}
}