Reimplement MD035/hr-style using micromark tokens (fixes #736).

This commit is contained in:
David Anson 2023-03-06 21:38:40 -08:00
parent f1e33672ba
commit 8057f3d37e
7 changed files with 508 additions and 25 deletions

View file

@ -5129,28 +5129,37 @@ module.exports = {
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
addErrorDetailIf = _require.addErrorDetailIf,
filterTokens = _require.filterTokens;
addErrorDetailIf = _require.addErrorDetailIf;
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
filterByTypes = _require2.filterByTypes;
module.exports = {
"names": ["MD035", "hr-style"],
"description": "Horizontal rule style",
"tags": ["hr"],
"function": function MD035(params, onError) {
var style = String(params.config.style || "consistent").trim();
filterTokens(params, "hr", function (token) {
var line = token.line,
lineNumber = token.lineNumber;
var markup = token.markup;
var match = line.match(/[_*\-\s]+$/);
if (match) {
markup = match[0].trim();
var thematicBreaks = filterByTypes(params.parsers.micromark.tokens, ["thematicBreak"]);
var _iterator = _createForOfIteratorHelper(thematicBreaks),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var token = _step.value;
var startLine = token.startLine,
text = token.text;
if (style === "consistent") {
style = text;
}
addErrorDetailIf(onError, startLine, style, text);
}
if (style === "consistent") {
style = markup;
}
addErrorDetailIf(onError, lineNumber, style, markup);
});
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
};

View file

@ -2,7 +2,8 @@
"use strict";
const { addErrorDetailIf, filterTokens } = require("../helpers");
const { addErrorDetailIf } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");
module.exports = {
"names": [ "MD035", "hr-style" ],
@ -10,17 +11,14 @@ module.exports = {
"tags": [ "hr" ],
"function": function MD035(params, onError) {
let style = String(params.config.style || "consistent").trim();
filterTokens(params, "hr", (token) => {
const { line, lineNumber } = token;
let { markup } = token;
const match = line.match(/[_*\-\s]+$/);
if (match) {
markup = match[0].trim();
}
const thematicBreaks =
filterByTypes(params.parsers.micromark.tokens, [ "thematicBreak" ]);
for (const token of thematicBreaks) {
const { startLine, text } = token;
if (style === "consistent") {
style = markup;
style = text;
}
addErrorDetailIf(onError, lineNumber, style, markup);
});
addErrorDetailIf(onError, startLine, style, text);
}
}
};

31
test/hr-in-list-dash.md Normal file
View file

@ -0,0 +1,31 @@
# HR in List, Dash
---
***
___
{MD035:5} {MD035:7}
<!-- markdownlint-disable blanks-around-lists ul-style -->
- list
- ---
- list
- ***
- list
- ___
- list
{MD035:14} {MD035:16} {MD035:18}
* list
* ---
* list
* ***
* list
* ___
* list
{MD035:26} {MD035:28}

31
test/hr-in-list-star.md Normal file
View file

@ -0,0 +1,31 @@
# HR in List, Star
***
___
---
{MD035:5} {MD035:7}
<!-- markdownlint-disable blanks-around-lists ul-style -->
- list
- ---
- list
- ***
- list
- ___
- list
{MD035:14} {MD035:18}
* list
* ---
* list
* ***
* list
* ___
* list
{MD035:24} {MD035:26} {MD035:28}

31
test/hr-in-list-under.md Normal file
View file

@ -0,0 +1,31 @@
# HR in List, Under
___
---
***
{MD035:5} {MD035:7}
<!-- markdownlint-disable blanks-around-lists ul-style -->
- list
- ---
- list
- ***
- list
- ___
- list
{MD035:14} {MD035:16}
* list
* ---
* list
* ***
* list
* ___
* list
{MD035:24} {MD035:26}

View file

@ -15312,6 +15312,389 @@ Generated by [AVA](https://avajs.dev).
`,
}
## hr-in-list-dash.md
> Snapshot 1
{
errors: [
{
errorContext: null,
errorDetail: 'Expected: ---; Actual: ***',
errorRange: null,
fixInfo: null,
lineNumber: 5,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ---; Actual: ___',
errorRange: null,
fixInfo: null,
lineNumber: 7,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ---; Actual: - ---',
errorRange: null,
fixInfo: null,
lineNumber: 14,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ---; Actual: ***',
errorRange: null,
fixInfo: null,
lineNumber: 16,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ---; Actual: ___',
errorRange: null,
fixInfo: null,
lineNumber: 18,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ---; Actual: * ***',
errorRange: null,
fixInfo: null,
lineNumber: 26,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ---; Actual: ___',
errorRange: null,
fixInfo: null,
lineNumber: 28,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
],
fixed: `# HR in List, Dash␊
---␊
***␊
___␊
{MD035:5} {MD035:7}␊
<!-- markdownlint-disable blanks-around-lists ul-style -->
- list␊
- ---␊
- list␊
- ***␊
- list␊
- ___␊
- list␊
{MD035:14} {MD035:16} {MD035:18}␊
* list␊
* ---␊
* list␊
* ***␊
* list␊
* ___␊
* list␊
{MD035:26} {MD035:28}␊
`,
}
## hr-in-list-star.md
> Snapshot 1
{
errors: [
{
errorContext: null,
errorDetail: 'Expected: ***; Actual: ___',
errorRange: null,
fixInfo: null,
lineNumber: 5,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ***; Actual: ---',
errorRange: null,
fixInfo: null,
lineNumber: 7,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ***; Actual: - ---',
errorRange: null,
fixInfo: null,
lineNumber: 14,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ***; Actual: ___',
errorRange: null,
fixInfo: null,
lineNumber: 18,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ***; Actual: ---',
errorRange: null,
fixInfo: null,
lineNumber: 24,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ***; Actual: * ***',
errorRange: null,
fixInfo: null,
lineNumber: 26,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ***; Actual: ___',
errorRange: null,
fixInfo: null,
lineNumber: 28,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
],
fixed: `# HR in List, Star␊
***␊
___␊
---␊
{MD035:5} {MD035:7}␊
<!-- markdownlint-disable blanks-around-lists ul-style -->
- list␊
- ---␊
- list␊
- ***␊
- list␊
- ___␊
- list␊
{MD035:14} {MD035:18}␊
* list␊
* ---␊
* list␊
* ***␊
* list␊
* ___␊
* list␊
{MD035:24} {MD035:26} {MD035:28}␊
`,
}
## hr-in-list-under.md
> Snapshot 1
{
errors: [
{
errorContext: null,
errorDetail: 'Expected: ___; Actual: ---',
errorRange: null,
fixInfo: null,
lineNumber: 5,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ___; Actual: ***',
errorRange: null,
fixInfo: null,
lineNumber: 7,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ___; Actual: - ---',
errorRange: null,
fixInfo: null,
lineNumber: 14,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ___; Actual: ***',
errorRange: null,
fixInfo: null,
lineNumber: 16,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ___; Actual: ---',
errorRange: null,
fixInfo: null,
lineNumber: 24,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
{
errorContext: null,
errorDetail: 'Expected: ___; Actual: * ***',
errorRange: null,
fixInfo: null,
lineNumber: 26,
ruleDescription: 'Horizontal rule style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md035.md',
ruleNames: [
'MD035',
'hr-style',
],
},
],
fixed: `# HR in List, Under␊
___␊
---␊
***␊
{MD035:5} {MD035:7}␊
<!-- markdownlint-disable blanks-around-lists ul-style -->
- list␊
- ---␊
- list␊
- ***␊
- list␊
- ___␊
- list␊
{MD035:14} {MD035:16}␊
* list␊
* ---␊
* list␊
* ***␊
* list␊
* ___␊
* list␊
{MD035:24} {MD035:26}␊
`,
}
## hr-style-custom.md
> Snapshot 1