diff --git a/.gitignore b/.gitignore index 7079b99..c81392a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules coverage lib +.idea diff --git a/.npmignore b/.npmignore index 62a6cc0..5b516cf 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ # If `lib` is ignored, then npm won’t publish it. coverage __tests__ +__mocks__ diff --git a/src/actions/__tests__/read-rule.test.js b/src/actions/__tests__/read-rule.test.js index ad24442..82a313e 100644 --- a/src/actions/__tests__/read-rule.test.js +++ b/src/actions/__tests__/read-rule.test.js @@ -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. @@ -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) + }) }) diff --git a/src/actions/read-rule.js b/src/actions/read-rule.js index d773866..a4e1311 100644 --- a/src/actions/read-rule.js +++ b/src/actions/read-rule.js @@ -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)