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 1 commit
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
28 changes: 14 additions & 14 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
ignore: [
'__mocks__',
'__tests__',
'__test_support__',
'/coverage',
"ignore": [
"__mocks__",
"__tests__",
"__test_support__",
"/coverage"
],
presets: [
['env', {
targets: {
node: 6,
},
}],
"presets": [
["env", {
"targets": {
"node": 6
}
}]
],
env: {
test: {
ignore: null
"env": {
"test": {
"ignore": null
}
}
}
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__
41 changes: 41 additions & 0 deletions src/actions/__tests__/read-rule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,45 @@ describe('readRule()', () => {
runReadRule(undefined, docs.replace('\n', '\r\n')).newDocs
).toMatchSnapshot()
})

it('mixed windows and unix newlines', () => {
const docs = `# ${description} (${name})\nSome random test:\r\n\r\n`

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

it('whitespaces before heading', () => {
const docs = `\n\n\n\n\r\n# ${description} (${name})\n`
const outputDocs = `# ${description} (${name})\n`

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

it('non string text before heading', () => {
const docs = `\nTest\n`
const outputDocs = `# ${description} (${name})\n\nTest\n`

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

it('non string text before heading without new line', () => {
const docs = `Test\n`
const outputDocs = `# ${description} (${name})\nTest\n`

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

it('single line heading', () => {
const docs = `# Test`
const outputDocs = `# ${description} (${name})`

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

it('empty file', () => {
const docs = ``
const outputDocs = `# ${description} (${name})\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 regTest = /^\s*#[^\r\n]*(\r?\n)?/

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

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