Skip to content

Commit

Permalink
Replace vfile patching of data with vfile-namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 31, 2015
1 parent 7fad9a5 commit c1136a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function transformer(ast, file) {
var gaps = [];
var offset = 0;
var isGap = false;
var scope = file.namespace('mdast-lint').ranges;

if (!file || !file.messages || !file.messages.length) {
return;
Expand Down Expand Up @@ -75,7 +76,7 @@ function transformer(ast, file) {
}

file.messages = file.messages.filter(function (message) {
var ranges = file.lintRanges[message.ruleId];
var ranges = scope[message.ruleId];
var index = ranges && ranges.length;
var gapIndex = gaps.length;
var length = -1;
Expand Down
23 changes: 14 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ function attachFactory(id, rule, options) {
* @param {Function} next - Signal end.
*/
function plugin(ast, file, next) {
var scope = file.namespace('mdast-lint');

/*
* Track new messages per file.
*/

if (file.lintIndex === undefined || file.lintIndex === null) {
file.lintIndex = file.messages.length;
if (scope.index === undefined || scope.index === null) {
scope.index = file.messages.length;
}

/**
Expand All @@ -74,10 +76,10 @@ function attachFactory(id, rule, options) {
function done(err) {
var messages = file.messages;

while (file.lintIndex < messages.length) {
messages[file.lintIndex].ruleId = id;
while (scope.index < messages.length) {
messages[scope.index].ruleId = id;

file.lintIndex++;
scope.index++;
}

next(err);
Expand Down Expand Up @@ -207,7 +209,8 @@ function lint(mdast, options) {
* @param {File} [file] - File (optional)
*/
function getState(ruleId, file) {
var ranges = file && file.lintRanges && file.lintRanges[ruleId];
var scope = file && file.namespace('mdast-lint');
var ranges = scope && scope.ranges && scope.ranges[ruleId];

if (ranges) {
return ranges[ranges.length - 1].state;
Expand All @@ -228,7 +231,8 @@ function lint(mdast, options) {
* @param {File} file - Virtual file.
*/
function store(file) {
var ranges = file.lintRanges;
var scope = file.namespace('mdast-lint');
var ranges = scope.ranges;
var ruleId;

if (!ranges) {
Expand All @@ -244,7 +248,7 @@ function lint(mdast, options) {
}];
}

file.lintRanges = ranges;
scope.ranges = ranges;
}
}

Expand All @@ -270,6 +274,7 @@ function lint(mdast, options) {
*/
function onparse(marker, parser) {
var file = parser.file;
var scope = file.namespace('mdast-lint');
var attributes = marker.attributes.split(' ');
var type = attributes[0];
var ruleId = attributes[1];
Expand All @@ -291,7 +296,7 @@ function lint(mdast, options) {
return;
}

markers = file.lintRanges[ruleId];
markers = scope.ranges[ruleId];

previousState = getState(ruleId, file);
currentState = type === 'enable';
Expand Down
26 changes: 16 additions & 10 deletions mdast-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function transformer(ast, file) {
var gaps = [];
var offset = 0;
var isGap = false;
var scope = file.namespace('mdast-lint').ranges;

if (!file || !file.messages || !file.messages.length) {
return;
Expand Down Expand Up @@ -89,7 +90,7 @@ function transformer(ast, file) {
}

file.messages = file.messages.filter(function (message) {
var ranges = file.lintRanges[message.ruleId];
var ranges = scope[message.ruleId];
var index = ranges && ranges.length;
var gapIndex = gaps.length;
var length = -1;
Expand Down Expand Up @@ -210,12 +211,14 @@ function attachFactory(id, rule, options) {
* @param {Function} next - Signal end.
*/
function plugin(ast, file, next) {
var scope = file.namespace('mdast-lint');

/*
* Track new messages per file.
*/

if (file.lintIndex === undefined || file.lintIndex === null) {
file.lintIndex = file.messages.length;
if (scope.index === undefined || scope.index === null) {
scope.index = file.messages.length;
}

/**
Expand All @@ -226,10 +229,10 @@ function attachFactory(id, rule, options) {
function done(err) {
var messages = file.messages;

while (file.lintIndex < messages.length) {
messages[file.lintIndex].ruleId = id;
while (scope.index < messages.length) {
messages[scope.index].ruleId = id;

file.lintIndex++;
scope.index++;
}

next(err);
Expand Down Expand Up @@ -359,7 +362,8 @@ function lint(mdast, options) {
* @param {File} [file] - File (optional)
*/
function getState(ruleId, file) {
var ranges = file && file.lintRanges && file.lintRanges[ruleId];
var scope = file && file.namespace('mdast-lint');
var ranges = scope && scope.ranges && scope.ranges[ruleId];

if (ranges) {
return ranges[ranges.length - 1].state;
Expand All @@ -380,7 +384,8 @@ function lint(mdast, options) {
* @param {File} file - Virtual file.
*/
function store(file) {
var ranges = file.lintRanges;
var scope = file.namespace('mdast-lint');
var ranges = scope.ranges;
var ruleId;

if (!ranges) {
Expand All @@ -396,7 +401,7 @@ function lint(mdast, options) {
}];
}

file.lintRanges = ranges;
scope.ranges = ranges;
}
}

Expand All @@ -422,6 +427,7 @@ function lint(mdast, options) {
*/
function onparse(marker, parser) {
var file = parser.file;
var scope = file.namespace('mdast-lint');
var attributes = marker.attributes.split(' ');
var type = attributes[0];
var ruleId = attributes[1];
Expand All @@ -443,7 +449,7 @@ function lint(mdast, options) {
return;
}

markers = file.lintRanges[ruleId];
markers = scope.ranges[ruleId];

previousState = getState(ruleId, file);
currentState = type === 'enable';
Expand Down
2 changes: 1 addition & 1 deletion mdast-lint.min.js

Large diffs are not rendered by default.

0 comments on commit c1136a1

Please sign in to comment.