Skip to content

Commit

Permalink
fix: Line offset and message keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Radchenko committed Feb 13, 2017
1 parent 83f00c6 commit 600770e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
54 changes: 40 additions & 14 deletions lib/processors/js.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use strict';

var extract = require('extract-comments');
var lineColumn = require('line-column');
var blockComments = [];

function preprocess(text, fileName) {
blockComments = extract.block(text);

var codeBlocks = blockComments.reduce(function (all, item) {
var blocks = extractCodeBlocks(item.value);
var blockValues = blocks.map(function (item) {
return item.value;
});

all = all.concat(blocks);
item.codeBlocks = blocks;
all = all.concat(blockValues);

return all;
}, []);
Expand All @@ -19,20 +24,37 @@ function preprocess(text, fileName) {

function postprocess(messages, fileName) {
var keep = [];
var codeBlocksProcessed = 0;

blockComments.forEach(function (item, index) {
var start = item.loc.start;
var lineOffset = start.line + 1; // 1 more for codeblock start
var columnOffset = item.loc.column;
var blockMessages = messages[index];

if (blockMessages) {
blockMessages.forEach(function (message) {
message.line += lineOffset;
message.column += columnOffset;
keep.push(message);
});
blockComments.forEach(function (item) {
var numCodeBlocks = item.codeBlocks.length;

if (!numCodeBlocks) {
return;
}

var baseLineOffset = item.loc.start.line;
var baseColumnOffset = item.loc.start.column;
var blockIndex = 0 + codeBlocksProcessed;

for (; blockIndex < numCodeBlocks; blockIndex++) {
var baseIndex = blockIndex - codeBlocksProcessed;
var block = item.codeBlocks[baseIndex];
var blockMessages = messages[blockIndex];
// minus 1 because ```[type] are not included
var lineOffset = baseLineOffset + block.loc.line - 1;
var columnOffset = baseColumnOffset + block.loc.col - 1;

if (blockMessages) {
blockMessages.forEach(function (message) {
message.line += lineOffset;
message.column += columnOffset;
keep.push(message);
});
}
}

codeBlocksProcessed++;
});

return keep;
Expand All @@ -45,8 +67,12 @@ function extractCodeBlocks(commentBlock) {

while ((match = regex.exec(commentBlock)) !== null) {
var validType = typeFilter(match[1]);

if (validType) {
code.push(match[2]);
var col = lineColumn(commentBlock);
var loc = col.fromIndex(match.index);

code.push({ value: match[2], loc: loc });
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"release": "standard-version"
},
"dependencies": {
"extract-comments": "^0.10.1"
"extract-comments": "^0.10.1",
"line-column": "^1.0.2"
},
"devDependencies": {
"eslint": "^3.9.1",
Expand Down
53 changes: 49 additions & 4 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ var cli = new CLIEngine({

cli.addPlugin('doc-code-blocks', plugin);

test('single comment - one block', function (t) {
test('single comment with block - failing block', function (t) {
var shortText = [
'/*',
'```js',
'var test = "a";',
'console.log(test);',
'```',
'*/'
'*/',
].join('\n');
var report = cli.executeOnText(shortText, 'test.js');

Expand All @@ -38,6 +38,23 @@ test('single comment - one block', function (t) {
t.end();
});

test('single comment with block - passing block', function (t) {
var shortText = [
'/*',
'```js',
'function blah(test) {',
' return test;',
'}',
'```',
'*/',
].join('\n');
var report = cli.executeOnText(shortText, 'test.js');

t.equal(report.results.length, 1);
t.equal(report.results[0].messages.length, 0);
t.end();
});

test('single comment - multiple blocks', function (t) {
var fixture = [
'/*',
Expand All @@ -64,8 +81,36 @@ test('single comment - multiple blocks', function (t) {
t.equal(report.results.length, 1);
t.equal(report.results[0].messages.length, 2);
t.equal(report.results[0].messages[0].message, 'Unexpected console statement.');
t.equal(report.results[0].messages[0].line, 4);
t.equal(report.results[0].messages[0].line, 6);
t.equal(report.results[0].messages[1].message, 'Unexpected console statement.');
t.equal(report.results[0].messages[1].line, 2);
t.equal(report.results[0].messages[1].line, 14);
t.end();
});

test('multiple comments with blocks - one failing block', function (t) {
var shortText = [
'/* hi */',
'/*',
'```js',
'var test = "a";',
'console.log(test);',
'```',
'some test',
'*/',
'',
'/*',
'```js',
'function test(val) {',
' return val;',
'}',
'```',
'*/'
].join('\n');
var report = cli.executeOnText(shortText, 'test.js');

t.equal(report.results.length, 1);
t.equal(report.results[0].messages.length, 1);
t.equal(report.results[0].messages[0].message, 'Unexpected console statement.');
t.equal(report.results[0].messages[0].line, 5);
t.end();
});

0 comments on commit 600770e

Please sign in to comment.