Re-implement MD044/proper-names for better accuracy (range and fixInfo are now always valid) (fixes #402, fixes #403).

This commit is contained in:
David Anson 2021-06-12 17:10:59 -07:00
parent fb5f647368
commit 4db40256d9
7 changed files with 135 additions and 150 deletions

View file

@ -3745,9 +3745,8 @@ module.exports = {
"use strict"; "use strict";
// @ts-check // @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addErrorDetailIf = _a.addErrorDetailIf, bareUrlRe = _a.bareUrlRe, escapeForRegExp = _a.escapeForRegExp, filterTokens = _a.filterTokens, forEachInlineChild = _a.forEachInlineChild, newLineRe = _a.newLineRe; var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addErrorDetailIf = _a.addErrorDetailIf, bareUrlRe = _a.bareUrlRe, escapeForRegExp = _a.escapeForRegExp, forEachLine = _a.forEachLine, newLineRe = _a.newLineRe, forEachInlineCodeSpan = _a.forEachInlineCodeSpan;
var startNonWordRe = /^\W/; var lineMetadata = __webpack_require__(/*! ./cache */ "../lib/cache.js").lineMetadata;
var endNonWordRe = /\W$/;
module.exports = { module.exports = {
"names": ["MD044", "proper-names"], "names": ["MD044", "proper-names"],
"description": "Proper names should have the correct capitalization", "description": "Proper names should have the correct capitalization",
@ -3755,73 +3754,55 @@ module.exports = {
"function": function MD044(params, onError) { "function": function MD044(params, onError) {
var names = params.config.names; var names = params.config.names;
names = Array.isArray(names) ? names : []; names = Array.isArray(names) ? names : [];
names.sort(function (a, b) { return (b.length - a.length) || a.localeCompare(b); });
var codeBlocks = params.config.code_blocks; var codeBlocks = params.config.code_blocks;
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks; var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
// Text of automatic hyperlinks is implicitly a URL var exclusions = [];
var autolinkText = new Set(); if (!includeCodeBlocks) {
filterTokens(params, "inline", function (token) { forEachInlineCodeSpan(params.lines.join("\n"), function (code, lineIndex, columnIndex) {
var inAutoLink = false; var codeLines = code.split(newLineRe);
token.children.forEach(function (child) { // eslint-disable-next-line unicorn/no-for-loop
var info = child.info, type = child.type; for (var i = 0; i < codeLines.length; i++) {
if ((type === "link_open") && (info === "auto")) { exclusions.push([lineIndex + i, columnIndex, codeLines[i].length]);
inAutoLink = true; columnIndex = 0;
}
else if (type === "link_close") {
inAutoLink = false;
}
else if ((type === "text") && inAutoLink) {
autolinkText.add(child);
} }
}); });
}); }
// For each proper name... var _loop_1 = function (name_1) {
names.forEach(function (name) { var escapedName = escapeForRegExp(name_1);
var escapedName = escapeForRegExp(name); var startNamePattern = /^\W/.test(name_1) ? "" : "[^\\s([\"]*\\b_*";
var startNamePattern = startNonWordRe.test(name) ? "" : "\\S*\\b"; var endNamePattern = /\W$/.test(name_1) ? "" : "_*\\b[^\\s)\\]\"]*";
var endNamePattern = endNonWordRe.test(name) ? "" : "\\b\\S*"; var namePattern = "(" + startNamePattern + ")(" + escapedName + ")" + endNamePattern;
var namePattern = "(" + startNamePattern + ")(" + escapedName + ")(" + endNamePattern + ")"; var nameRe = new RegExp(namePattern, "gi");
var anyNameRe = new RegExp(namePattern, "gi"); forEachLine(lineMetadata(), function (line, lineIndex, inCode, onFence) {
// eslint-disable-next-line jsdoc/require-jsdoc if (includeCodeBlocks || (!inCode && !onFence)) {
function forToken(token) { var match = null;
if (!autolinkText.has(token)) { var _loop_2 = function () {
var fenceOffset_1 = (token.type === "fence") ? 1 : 0; var fullMatch = match[0], leftMatch = match[1], nameMatch = match[2];
token.content.split(newLineRe).forEach(function (line, index) { var index = match.index + leftMatch.length;
var match = null; var length_1 = nameMatch.length;
while ((match = anyNameRe.exec(line)) !== null) { if ((fullMatch.search(bareUrlRe) === -1) &&
var fullMatch = match[0], leftMatch = match[1], nameMatch = match[2], rightMatch = match[3]; exclusions.every(function (span) { return ((lineIndex !== span[0]) ||
if (fullMatch.search(bareUrlRe) === -1) { (index + length_1 < span[1]) ||
var wordMatch = fullMatch (index > span[1] + span[2])); })) {
.replace(new RegExp("^\\W{0," + leftMatch.length + "}"), "") addErrorDetailIf(onError, lineIndex + 1, name_1, nameMatch, null, null, [index + 1, length_1], {
.replace(new RegExp("\\W{0," + rightMatch.length + "}$"), ""); "editColumn": index + 1,
if (!names.includes(wordMatch)) { "deleteCount": length_1,
var lineNumber = token.lineNumber + index + fenceOffset_1; "insertText": name_1
var fullLine = params.lines[lineNumber - 1]; });
var matchLength = wordMatch.length;
var matchIndex = fullLine.indexOf(wordMatch);
var range = (matchIndex === -1) ?
null :
[matchIndex + 1, matchLength];
var fixInfo = (matchIndex === -1) ?
null :
{
"editColumn": matchIndex + 1,
"deleteCount": matchLength,
"insertText": name
};
addErrorDetailIf(onError, lineNumber, name, nameMatch, null, null, range, fixInfo);
}
}
} }
}); exclusions.push([lineIndex, index, length_1]);
};
while ((match = nameRe.exec(line)) !== null) {
_loop_2();
}
} }
} });
forEachInlineChild(params, "text", forToken); };
if (includeCodeBlocks) { for (var _i = 0, names_1 = names; _i < names_1.length; _i++) {
forEachInlineChild(params, "code_inline", forToken); var name_1 = names_1[_i];
filterTokens(params, "code_block", forToken); _loop_1(name_1);
filterTokens(params, "fence", forToken); }
}
});
} }
}; };

View file

@ -2,11 +2,9 @@
"use strict"; "use strict";
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, filterTokens, const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine, newLineRe,
forEachInlineChild, newLineRe } = require("../helpers"); forEachInlineCodeSpan } = require("../helpers");
const { lineMetadata } = require("./cache");
const startNonWordRe = /^\W/;
const endNonWordRe = /\W$/;
module.exports = { module.exports = {
"names": [ "MD044", "proper-names" ], "names": [ "MD044", "proper-names" ],
@ -15,80 +13,66 @@ module.exports = {
"function": function MD044(params, onError) { "function": function MD044(params, onError) {
let names = params.config.names; let names = params.config.names;
names = Array.isArray(names) ? names : []; names = Array.isArray(names) ? names : [];
names.sort((a, b) => (b.length - a.length) || a.localeCompare(b));
const codeBlocks = params.config.code_blocks; const codeBlocks = params.config.code_blocks;
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks; const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
// Text of automatic hyperlinks is implicitly a URL const exclusions = [];
const autolinkText = new Set(); if (!includeCodeBlocks) {
filterTokens(params, "inline", (token) => { forEachInlineCodeSpan(
let inAutoLink = false; params.lines.join("\n"),
token.children.forEach((child) => { (code, lineIndex, columnIndex) => {
const { info, type } = child; const codeLines = code.split(newLineRe);
if ((type === "link_open") && (info === "auto")) { // eslint-disable-next-line unicorn/no-for-loop
inAutoLink = true; for (let i = 0; i < codeLines.length; i++) {
} else if (type === "link_close") { exclusions.push(
inAutoLink = false; [ lineIndex + i, columnIndex, codeLines[i].length ]
} else if ((type === "text") && inAutoLink) { );
autolinkText.add(child); columnIndex = 0;
}
}
);
}
for (const name of names) {
const escapedName = escapeForRegExp(name);
const startNamePattern = /^\W/.test(name) ? "" : "[^\\s([\"]*\\b_*";
const endNamePattern = /\W$/.test(name) ? "" : "_*\\b[^\\s)\\]\"]*";
const namePattern =
`(${startNamePattern})(${escapedName})${endNamePattern}`;
const nameRe = new RegExp(namePattern, "gi");
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence) => {
if (includeCodeBlocks || (!inCode && !onFence)) {
let match = null;
while ((match = nameRe.exec(line)) !== null) {
const [ fullMatch, leftMatch, nameMatch ] = match;
const index = match.index + leftMatch.length;
const length = nameMatch.length;
if (
(fullMatch.search(bareUrlRe) === -1) &&
exclusions.every((span) => (
(lineIndex !== span[0]) ||
(index + length < span[1]) ||
(index > span[1] + span[2])
))
) {
addErrorDetailIf(
onError,
lineIndex + 1,
name,
nameMatch,
null,
null,
[ index + 1, length ],
{
"editColumn": index + 1,
"deleteCount": length,
"insertText": name
}
);
}
exclusions.push([ lineIndex, index, length ]);
}
} }
}); });
}); }
// For each proper name...
names.forEach((name) => {
const escapedName = escapeForRegExp(name);
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) {
if (!autolinkText.has(token)) {
const fenceOffset = (token.type === "fence") ? 1 : 0;
token.content.split(newLineRe).forEach((line, index) => {
let match = null;
while ((match = anyNameRe.exec(line)) !== null) {
const [ fullMatch, leftMatch, nameMatch, rightMatch ] = match;
if (fullMatch.search(bareUrlRe) === -1) {
const wordMatch = fullMatch
.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];
const matchLength = wordMatch.length;
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
};
addErrorDetailIf(
onError,
lineNumber,
name,
nameMatch,
null,
null,
range,
fixInfo
);
}
}
}
});
}
}
forEachInlineChild(params, "text", forToken);
if (includeCodeBlocks) {
forEachInlineChild(params, "code_inline", forToken);
filterTokens(params, "code_block", forToken);
filterTokens(params, "fence", forToken);
}
});
} }
}; };

View file

@ -29,6 +29,7 @@ function createTestForFile(file) {
.then( .then(
function configFileExists() { function configFileExists() {
return fs.promises.readFile(configFile, "utf8") return fs.promises.readFile(configFile, "utf8")
// @ts-ignore
.then(JSON.parse); .then(JSON.parse);
}, },
function noConfigFile() { function noConfigFile() {

View file

@ -22,4 +22,22 @@ node.js is runtime
A short paragraph A short paragraph
about node.js and {MD044} about node.js and {MD044}
javascript. {MD044} also javascript. {MD044}
`javascript`
`code
javascript`
`code
javascript
code`
`javascript
code`
text JavaScript text `javascript` text JavaScript text
text `javascript` text JavaScript text `javascript` text
text javascript text `javascript` text {MD044}
text `javascript` text javascript text {MD044}

View file

@ -6,7 +6,6 @@
"github.com", "github.com",
"github.com/about", "github.com/about",
"npm", "npm",
"NPM",
"Vue", "Vue",
"Vuex", "Vuex",
"vue-router" "vue-router"

View file

@ -32,14 +32,16 @@ The library vue-router
Not Vue-router {MD044} Not Vue-router {MD044}
Or vue-router-extra {MD044} But vue-router-extra is different
Or extra-vue-router {MD044} As is extra-vue-router
Quoted "Vue" and "vue-router" Quoted "Vue" and "vue-router"
Emphasized *Vue* and *vue-router* Emphasized *Vue* and *vue-router*
Underscored _Vue_ and _vue-router_
Call it npm Call it npm
Or NPM
But not Npm {MD044} But not Npm {MD044}
Or NPM {MD044}

View file

@ -72,12 +72,12 @@ javascript. {MD044}
{MD044} `javascript` {MD044} `javascript`
{MD044} `code `code
javascript` javascript` {MD044}
{MD044} `code `code
javascript javascript {MD044}
code` code`
{MD044} `javascript `javascript {MD044}
code` code`