From dcf6e3c82cf7e311803383bb26a3c34e9f0f0a37 Mon Sep 17 00:00:00 2001 From: Titus Date: Sat, 15 Jun 2019 10:13:38 +0200 Subject: [PATCH] Add support for controlling the start of a marker Closes GH-4. Closes GH-6. --- index.js | 34 ++++++++++----------- test.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 6840b29..3a9d4bc 100644 --- a/index.js +++ b/index.js @@ -82,20 +82,8 @@ 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) @@ -103,6 +91,18 @@ function messageControl(options) { 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) + } + } + } } } @@ -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) { @@ -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. @@ -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 } diff --git a/test.js b/test.js index bddd783..17e877f 100644 --- a/test.js +++ b/test.js @@ -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('\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('\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('\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('\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() })