Skip to content

Commit

Permalink
Add support for controlling the start of a marker
Browse files Browse the repository at this point in the history
Closes GH-4.
Closes GH-6.
  • Loading branch information
wooorm committed Jun 15, 2019
1 parent 785303b commit dcf6e3c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 18 deletions.
34 changes: 16 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,27 @@ function messageControl(options) {
length = ruleIds.length
index = -1

while (++index < length) {
ruleId = ruleIds[index]

if (isKnown(ruleId, verb, mark.node)) {
toggle(pos, verb === 'enable', ruleId)

if (verb === 'ignore') {
toggle(tail, true, ruleId)
}
}
}

// Apply to all rules.
if (!length) {
if (length === 0) {
if (verb === 'ignore') {
toggle(pos, false)
toggle(tail, true)
} else {
toggle(pos, verb === 'enable')
reset = verb !== 'enable'
}
} else {
while (++index < length) {
ruleId = ruleIds[index]

if (isKnown(ruleId, verb, mark.node)) {
toggle(pos, verb === 'enable', ruleId)

if (verb === 'ignore') {
toggle(tail, true, ruleId)
}
}
}
}
}

Expand Down Expand Up @@ -177,7 +177,6 @@ function messageControl(options) {
// Handle a rule.
function toggle(pos, state, ruleId) {
var markers = ruleId ? scope[ruleId] : globals
var currentState
var previousState

if (!markers) {
Expand All @@ -186,10 +185,9 @@ function messageControl(options) {
}

previousState = getState(ruleId)
currentState = state

if (currentState !== previousState) {
markers.push({state: currentState, position: pos})
if (state !== previousState) {
markers.push({state: state, position: pos})
}

// Toggle all known rules.
Expand Down Expand Up @@ -218,7 +216,7 @@ function messageControl(options) {
if (
range.position.line < message.line ||
(range.position.line === message.line &&
range.position.column < message.column)
range.position.column <= message.column)
) {
return range.state === true
}
Expand Down
90 changes: 90 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,5 +647,95 @@ test('control()', function(t) {
)
})

remark()
.use(function() {
var transformer = control({
name: 'foo',
marker: mdastMarker,
test: 'html'
})

return function(tree, file) {
file.message('Error', {line: 1, column: 1}, 'foo:bar')

transformer(tree, file)
}
})
.process('<!--foo disable bar-->\n', function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null],
'should disable messages at the start of a marker'
)
})

remark()
.use(function() {
var transformer = control({
name: 'foo',
marker: mdastMarker,
test: 'html',
reset: true
})

return function(tree, file) {
file.message('Error', {line: 1, column: 1}, 'foo:bar')

transformer(tree, file)
}
})
.process('<!--foo enable-->\n', function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null, '1:1: Error'],
'should enable messages at the start of a marker'
)
})

remark()
.use(function() {
var transformer = control({
name: 'foo',
marker: mdastMarker,
test: 'html'
})

return function(tree, file) {
file.message('Error', 'foo:bar')

transformer(tree, file)
}
})
.process('<!--foo disable bar-->\n', function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null],
'should disable messages without positional info (at the start of a document)'
)
})

remark()
.use(function() {
var transformer = control({
name: 'foo',
marker: mdastMarker,
test: 'html',
reset: true
})

return function(tree, file) {
file.message('Error', 'foo:bar')

transformer(tree, file)
}
})
.process('<!--foo enable-->\n', function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null, '1:1: Error'],
'should enable messages without positional info (at the start of a document)'
)
})

t.end()
})

0 comments on commit dcf6e3c

Please sign in to comment.