Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Replace Markdown parser (fixes #125, fixes #186) #188

Merged
merged 5 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
* @property {string} [lang]
*
* @typedef {Object} RangeMap
* @property {number} js
* @property {number} md
* @property {number} indent Number of code block indent characters trimmed from
* the beginning of the line during extraction.
* @property {number} js Offset from the start of the code block's range in the
* extracted JS.
* @property {number} md Offset from the start of the code block's range in the
* original Markdown.
*
* @typedef {Object} BlockBase
* @property {string} baseIndentText
Expand All @@ -24,17 +28,14 @@

"use strict";

const unified = require("unified");
const remarkParse = require("remark-parse");
const parse = require("mdast-util-from-markdown");

const UNSATISFIABLE_RULES = [
"eol-last", // The Markdown parser strips trailing newlines in code fences
"unicode-bom" // Code blocks will begin in the middle of Markdown files
];
const SUPPORTS_AUTOFIX = true;

const markdown = unified().use(remarkParse);

/**
* @type {Map<string, Block[]>}
*/
Expand Down Expand Up @@ -152,7 +153,7 @@ function getBlockRangeMap(text, node, comments) {
/*
* The parser sets the fenced code block's start offset to wherever content
* should normally begin (typically the first column of the line, but more
* inside a list item, for example). The code block's opening fance may be
* inside a list item, for example). The code block's opening fence may be
* further indented by up to three characters. If the code block has
* additional indenting, the opening fence's first backtick may be up to
* three whitespace characters after the start offset.
Expand Down Expand Up @@ -187,6 +188,7 @@ function getBlockRangeMap(text, node, comments) {
* last range that matches, skipping this initialization entry.
*/
const rangeMap = [{
indent: baseIndent,
js: 0,
md: 0
}];
Expand Down Expand Up @@ -215,6 +217,7 @@ function getBlockRangeMap(text, node, comments) {
const trimLength = Math.min(baseIndent, leadingWhitespaceLength);

rangeMap.push({
indent: trimLength,
js: jsOffset,

// Advance `trimLength` character from the beginning of the Markdown
Expand All @@ -239,7 +242,7 @@ function getBlockRangeMap(text, node, comments) {
* @returns {Array<{ filename: string, text: string }>} Source code blocks to lint.
*/
function preprocess(text, filename) {
const ast = markdown.parse(text);
const ast = parse(text);
const blocks = [];

blocksCache.set(filename, blocks);
Expand Down Expand Up @@ -327,7 +330,7 @@ function adjustBlock(block) {

const out = {
line: lineInCode + blockStart,
column: message.column + block.position.indent[lineInCode - 1] - 1
column: message.column + block.rangeMap[lineInCode].indent
};

if (Number.isInteger(message.endLine)) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
"nyc": "^14.1.1"
},
"dependencies": {
"remark-parse": "^7.0.0",
"unified": "^6.1.2"
"mdast-util-from-markdown": "^0.8.5"
},
"peerDependencies": {
"eslint": ">=6.0.0"
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,33 @@ describe("plugin", () => {
assert.strictEqual(actual, expected);
});

it("with multiline autofix and CRLF", () => {
const input = [
"This is Markdown.",
"",
"```js",
"console.log('Hello, \\",
"world!')",
"console.log('Hello, \\",
"world!')",
"```"
].join("\r\n");
const expected = [
"This is Markdown.",
"",
"```js",
"console.log(\"Hello, \\",
"world!\")",
"console.log(\"Hello, \\",
"world!\")",
"```"
].join("\r\n");
const report = cli.executeOnText(input, "test.md");
const actual = report.results[0].output;

assert.strictEqual(actual, expected);
});

// https://spec.commonmark.org/0.28/#fenced-code-blocks
describe("when indented", () => {
it("by one space", () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks[0].filename, "0.js");
assert.strictEqual(blocks[0].text, "var answer = 6 * 7;\nconsole.log(answer);\n");
assert.strictEqual(blocks[0].text, "var answer = 6 * 7;\r\nconsole.log(answer);\n");
});

it("should unindent space-indented code fences", () => {
Expand Down Expand Up @@ -691,7 +691,7 @@ describe("processor", () => {
const result = processor.postprocess(messages);

assert.strictEqual(result[2].column, 9);
assert.strictEqual(result[3].column, 2);
assert.strictEqual(result[3].column, 4);
assert.strictEqual(result[4].column, 2);
});

Expand Down