diff --git a/lib/index.js b/lib/index.js index 66cf050..a162b58 100644 --- a/lib/index.js +++ b/lib/index.js @@ -27,7 +27,7 @@ const emptyOptions = {} */ export default function retextSentenceSpacing(options) { const settings = options || emptyOptions - const preferred = + const expected = settings.preferred === 'double-space' || settings.preferred === 2 ? 2 : settings.preferred === 'newline' || settings.preferred === 0 @@ -63,53 +63,47 @@ export default function retextSentenceSpacing(options) { continue } - const actual = toString(child) + const value = toString(child) // We only check for whitespace that is *just* spaces: it’s OK to add // line feeds if `space` is expected. - if (!/^ +$/.test(actual)) { + if (!/^ +$/.test(value)) { continue } - const size = actual.length - /** @type {string} */ - let reason + const actual = value.length - // Size is never preferred if we want a line feed. - if (preferred === 0) { - reason = - 'Expected a newline between sentences, not `' + - size + - '` space' + - (size === 1 ? '' : 's') - } else if (size === preferred) { + // `actual` is never `expected` if we want a line feed. + if (actual === expected) { continue - } else { - reason = - 'Expected `' + - preferred + - '` space' + - (preferred === 1 ? '' : 's') + - ' between sentences, not `' + - size + - '`' } - const message = file.message(reason, { - place: child.position, - ruleId: - preferred === 0 - ? 'newline' - : preferred === 1 - ? 'space' - : 'double-space', - source: 'retext-sentence-spacing' - }) + const message = file.message( + expected === 0 + ? 'Unexpected spaces between sentence, expected a line ending' + : 'Unexpected ' + + actual + + ' space' + + (actual === 1 ? '' : 's') + + ' between sentence, expected ' + + expected + + ' space' + + (expected === 1 ? '' : 's'), + { + ancestors: [node, child], + place: child.position, + ruleId: + expected === 0 + ? 'newline' + : expected === 1 + ? 'space' + : 'double-space', + source: 'retext-sentence-spacing' + } + ) - message.actual = actual - message.expected = [ - preferred === 0 ? '\n' : preferred === 1 ? ' ' : ' ' - ] + message.actual = value + message.expected = [expected === 0 ? '\n' : expected === 1 ? ' ' : ' '] message.url = 'https://github.com/retextjs/retext-sentence-spacing#readme' } diff --git a/test.js b/test.js index 24f91ea..5a13a2f 100644 --- a/test.js +++ b/test.js @@ -29,14 +29,14 @@ test('retextSentenceSpacing', async function (t) { ancestors: [], column: 14, fatal: false, - message: 'Expected `1` space between sentences, not `2`', + message: 'Unexpected 2 spaces between sentence, expected 1 space', line: 3, name: '3:14-3:16', place: { start: {line: 3, column: 14, offset: 43}, end: {line: 3, column: 16, offset: 45} }, - reason: 'Expected `1` space between sentences, not `2`', + reason: 'Unexpected 2 spaces between sentence, expected 1 space', ruleId: 'space', source: 'retext-sentence-spacing', actual: ' ', @@ -60,7 +60,7 @@ test('retextSentenceSpacing', async function (t) { .process(mixed) assert.deepEqual(file.messages.map(String), [ - '3:14-3:16: Expected `1` space between sentences, not `2`' + '3:14-3:16: Unexpected 2 spaces between sentence, expected 1 space' ]) } ) @@ -77,7 +77,7 @@ test('retextSentenceSpacing', async function (t) { .process(mixed) assert.deepEqual(file.messages.map(String), [ - '1:14-1:15: Expected `2` spaces between sentences, not `1`' + '1:14-1:15: Unexpected 1 space between sentence, expected 2 spaces' ]) } ) @@ -94,8 +94,8 @@ test('retextSentenceSpacing', async function (t) { .process(mixed) assert.deepEqual(file.messages.map(String), [ - '1:14-1:15: Expected a newline between sentences, not `1` space', - '3:14-3:16: Expected a newline between sentences, not `2` spaces' + '1:14-1:15: Unexpected spaces between sentence, expected a line ending', + '3:14-3:16: Unexpected spaces between sentence, expected a line ending' ]) } ) @@ -108,7 +108,7 @@ test('retextSentenceSpacing', async function (t) { .process('One sentence. Three sentences.') assert.deepEqual(file.messages.map(String), [ - '1:14-1:17: Expected `1` space between sentences, not `3`' + '1:14-1:17: Unexpected 3 spaces between sentence, expected 1 space' ]) })