Reimplement MD026/no-trailing-punctuation using micromark tokens, ignore trailing GitHub emoji codes, improve tests (fixes #457).

This commit is contained in:
David Anson 2023-06-24 15:45:51 -07:00
parent 272c15ed39
commit c06506c317
9 changed files with 157 additions and 32 deletions

View file

@ -81,6 +81,16 @@ module.exports.blockquotePrefixRe = blockquotePrefixRe;
var linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])\]:/;
module.exports.linkReferenceDefinitionRe = linkReferenceDefinitionRe;
// Regular expression for identifying an HTML entity at the end of a line
module.exports.endOfLineHtmlEntityRe =
// eslint-disable-next-line max-len
/&(?:#\d+|#[xX][\da-fA-F]+|[a-zA-Z]{2,31}|blk\d{2}|emsp1[34]|frac\d{2}|sup\d|there4);$/;
// Regular expression for identifying a GitHub emoji code at the end of a line
module.exports.endOfLineGemojiCodeRe =
// eslint-disable-next-line max-len
/:(?:[abmovx]|[-+]1|100|1234|(?:1st|2nd|3rd)_place_medal|8ball|clock\d{1,4}|e-mail|non-potable_water|o2|t-rex|u5272|u5408|u55b6|u6307|u6708|u6709|u6e80|u7121|u7533|u7981|u7a7a|[a-z]{2,15}2?|[a-z]{1,14}(?:_[a-z\d]{1,16})+):$/;
// All punctuation characters (normal and full-width)
var allPunctuation = ".,;:!?。,;:!?";
module.exports.allPunctuation = allPunctuation;
@ -4434,12 +4444,17 @@ 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"),
addError = _require.addError,
allPunctuationNoQuestion = _require.allPunctuationNoQuestion,
escapeForRegExp = _require.escapeForRegExp,
forEachHeading = _require.forEachHeading;
var endOfLineHtmlEntityRe = /&#?[\da-zA-Z]+;$/;
endOfLineGemojiCodeRe = _require.endOfLineGemojiCodeRe,
endOfLineHtmlEntityRe = _require.endOfLineHtmlEntityRe,
escapeForRegExp = _require.escapeForRegExp;
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
filterByTypes = _require2.filterByTypes;
module.exports = {
"names": ["MD026", "no-trailing-punctuation"],
"description": "Trailing punctuation in heading",
@ -4448,21 +4463,31 @@ module.exports = {
var punctuation = params.config.punctuation;
punctuation = String(punctuation === undefined ? allPunctuationNoQuestion : punctuation);
var trailingPunctuationRe = new RegExp("\\s*[" + escapeForRegExp(punctuation) + "]+$");
forEachHeading(params, function (heading) {
var line = heading.line,
lineNumber = heading.lineNumber;
var trimmedLine = line.replace(/([^\s#])[\s#]+$/, "$1");
var match = trailingPunctuationRe.exec(trimmedLine);
if (match && !endOfLineHtmlEntityRe.test(trimmedLine)) {
var fullMatch = match[0];
var column = match.index + 1;
var length = fullMatch.length;
addError(onError, lineNumber, "Punctuation: '".concat(fullMatch, "'"), null, [column, length], {
"editColumn": column,
"deleteCount": length
});
var headings = filterByTypes(params.parsers.micromark.tokens, ["atxHeadingText", "setextHeadingText"]);
var _iterator = _createForOfIteratorHelper(headings),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var heading = _step.value;
var endLine = heading.endLine,
startColumn = heading.startColumn,
text = heading.text;
var match = trailingPunctuationRe.exec(text);
if (match && !endOfLineHtmlEntityRe.test(text) && !endOfLineGemojiCodeRe.test(text)) {
var fullMatch = match[0];
var column = startColumn + match.index;
var length = fullMatch.length;
addError(onError, endLine, "Punctuation: '".concat(fullMatch, "'"), undefined, [column, length], {
"editColumn": column,
"deleteCount": length
});
}
}
});
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
};

View file

@ -39,6 +39,16 @@ module.exports.blockquotePrefixRe = blockquotePrefixRe;
const linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])\]:/;
module.exports.linkReferenceDefinitionRe = linkReferenceDefinitionRe;
// Regular expression for identifying an HTML entity at the end of a line
module.exports.endOfLineHtmlEntityRe =
// eslint-disable-next-line max-len
/&(?:#\d+|#[xX][\da-fA-F]+|[a-zA-Z]{2,31}|blk\d{2}|emsp1[34]|frac\d{2}|sup\d|there4);$/;
// Regular expression for identifying a GitHub emoji code at the end of a line
module.exports.endOfLineGemojiCodeRe =
// eslint-disable-next-line max-len
/:(?:[abmovx]|[-+]1|100|1234|(?:1st|2nd|3rd)_place_medal|8ball|clock\d{1,4}|e-mail|non-potable_water|o2|t-rex|u5272|u5408|u55b6|u6307|u6708|u6709|u6e80|u7121|u7533|u7981|u7a7a|[a-z]{2,15}2?|[a-z]{1,14}(?:_[a-z\d]{1,16})+):$/;
// All punctuation characters (normal and full-width)
const allPunctuation = ".,;:!?。,;:!?";
module.exports.allPunctuation = allPunctuation;

View file

@ -2,10 +2,10 @@
"use strict";
const { addError, allPunctuationNoQuestion, escapeForRegExp, forEachHeading } =
require("../helpers");
const { addError, allPunctuationNoQuestion, endOfLineGemojiCodeRe,
endOfLineHtmlEntityRe, escapeForRegExp } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");
const endOfLineHtmlEntityRe = /&#?[\da-zA-Z]+;$/;
module.exports = {
"names": [ "MD026", "no-trailing-punctuation" ],
@ -18,19 +18,26 @@ module.exports = {
);
const trailingPunctuationRe =
new RegExp("\\s*[" + escapeForRegExp(punctuation) + "]+$");
forEachHeading(params, (heading) => {
const { line, lineNumber } = heading;
const trimmedLine = line.replace(/([^\s#])[\s#]+$/, "$1");
const match = trailingPunctuationRe.exec(trimmedLine);
if (match && !endOfLineHtmlEntityRe.test(trimmedLine)) {
const headings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeadingText", "setextHeadingText" ]
);
for (const heading of headings) {
const { endLine, startColumn, text } = heading;
const match = trailingPunctuationRe.exec(text);
if (
match &&
!endOfLineHtmlEntityRe.test(text) &&
!endOfLineGemojiCodeRe.test(text)
) {
const fullMatch = match[0];
const column = match.index + 1;
const column = startColumn + match.index;
const length = fullMatch.length;
addError(
onError,
lineNumber,
endLine,
`Punctuation: '${fullMatch}'`,
null,
undefined,
[ column, length ],
{
"editColumn": column,
@ -38,6 +45,6 @@ module.exports = {
}
);
}
});
}
}
};

View file

@ -73,12 +73,14 @@
"ava": "5.3.1",
"babel-loader": "9.1.2",
"c8": "8.0.0",
"character-entities": "2.0.2",
"eslint": "8.43.0",
"eslint-plugin-es": "4.1.0",
"eslint-plugin-jsdoc": "46.2.6",
"eslint-plugin-n": "16.0.0",
"eslint-plugin-regexp": "1.15.0",
"eslint-plugin-unicorn": "47.0.0",
"gemoji": "8.1.0",
"globby": "13.2.0",
"js-yaml": "4.1.0",
"markdown-it-for-inline": "0.1.1",

View file

@ -0,0 +1,25 @@
# headings-with-emoji
## Known Issues :bug:
## Love :heartpulse:
## :tada:
## :checkered_flag:
## :clock930:
## :t-rex:
## Boba:bubble_tea:
<!-- markdownlint-disable heading-style -->
## Fix the :bug: ##
Another :heartpulse:
--------------------
:eyes:
------

View file

@ -12,6 +12,8 @@
## Copyright 2004 &#x000A9;
## Copyright 2004 &#X000A9;
## Copyright 2005 &#169;
## Copyright 2006 &#169; ##

View file

@ -1283,3 +1283,21 @@ Empty bracket pairs: [text3[]][]
};
return markdownlint(options).then(() => null);
});
test("endOfLineHtmlEntityRe", async(t) => {
const { characterEntities } = await import("character-entities");
const entities = Object.keys(characterEntities);
t.plan(entities.length);
for (const entity of entities) {
t.true(helpers.endOfLineHtmlEntityRe.test(`-&${entity};`), entity);
}
});
test("endOfLineGemojiCodeRe", async(t) => {
const { gemoji } = await import("gemoji");
const emojis = gemoji.flatMap((i) => i.names);
t.plan(emojis.length);
for (const emoji of emojis) {
t.true(helpers.endOfLineGemojiCodeRe.test(`-:${emoji}:`), emoji);
}
});

View file

@ -14772,6 +14772,40 @@ Generated by [AVA](https://avajs.dev).
`,
}
## headings-with-emoji.md
> Snapshot 1
{
errors: [],
fixed: `# headings-with-emoji␊
## Known Issues :bug:␊
## Love :heartpulse:␊
## :tada:␊
## :checkered_flag:␊
## :clock930:␊
## :t-rex:␊
## Boba:bubble_tea:␊
<!-- markdownlint-disable heading-style -->
## Fix the :bug: ##␊
Another :heartpulse:␊
--------------------␊
:eyes:␊
------␊
`,
}
## headings-with-html-entities.md
> Snapshot 1
@ -14789,7 +14823,7 @@ Generated by [AVA](https://avajs.dev).
deleteCount: 1,
editColumn: 31,
},
lineNumber: 22,
lineNumber: 24,
ruleDescription: 'Trailing punctuation in heading',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md026.md',
ruleNames: [
@ -14808,7 +14842,7 @@ Generated by [AVA](https://avajs.dev).
deleteCount: 1,
editColumn: 34,
},
lineNumber: 24,
lineNumber: 26,
ruleDescription: 'Trailing punctuation in heading',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md026.md',
ruleNames: [
@ -14827,7 +14861,7 @@ Generated by [AVA](https://avajs.dev).
deleteCount: 1,
editColumn: 31,
},
lineNumber: 26,
lineNumber: 28,
ruleDescription: 'Trailing punctuation in heading',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md026.md',
ruleNames: [
@ -14850,6 +14884,8 @@ Generated by [AVA](https://avajs.dev).
## Copyright 2004 &#x000A9;
## Copyright 2004 &#X000A9;
## Copyright 2005 &#169;
## Copyright 2006 &#169; ##␊