Skip to content
This repository was archived by the owner on Jun 15, 2020. It is now read-only.

fix: issue with preserving new line characters in read-rule #37

Merged
merged 3 commits into from
Dec 19, 2018
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
lib
.idea
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# If `lib` is ignored, then npm won’t publish it.
coverage
__tests__
__mocks__
44 changes: 43 additions & 1 deletion src/actions/__tests__/read-rule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const readRule = require('../../actions/read-rule')

const description = 'Require that the file be empty'
const name = 'require-empty'
const heading = `# ${description} (${name})`
const docs =
`
# ${description} (${name})
${heading}

This rule ensures great productivity by requiring that
all files you write are completely empty.
Expand Down Expand Up @@ -74,4 +75,45 @@ describe('readRule()', () => {
runReadRule(undefined, docs.replace('\n', '\r\n')).newDocs
).toMatchSnapshot()
})

it('properly handles mixed Windows and Unix newlines', () => {
const docs = `${heading}\nSome random test:\r\n\r\n`

expect(runReadRule(undefined, docs).newDocs).toBe(docs)
})

it('properly handles multiple newlines before heading', () => {
const docs = `\n\n\n\n\r\n${heading}\n`
const outputDocs = `${heading}\n`

expect(runReadRule(undefined, docs).newDocs).toBe(outputDocs)
})

it('properly handles non heading text with new line', () => {
const docs = `\nTest\n`
const outputDocs = `${heading}\n\nTest\n`

expect(runReadRule(undefined, docs).newDocs).toBe(outputDocs)
})

it('properly handles non heading text without new line', () => {
const docs = `Test\n`
const outputDocs = `${heading}\nTest\n`

expect(runReadRule(undefined, docs).newDocs).toBe(outputDocs)
})

it('properly handles an single line heading', () => {
const docs = `# Test`
const outputDocs = `${heading}`

expect(runReadRule(undefined, docs).newDocs).toBe(outputDocs)
})

it('handles an empty file properly', () => {
const docs = ``
const outputDocs = `${heading}\n`

expect(runReadRule(undefined, docs).newDocs).toBe(outputDocs)
})
})
10 changes: 6 additions & 4 deletions src/actions/read-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ module.exports = ({ rule, docs, friendlyDocPath }, name) => {
const fixable = rule.meta.fixable
const { description, extraDescription, recommended } = rule.meta.docs

const nl = detectNewline(docs)
const heading = `# ${description} (${name})`

const newDocs = [`# ${description} (${name})`]
.concat(docs.split(nl).slice(1))
.join(nl)
const headingRe = /^\s*#[^\r\n]*(\r?\n)?/

const newDocs = headingRe.test(docs)
? docs.replace(headingRe, heading + `$1`)
: `${heading}${detectNewline(docs)}${docs}`

if (newDocs !== docs) {
const patch = diff(friendlyDocPath, 'generated', docs, newDocs)
Expand Down