Update MD044/proper-names to handle names with non-word-character boundaries better.

This commit is contained in:
David Anson 2020-06-19 21:08:34 -07:00
parent de56bc56ed
commit e696960aab
7 changed files with 360 additions and 16 deletions

View file

@ -5,6 +5,9 @@
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, filterTokens,
forEachInlineChild, newLineRe } = require("../helpers");
const startNonWordRe = /^\W/;
const endNonWordRe = /\W$/;
module.exports = {
"names": [ "MD044", "proper-names" ],
"description": "Proper names should have the correct capitalization",
@ -16,7 +19,10 @@ module.exports = {
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
names.forEach((name) => {
const escapedName = escapeForRegExp(name);
const namePattern = "\\S*\\b(" + escapedName + ")\\b\\S*";
const startNamePattern = startNonWordRe.test(name) ? "" : "\\S*\\b";
const endNamePattern = endNonWordRe.test(name) ? "" : "\\b\\S*";
const namePattern =
`(${startNamePattern})(${escapedName})(${endNamePattern})`;
const anyNameRe = new RegExp(namePattern, "gi");
// eslint-disable-next-line jsdoc/require-jsdoc
function forToken(token) {
@ -25,34 +31,31 @@ module.exports = {
.forEach((line, index) => {
let match = null;
while ((match = anyNameRe.exec(line)) !== null) {
const fullMatch = match[0];
const [ fullMatch, leftMatch, nameMatch, rightMatch ] = match;
if (fullMatch.search(bareUrlRe) === -1) {
const wordMatch = fullMatch
.replace(/^\W*/, "").replace(/\W*$/, "");
.replace(new RegExp(`^\\W{0,${leftMatch.length}}`), "")
.replace(new RegExp(`\\W{0,${rightMatch.length}}$`), "");
if (!names.includes(wordMatch)) {
const lineNumber = token.lineNumber + index + fenceOffset;
const fullLine = params.lines[lineNumber - 1];
let matchIndex = match.index;
const matchLength = wordMatch.length;
const fullLineWord =
fullLine.slice(matchIndex, matchIndex + matchLength);
if (fullLineWord !== wordMatch) {
// Attempt to fix bad offset due to inline content
matchIndex = fullLine.indexOf(wordMatch);
}
const matchIndex = fullLine.indexOf(wordMatch);
const range = (matchIndex === -1) ?
null :
[ matchIndex + 1, matchLength ];
const fixInfo = (matchIndex === -1) ? null : {
"editColumn": matchIndex + 1,
"deleteCount": matchLength,
"insertText": name
};
const fixInfo = (matchIndex === -1) ?
null :
{
"editColumn": matchIndex + 1,
"deleteCount": matchLength,
"insertText": name
};
addErrorDetailIf(
onError,
lineNumber,
name,
match[1],
nameMatch,
null,
null,
range,