mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Update MD044/proper-names to handle names with non-word-character boundaries better.
This commit is contained in:
parent
de56bc56ed
commit
e696960aab
7 changed files with 360 additions and 16 deletions
35
lib/md044.js
35
lib/md044.js
|
|
@ -5,6 +5,9 @@
|
||||||
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, filterTokens,
|
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, filterTokens,
|
||||||
forEachInlineChild, newLineRe } = require("../helpers");
|
forEachInlineChild, newLineRe } = require("../helpers");
|
||||||
|
|
||||||
|
const startNonWordRe = /^\W/;
|
||||||
|
const 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",
|
||||||
|
|
@ -16,7 +19,10 @@ module.exports = {
|
||||||
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||||
names.forEach((name) => {
|
names.forEach((name) => {
|
||||||
const escapedName = escapeForRegExp(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");
|
const anyNameRe = new RegExp(namePattern, "gi");
|
||||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||||
function forToken(token) {
|
function forToken(token) {
|
||||||
|
|
@ -25,34 +31,31 @@ module.exports = {
|
||||||
.forEach((line, index) => {
|
.forEach((line, index) => {
|
||||||
let match = null;
|
let match = null;
|
||||||
while ((match = anyNameRe.exec(line)) !== null) {
|
while ((match = anyNameRe.exec(line)) !== null) {
|
||||||
const fullMatch = match[0];
|
const [ fullMatch, leftMatch, nameMatch, rightMatch ] = match;
|
||||||
if (fullMatch.search(bareUrlRe) === -1) {
|
if (fullMatch.search(bareUrlRe) === -1) {
|
||||||
const wordMatch = fullMatch
|
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)) {
|
if (!names.includes(wordMatch)) {
|
||||||
const lineNumber = token.lineNumber + index + fenceOffset;
|
const lineNumber = token.lineNumber + index + fenceOffset;
|
||||||
const fullLine = params.lines[lineNumber - 1];
|
const fullLine = params.lines[lineNumber - 1];
|
||||||
let matchIndex = match.index;
|
|
||||||
const matchLength = wordMatch.length;
|
const matchLength = wordMatch.length;
|
||||||
const fullLineWord =
|
const matchIndex = fullLine.indexOf(wordMatch);
|
||||||
fullLine.slice(matchIndex, matchIndex + matchLength);
|
|
||||||
if (fullLineWord !== wordMatch) {
|
|
||||||
// Attempt to fix bad offset due to inline content
|
|
||||||
matchIndex = fullLine.indexOf(wordMatch);
|
|
||||||
}
|
|
||||||
const range = (matchIndex === -1) ?
|
const range = (matchIndex === -1) ?
|
||||||
null :
|
null :
|
||||||
[ matchIndex + 1, matchLength ];
|
[ matchIndex + 1, matchLength ];
|
||||||
const fixInfo = (matchIndex === -1) ? null : {
|
const fixInfo = (matchIndex === -1) ?
|
||||||
"editColumn": matchIndex + 1,
|
null :
|
||||||
"deleteCount": matchLength,
|
{
|
||||||
"insertText": name
|
"editColumn": matchIndex + 1,
|
||||||
};
|
"deleteCount": matchLength,
|
||||||
|
"insertText": name
|
||||||
|
};
|
||||||
addErrorDetailIf(
|
addErrorDetailIf(
|
||||||
onError,
|
onError,
|
||||||
lineNumber,
|
lineNumber,
|
||||||
name,
|
name,
|
||||||
match[1],
|
nameMatch,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
range,
|
range,
|
||||||
|
|
|
||||||
10
test/detailed-results-proper-names.json
Normal file
10
test/detailed-results-proper-names.json
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD044": {
|
||||||
|
"names": [
|
||||||
|
"markdownlint",
|
||||||
|
"Node.js",
|
||||||
|
".NET"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
33
test/detailed-results-proper-names.md
Normal file
33
test/detailed-results-proper-names.md
Normal 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
|
||||||
33
test/detailed-results-proper-names.md.fixed
Normal file
33
test/detailed-results-proper-names.md.fixed
Normal 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
|
||||||
242
test/detailed-results-proper-names.results.json
Normal file
242
test/detailed-results-proper-names.results.json
Normal 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
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
10
test/proper-names-non-word-boundaries.json
Normal file
10
test/proper-names-non-word-boundaries.json
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD044": {
|
||||||
|
"names": [
|
||||||
|
".NET",
|
||||||
|
"NET.",
|
||||||
|
".NET."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
13
test/proper-names-non-word-boundaries.md
Normal file
13
test/proper-names-non-word-boundaries.md
Normal 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}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue