Skip to content

Commit

Permalink
Merge pull request #132 from protofire/handle-crlf
Browse files Browse the repository at this point in the history
Handle CRLF newlines
  • Loading branch information
Mariano Aguero authored Aug 8, 2019
2 parents 2a8ec18 + 93cd1aa commit 98c3ea4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/rules/best-practises/max-line-length.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const BaseChecker = require('./../base-checker')

const lineBreakPattern = /\r\n|[\r\n\u2028\u2029]/u

const ruleId = 'max-line-length'
const meta = {
type: 'best-practises',
Expand Down Expand Up @@ -31,7 +33,7 @@ class MaxLineLengthChecker extends BaseChecker {
}

enterSourceUnit(ctx) {
const lines = ctx.parser._input.tokenSource._input.strdata.split('\n')
const lines = ctx.parser._input.tokenSource._input.strdata.split(lineBreakPattern)

lines.map(line => line.length).forEach(this.validateLineLength.bind(this))
}
Expand Down
32 changes: 32 additions & 0 deletions test/rules/best-practises/max-line-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,36 @@ describe('Linter - max-line-length', () => {

assertNoErrors(report)
})

it('should not raise error when line is exactly the max length', () => {
const code = ' '.repeat(120)

const report = linter.processStr(code, {
rules: { 'max-line-length': 'error' }
})

assertNoErrors(report)
})

it('should not count newlines', () => {
const line = ' '.repeat(120)
const code = `${line}\n${line}\n`

const report = linter.processStr(code, {
rules: { 'max-line-length': 'error' }
})

assertNoErrors(report)
})

it('should not count windows newlines', () => {
const line = ' '.repeat(120)
const code = `${line}\n\r${line}\n\r`

const report = linter.processStr(code, {
rules: { 'max-line-length': 'error' }
})

assertNoErrors(report)
})
})

0 comments on commit 98c3ea4

Please sign in to comment.