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,

View file

@ -0,0 +1,10 @@
{
"default": true,
"MD044": {
"names": [
"markdownlint",
"Node.js",
".NET"
]
}
}

View file

@ -0,0 +1,33 @@
# Detailed Results Proper Names
Text Markdownlint text.
Text node.JS text.
Text .net text.
Text example.net text.
Text "Markdownlint" text.
Text "node.JS" text.
Text ".net" text.
Text "example.net" text.
Text **Markdownlint** text.
Text **node.JS** text.
Text **.net** text.
Text **example.net** text.
Markdownlint
node.JS
.net
example.net

View file

@ -0,0 +1,33 @@
# Detailed Results Proper Names
Text markdownlint text.
Text Node.js text.
Text .NET text.
Text example.NET text.
Text "markdownlint" text.
Text "Node.js" text.
Text ".NET" text.
Text "example.NET" text.
Text **markdownlint** text.
Text **Node.js** text.
Text **.NET** text.
Text **example.NET** text.
markdownlint
Node.js
.NET
example.NET

View file

@ -0,0 +1,242 @@
[
{
"lineNumber": 3,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: markdownlint; Actual: Markdownlint",
"errorContext": null,
"errorRange": [
6,
12
]
},
{
"lineNumber": 5,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: Node.js; Actual: node.JS",
"errorContext": null,
"errorRange": [
6,
7
]
},
{
"lineNumber": 7,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
6,
4
]
},
{
"lineNumber": 9,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
13,
4
]
},
{
"lineNumber": 11,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: markdownlint; Actual: Markdownlint",
"errorContext": null,
"errorRange": [
7,
12
]
},
{
"lineNumber": 13,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: Node.js; Actual: node.JS",
"errorContext": null,
"errorRange": [
7,
7
]
},
{
"lineNumber": 15,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
7,
4
]
},
{
"lineNumber": 17,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
14,
4
]
},
{
"lineNumber": 19,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: markdownlint; Actual: Markdownlint",
"errorContext": null,
"errorRange": [
8,
12
]
},
{
"lineNumber": 21,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: Node.js; Actual: node.JS",
"errorContext": null,
"errorRange": [
8,
7
]
},
{
"lineNumber": 23,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
8,
4
]
},
{
"lineNumber": 25,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
15,
4
]
},
{
"lineNumber": 27,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: markdownlint; Actual: Markdownlint",
"errorContext": null,
"errorRange": [
1,
12
]
},
{
"lineNumber": 29,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: Node.js; Actual: node.JS",
"errorContext": null,
"errorRange": [
1,
7
]
},
{
"lineNumber": 31,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
1,
4
]
},
{
"lineNumber": 33,
"ruleNames": [
"MD044",
"proper-names"
],
"ruleDescription": "Proper names should have the correct capitalization",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044",
"errorDetail": "Expected: .NET; Actual: .net",
"errorContext": null,
"errorRange": [
8,
4
]
}
]

View file

@ -0,0 +1,10 @@
{
"default": true,
"MD044": {
"names": [
".NET",
"NET.",
".NET."
]
}
}

View file

@ -0,0 +1,13 @@
# Proper Names Non-Word Boundaries
Text .NET text.
Text NET. text.
Text .NET. text.
Text .net text. {MD044}
Text net. text. {MD044}
Text .net. text. {MD044}