Skip to content

Commit

Permalink
Add ESLint plugin eslint-plugin-regexp, enable recommended rules, app…
Browse files Browse the repository at this point in the history
…ly all automated fixes (refs #657).
  • Loading branch information
DavidAnson committed Dec 20, 2022
1 parent c346e0d commit 6b01a98
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"eslint:all",
"plugin:jsdoc/recommended",
"plugin:n/recommended",
"plugin:regexp/recommended",
"plugin:unicorn/all"
],
"ignorePatterns": [
Expand Down Expand Up @@ -200,6 +201,11 @@
"n/prefer-promises/dns": "error",
"n/prefer-promises/fs": "error",

"regexp/no-empty-alternative": "off",
"regexp/no-super-linear-backtracking": "off",
"regexp/no-unused-capturing-group": "off",
"regexp/no-useless-assertions": "off",

"unicorn/better-regex": "off",
"unicorn/consistent-function-scoping": "off",
"unicorn/filename-case": "off",
Expand Down
32 changes: 16 additions & 16 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ module.exports.newLineRe = newLineRe;
// Regular expression for matching common front matter (YAML and TOML)
module.exports.frontMatterRe =
// eslint-disable-next-line max-len
/((^---\s*$[^]*?^---\s*$)|(^\+\+\+\s*$[^]*?^(\+\+\+|\.\.\.)\s*$)|(^\{\s*$[^]*?^\}\s*$))(\r\n|\r|\n|$)/m;
/((^---\s*$[\s\S]*?^---\s*$)|(^\+\+\+\s*$[\s\S]*?^(\+\+\+|\.\.\.)\s*$)|(^\{\s*$[\s\S]*?^\}\s*$))(\r\n|\r|\n|$)/m;
// Regular expression for matching the start of inline disable/enable comments
const inlineCommentStartRe =
// eslint-disable-next-line max-len
/(<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file|disable-line|disable-next-line|configure-file))(?:\s|-->)/ig;
/(<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file|disable-line|disable-next-line|configure-file))(?:\s|-->)/gi;
module.exports.inlineCommentStartRe = inlineCommentStartRe;
// Regular expression for matching HTML elements
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^`>]*)?)\/?>/g;
const htmlElementRe = /<(([A-Za-z][A-Za-z\d-]*)(?:\s[^`>]*)?)\/?>/g;
module.exports.htmlElementRe = htmlElementRe;
// Regular expressions for range matching
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
Expand All @@ -55,9 +55,9 @@ const emphasisMarkersRe = /[_*]/g;
const blockquotePrefixRe = /^[>\s]*/;
module.exports.blockquotePrefixRe = blockquotePrefixRe;
// Regular expression for reference links (full, collapsed, and shortcut)
const referenceLinkRe = /!?\\?\[((?:\[[^\]\0]*]|[^[\]\0])*)](?:(?:\[([^\]\0]*)\])|([^(])|$)/g;
const referenceLinkRe = /!?\\?\[((?:\[[^\]\0]*\]|[^[\]\0])*)\](?:\[([^\]\0]*)\]|([^(])|$)/g;
// Regular expression for link reference definitions
const linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])]:/;
const linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])\]:/;
module.exports.linkReferenceDefinitionRe = linkReferenceDefinitionRe;
// All punctuation characters (normal and full-width)
const allPunctuation = ".,;:!?。,;:!?";
Expand Down Expand Up @@ -2902,7 +2902,7 @@ module.exports = {

const { addError, forEachLine, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const reversedLinkRe = /(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)](?!\()/g;
const reversedLinkRe = /(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)\](?!\()/g;
module.exports = {
"names": ["MD011", "no-reversed-links"],
"description": "Reversed link syntax",
Expand Down Expand Up @@ -3163,7 +3163,7 @@ module.exports = {
filterTokens(params, "heading_open", (token) => {
if (headingStyleFor(token) === "atx") {
const { line, lineNumber } = token;
const match = /^(#+)([ \t]{2,})(?:\S)/.exec(line);
const match = /^(#+)([ \t]{2,})\S/.exec(line);
if (match) {
const [, { "length": hashLength }, { "length": spacesLength }] = match;
addErrorContext(onError, lineNumber, line.trim(), null, null, [1, hashLength + spacesLength + 1], {
Expand Down Expand Up @@ -3346,7 +3346,7 @@ module.exports = {
// @ts-check

const { addErrorContext, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const spaceBeforeHeadingRe = /^((?:\s+)|(?:[>\s]+\s\s))[^>\s]/;
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
module.exports = {
"names": ["MD023", "heading-start-left", "header-start-left"],
"description": "Headings must start at the beginning of the line",
Expand Down Expand Up @@ -3465,7 +3465,7 @@ module.exports = {
// @ts-check

const { addError, allPunctuationNoQuestion, escapeForRegExp, forEachHeading } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const endOfLineHtmlEntityRe = /&#?[0-9a-zA-Z]+;$/;
const endOfLineHtmlEntityRe = /&#?[\da-zA-Z]+;$/;
module.exports = {
"names": ["MD026", "no-trailing-punctuation"],
"description": "Trailing punctuation in heading",
Expand Down Expand Up @@ -3798,11 +3798,11 @@ module.exports = {

const { addError, forEachLine, htmlElementRe, withinAnyRange, unescapeMarkdown } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, lineMetadata, referenceLinkImageData } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const linkDestinationRe = /]\(\s*$/;
const linkDestinationRe = /\]\(\s*$/;
// See https://spec.commonmark.org/0.29/#autolinks
const emailAddressRe =
// eslint-disable-next-line max-len
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
/^[\w.!#$%&'*+/=?^`{|}~-]+@[a-zA-Z\d](?:[a-zA-Z\d-]{0,61}[a-zA-Z\d])?(?:\.[a-zA-Z\d](?:[a-zA-Z\d-]{0,61}[a-zA-Z\d])?)*$/;
module.exports = {
"names": ["MD033", "no-inline-html"],
"description": "Inline HTML",
Expand Down Expand Up @@ -3858,7 +3858,7 @@ module.exports = {

const { addErrorContext, filterTokens, funcExpExec, urlFe, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, htmlElementRanges, referenceLinkImageData } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const htmlLinkRe = /<a(?:|\s[^>]+)>[^<>]*<\/a\s*>/ig;
const htmlLinkRe = /<a(?:|\s[^>]+)>[^<>]*<\/a\s*>/gi;
module.exports = {
"names": ["MD034", "no-bare-urls"],
"description": "Bare URL used",
Expand Down Expand Up @@ -3945,7 +3945,7 @@ module.exports = {
filterTokens(params, "hr", (token) => {
const { line, lineNumber } = token;
let { markup } = token;
const match = line.match(/[_*\-\s\t]+$/);
const match = line.match(/[_*\-\s]+$/);
if (match) {
markup = match[0].trim();
}
Expand Down Expand Up @@ -4036,8 +4036,8 @@ module.exports = {

const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { htmlElementRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*{1,3})|(_{1,3}))/g;
const embeddedUnderscoreRe = /([A-Za-z\d])_([A-Za-z\d])/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
Expand Down Expand Up @@ -4271,7 +4271,7 @@ module.exports = {
// @ts-check

const { addErrorContext, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const spaceInLinkRe = /\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=((?:\([^)]*\))|(?:\[[^\]]*\])))/;
const spaceInLinkRe = /\[(?:\s[^\]]*|[^\]]*?\s)\](?=(\([^)]*\)|\[[^\]]*\]))/;
module.exports = {
"names": ["MD039", "no-space-in-links"],
"description": "Spaces inside link text",
Expand Down
10 changes: 5 additions & 5 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ module.exports.newLineRe = newLineRe;
// Regular expression for matching common front matter (YAML and TOML)
module.exports.frontMatterRe =
// eslint-disable-next-line max-len
/((^---\s*$[^]*?^---\s*$)|(^\+\+\+\s*$[^]*?^(\+\+\+|\.\.\.)\s*$)|(^\{\s*$[^]*?^\}\s*$))(\r\n|\r|\n|$)/m;
/((^---\s*$[\s\S]*?^---\s*$)|(^\+\+\+\s*$[\s\S]*?^(\+\+\+|\.\.\.)\s*$)|(^\{\s*$[\s\S]*?^\}\s*$))(\r\n|\r|\n|$)/m;

// Regular expression for matching the start of inline disable/enable comments
const inlineCommentStartRe =
// eslint-disable-next-line max-len
/(<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file|disable-line|disable-next-line|configure-file))(?:\s|-->)/ig;
/(<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file|disable-line|disable-next-line|configure-file))(?:\s|-->)/gi;
module.exports.inlineCommentStartRe = inlineCommentStartRe;

// Regular expression for matching HTML elements
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^`>]*)?)\/?>/g;
const htmlElementRe = /<(([A-Za-z][A-Za-z\d-]*)(?:\s[^`>]*)?)\/?>/g;
module.exports.htmlElementRe = htmlElementRe;

// Regular expressions for range matching
Expand All @@ -35,10 +35,10 @@ module.exports.blockquotePrefixRe = blockquotePrefixRe;

// Regular expression for reference links (full, collapsed, and shortcut)
const referenceLinkRe =
/!?\\?\[((?:\[[^\]\0]*]|[^[\]\0])*)](?:(?:\[([^\]\0]*)\])|([^(])|$)/g;
/!?\\?\[((?:\[[^\]\0]*\]|[^[\]\0])*)\](?:\[([^\]\0]*)\]|([^(])|$)/g;

// Regular expression for link reference definitions
const linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])]:/;
const linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])\]:/;
module.exports.linkReferenceDefinitionRe = linkReferenceDefinitionRe;

// All punctuation characters (normal and full-width)
Expand Down
2 changes: 1 addition & 1 deletion lib/md011.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { addError, forEachLine, withinAnyRange } = require("../helpers");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");

const reversedLinkRe =
/(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)](?!\()/g;
/(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)\](?!\()/g;

module.exports = {
"names": [ "MD011", "no-reversed-links" ],
Expand Down
2 changes: 1 addition & 1 deletion lib/md019.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
filterTokens(params, "heading_open", (token) => {
if (headingStyleFor(token) === "atx") {
const { line, lineNumber } = token;
const match = /^(#+)([ \t]{2,})(?:\S)/.exec(line);
const match = /^(#+)([ \t]{2,})\S/.exec(line);
if (match) {
const [
,
Expand Down
2 changes: 1 addition & 1 deletion lib/md023.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const { addErrorContext, filterTokens } = require("../helpers");

const spaceBeforeHeadingRe = /^((?:\s+)|(?:[>\s]+\s\s))[^>\s]/;
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;

module.exports = {
"names": [ "MD023", "heading-start-left", "header-start-left" ],
Expand Down
2 changes: 1 addition & 1 deletion lib/md026.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const { addError, allPunctuationNoQuestion, escapeForRegExp, forEachHeading } =
require("../helpers");

const endOfLineHtmlEntityRe = /&#?[0-9a-zA-Z]+;$/;
const endOfLineHtmlEntityRe = /&#?[\da-zA-Z]+;$/;

module.exports = {
"names": [ "MD026", "no-trailing-punctuation" ],
Expand Down
4 changes: 2 additions & 2 deletions lib/md033.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const {
const { codeBlockAndSpanRanges, lineMetadata, referenceLinkImageData } =
require("./cache");

const linkDestinationRe = /]\(\s*$/;
const linkDestinationRe = /\]\(\s*$/;
// See https://spec.commonmark.org/0.29/#autolinks
const emailAddressRe =
// eslint-disable-next-line max-len
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
/^[\w.!#$%&'*+/=?^`{|}~-]+@[a-zA-Z\d](?:[a-zA-Z\d-]{0,61}[a-zA-Z\d])?(?:\.[a-zA-Z\d](?:[a-zA-Z\d-]{0,61}[a-zA-Z\d])?)*$/;

module.exports = {
"names": [ "MD033", "no-inline-html" ],
Expand Down
2 changes: 1 addition & 1 deletion lib/md034.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { addErrorContext, filterTokens, funcExpExec, urlFe, withinAnyRange } =
const { codeBlockAndSpanRanges, htmlElementRanges, referenceLinkImageData } =
require("./cache");

const htmlLinkRe = /<a(?:|\s[^>]+)>[^<>]*<\/a\s*>/ig;
const htmlLinkRe = /<a(?:|\s[^>]+)>[^<>]*<\/a\s*>/gi;

module.exports = {
"names": [ "MD034", "no-bare-urls" ],
Expand Down
2 changes: 1 addition & 1 deletion lib/md035.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
filterTokens(params, "hr", (token) => {
const { line, lineNumber } = token;
let { markup } = token;
const match = line.match(/[_*\-\s\t]+$/);
const match = line.match(/[_*\-\s]+$/);
if (match) {
markup = match[0].trim();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/md037.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine,
withinAnyRange } = require("../helpers");
const { htmlElementRanges, lineMetadata } = require("./cache");

const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*{1,3})|(_{1,3}))/g;
const embeddedUnderscoreRe = /([A-Za-z\d])_([A-Za-z\d])/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
Expand Down
2 changes: 1 addition & 1 deletion lib/md039.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const { addErrorContext, filterTokens } = require("../helpers");

const spaceInLinkRe =
/\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=((?:\([^)]*\))|(?:\[[^\]]*\])))/;
/\[(?:\s[^\]]*|[^\]]*?\s)\](?=(\([^)]*\)|\[[^\]]*\]))/;

module.exports = {
"names": [ "MD039", "no-space-in-links" ],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"eslint-plugin-es": "4.1.0",
"eslint-plugin-jsdoc": "39.6.4",
"eslint-plugin-n": "15.6.0",
"eslint-plugin-regexp": "1.11.0",
"eslint-plugin-unicorn": "45.0.2",
"globby": "13.1.3",
"js-yaml": "4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion test/markdownlint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ test("customFrontMatter", (t) => new Promise((resolve) => {
"strings": {
"content": "<head>\n\t\n</head>\n# Heading\n"
},
"frontMatter": /<head>[^]*<\/head>/,
"frontMatter": /<head>[\s\S]*<\/head>/,
"config": {
"default": false,
"MD010": true
Expand Down
2 changes: 1 addition & 1 deletion test/rules/lint-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const languageJavaScript = /js|javascript/i;
function cleanJsdocRulesFromEslintConfig(config) {
const cleanedConfig = { ...config };
for (const rule in config.rules) {
if (/^(es|jsdoc|n|unicorn)\//.test(rule)) {
if (/^(es|jsdoc|n|regexp|unicorn)\//.test(rule)) {
delete cleanedConfig.rules[rule];
}
}
Expand Down

0 comments on commit 6b01a98

Please sign in to comment.