Skip to content

Commit

Permalink
Fix: More reliable comment attachment (fixes #76)
Browse files Browse the repository at this point in the history
Comment attachment was sensitive to whitespace around the code block and
preceding comments. In some cases, the parser would place comments as
descendants of code blocks' preceding sibling nodes. However, a
depth-first traversal of the tree will still encounter the comments in
linear order, which is sufficient for our purposes.
  • Loading branch information
btmills committed Mar 1, 2021
1 parent 610cffd commit 6add1d9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ let blocks = [];
* Performs a depth-first traversal of the Markdown AST.
* @param {ASTNode} node A Markdown AST node.
* @param {Object} callbacks A map of node types to callbacks.
* @param {Object} [parent] The node's parent AST node.
* @returns {void}
*/
function traverse(node, callbacks, parent) {
function traverse(node, callbacks) {
if (callbacks[node.type]) {
callbacks[node.type](node, parent);
callbacks[node.type](node);
} else {
callbacks["*"]();
}

if (typeof node.children !== "undefined") {
for (let i = 0; i < node.children.length; i++) {
traverse(node.children[i], callbacks, node);
traverse(node.children[i], callbacks);
}
}
}
Expand Down Expand Up @@ -215,39 +216,44 @@ function getBlockRangeMap(text, node, comments) {
*/
function preprocess(text) {
const ast = markdown.parse(text);
let htmlComments = [];

blocks = [];
traverse(ast, {
code(node, parent) {
const comments = [];

"*"() {
htmlComments = [];
},
code(node) {
if (node.lang) {
let index = parent.children.indexOf(node) - 1;
let previousNode = parent.children[index];

while (previousNode && previousNode.type === "html") {
const comment = getComment(previousNode.value);

if (!comment) {
break;
}
const comments = [];

for (const comment of htmlComments) {
if (comment.trim() === "eslint-skip") {
htmlComments = [];
return;
}

comments.unshift(`/*${comment}*/`);
index--;
previousNode = parent.children[index];
comments.push(`/*${comment}*/`);
}

htmlComments = [];

blocks.push({
...node,
baseIndentText: getIndentText(text, node),
comments,
rangeMap: getBlockRangeMap(text, node, comments)
});
}
},
html(node) {
const comment = getComment(node.value);

if (comment) {
htmlComments.push(comment);
} else {
htmlComments = [];
}
}
});

Expand Down
31 changes: 31 additions & 0 deletions tests/lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,37 @@ describe("processor", () => {
].join("\n"));
});

// https://github.com/eslint/eslint-plugin-markdown/issues/76
it("should insert comments inside list items", () => {
const code = [
"* List item followed by a blank line",
"",
"<!-- eslint-disable no-console -->",
"```js",
"console.log(\"Blank line\");",
"```",
"",
"* List item without a blank line",
"<!-- eslint-disable no-console -->",
"```js",
"console.log(\"No blank line\");",
"```"
].join("\n");
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 2);
assert.strictEqual(blocks[0].text, [
"/* eslint-disable no-console */",
"console.log(\"Blank line\");",
""
].join("\n"));
assert.strictEqual(blocks[1].text, [
"/* eslint-disable no-console */",
"console.log(\"No blank line\");",
""
].join("\n"));
});

it("should ignore non-eslint comments", () => {
const code = [
"<!-- eslint-env browser -->",
Expand Down

0 comments on commit 6add1d9

Please sign in to comment.