diff --git a/lib/processors/js.js b/lib/processors/js.js index 74505f1..49954b3 100644 --- a/lib/processors/js.js +++ b/lib/processors/js.js @@ -1,6 +1,7 @@ 'use strict'; var extract = require('extract-comments'); +var lineColumn = require('line-column'); var blockComments = []; function preprocess(text, fileName) { @@ -8,8 +9,12 @@ function preprocess(text, fileName) { 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; }, []); @@ -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; @@ -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 }); } } diff --git a/package.json b/package.json index eeffa66..d2f6bf7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/index.js b/tests/index.js index bc4cb90..a74131c 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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'); @@ -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 = [ '/*', @@ -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(); });