Skip to content

Commit

Permalink
Add more info to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 7, 2019
1 parent deae874 commit b1cbd84
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
55 changes: 32 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module.exports = sentenceSpacing
var sentence = convert('SentenceNode')
var whiteSpace = convert('WhiteSpaceNode')

var id = 'retext-sentence-spacing:retext-sentence-spacing'
var source = 'retext-sentence-spacing'
var ids = ['newline', 'space', 'double-space']
var expected = ['\n', ' ', ' ']

function sentenceSpacing(options) {
var preferred = (options || {}).preferred
Expand Down Expand Up @@ -44,7 +46,9 @@ function sentenceSpacing(options) {
var children = node.children
var length = children.length
var index = 0
var value
var reason
var message
var actual
var child
var size

Expand All @@ -59,39 +63,44 @@ function sentenceSpacing(options) {
continue
}

value = toString(child)
actual = toString(child)

// We only check for white-space that is *just* spaces: it’s OK to add
// line feeds if `space` is expected.
if (!/^ +$/.test(value)) {
if (!/^ +$/.test(actual)) {
continue
}

size = value.length
size = actual.length

// Size is never preferred if we want a line feed.
if (preferred === 0) {
file.message(
reason =
'Expected a newline between sentences, not `' +
size +
'` ' +
plural('space', size),
child,
id
)
} else if (size !== preferred) {
file.message(
size +
'` ' +
plural('space', size)
} else if (size === preferred) {
continue
} else {
reason =
'Expected `' +
preferred +
'` ' +
plural('space', preferred) +
' between sentences, not `' +
size +
'`',
child,
id
)
preferred +
'` ' +
plural('space', preferred) +
' between sentences, not `' +
size +
'`'
}

message = file.message(
reason,
child,
[source, ids[preferred]].join(':')
)

message.actual = actual
message.expected = [expected[preferred]]
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ test('sentenceSpacing(value[, size])', function(t) {
var one = [null, 'space', 1]
var two = [2]

retext()
.use(spacing, {preferred: 1})
.process(mixed, function(err, file) {
t.deepEqual(
[err].concat(file.messages),
[
null,
{
message: 'Expected `1` space between sentences, not `2`',
name: '3:14-3:16',
reason: 'Expected `1` space between sentences, not `2`',
line: 3,
column: 14,
location: {
start: {line: 3, column: 14, offset: 43},
end: {line: 3, column: 16, offset: 45}
},
source: 'retext-sentence-spacing',
ruleId: 'space',
fatal: false,
actual: ' ',
expected: [' ']
}
],
'should emit messages'
)
})

one.forEach(function(pref) {
retext()
.use(spacing, {preferred: pref})
Expand Down

0 comments on commit b1cbd84

Please sign in to comment.