Skip to content

Commit

Permalink
Change to improve message
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 10, 2023
1 parent b49984f commit d9dda3a
Showing 2 changed files with 38 additions and 44 deletions.
68 changes: 31 additions & 37 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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'
}
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -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'
])
})

0 comments on commit d9dda3a

Please sign in to comment.