Skip to content

Commit

Permalink
fix(prettier-plugin-jsdoc): don\'t allow more than one empty line
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 24, 2020
1 parent 778d366 commit d032687
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/fns/splitText.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const R = require('ramda');
const { ensureArray, replaceLastItem } = require('./utils');
const { ensureArray, replaceLastItem, limitAdjacentRepetitions } = require('./utils');

/**
* This is used when splitting lines that contain linebreaks; it's used as a filter so a text won't
* include multiple empty lines.
*
* @type {number}
*/
const ADJACENT_LINEBREAKS_LIMIT = 2;

/**
* This is a utility used inside the reducers in order to take "words" that include line breaks
* and split them into multiple "words".
Expand All @@ -13,6 +22,7 @@ const { ensureArray, replaceLastItem } = require('./utils');
* @type {SplitLineBreaksFn}
*/
const splitLineBreaks = R.compose(
limitAdjacentRepetitions(R.equals('\n'), ADJACENT_LINEBREAKS_LIMIT),
R.dropLast(1),
R.reduce((sacc, item) => [...sacc, item, '\n'], []),
R.map(R.when(R.isEmpty, R.always('\n'))),
Expand Down Expand Up @@ -59,7 +69,7 @@ const reduceSentences = R.curry((length, list, word, index) => {
newList = R.append('', list);
} else if (index) {
const currentLine = R.last(list).trim();
const newLine = `${currentLine} ${word}`;
const newLine = `${currentLine} ${word}`.trim();
newList = R.ifElse(
R.always(newLine.length > length),
R.append(word),
Expand Down
22 changes: 22 additions & 0 deletions test/unit/fns/splitText.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ describe('splitText', () => {
expect(result).toEqual(output);
});

it('should only allow one empty line between paragraphs', () => {
// Given
const input = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus\nlobortis',
'erat molestie posuere dictum. Integer libero justo, viverra\nquis efficitur',
'in, condimentum\n\n\ncongue lorem.',
].join(' ');
const output = [
'Lorem ipsum dolor sit amet, consectetur adipiscing',
'elit. Phasellus lobortis erat molestie posuere',
'dictum. Integer libero justo, viverra quis',
'efficitur in, condimentum',
'',
'congue lorem.',
];
let result = null;
// When
result = splitText(input, 50);
// Then
expect(result).toEqual(output);
});

it('shouldn\'t add a leading space when there\'s only one word (bug)', () => {
// Given
const input = 'Description';
Expand Down

0 comments on commit d032687

Please sign in to comment.