Enable ESLint rule unicorn/no-array-for-each, auto-fix all violations, manually address new issues for ~4% time reduction measured via profile-fixture.mjs on Apple Silicon M1.

This commit is contained in:
David Anson 2022-06-08 22:10:27 -07:00
parent 15efcb4282
commit b6471fba31
34 changed files with 414 additions and 389 deletions

View file

@ -196,7 +196,6 @@
"unicorn/consistent-function-scoping": "off",
"unicorn/filename-case": "off",
"unicorn/no-array-callback-reference": "off",
"unicorn/no-array-for-each": "off",
"unicorn/no-keyword-prefix": "off",
"unicorn/no-new-array": "off",
"unicorn/no-null": "off",

View file

@ -327,11 +327,11 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
filterTokens(params, "hr", (token) => {
lineMetadata[token.map[0]][6] = true;
});
params.tokens.filter(isMathBlock).forEach((token) => {
for (const token of params.tokens.filter(isMathBlock)) {
for (let i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][7] = true;
}
});
}
return lineMetadata;
};
/**
@ -343,9 +343,9 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
* @returns {void}
*/
function forEachLine(lineMetadata, handler) {
lineMetadata.forEach(function forMetadata(metadata) {
for (const metadata of lineMetadata) {
handler(...metadata);
});
}
}
module.exports.forEachLine = forEachLine;
// Returns (nested) lists as a flat array (in order)
@ -356,7 +356,7 @@ module.exports.flattenLists = function flattenLists(tokens) {
let nesting = 0;
const nestingStack = [];
let lastWithMap = { "map": [0, 1] };
tokens.forEach((token) => {
for (const token of tokens) {
if ((token.type === "bullet_list_open") ||
(token.type === "ordered_list_open")) {
// Save current context and start a new one
@ -399,24 +399,24 @@ module.exports.flattenLists = function flattenLists(tokens) {
// Track last token with map
lastWithMap = token;
}
});
}
return flattenedLists;
};
// Calls the provided function for each specified inline child token
module.exports.forEachInlineChild =
function forEachInlineChild(params, type, handler) {
filterTokens(params, "inline", function forToken(token) {
token.children.forEach(function forChild(child) {
for (const child of token.children) {
if (child.type === type) {
handler(child, token);
}
});
}
});
};
// Calls the provided function for each heading's content
module.exports.forEachHeading = function forEachHeading(params, handler) {
let heading = null;
params.tokens.forEach(function forToken(token) {
for (const token of params.tokens) {
if (token.type === "heading_open") {
heading = token;
}
@ -426,7 +426,7 @@ module.exports.forEachHeading = function forEachHeading(params, handler) {
else if ((token.type === "inline") && heading) {
handler(heading, token.content, token);
}
});
}
};
/**
* Calls the provided function for each inline code span's content.
@ -751,7 +751,7 @@ function emphasisMarkersInContent(params) {
const { lines } = params;
const byLine = new Array(lines.length);
// Search links
lines.forEach((tokenLine, tokenLineIndex) => {
for (const [tokenLineIndex, tokenLine] of lines.entries()) {
const inLine = [];
forEachLink(tokenLine, (index, match) => {
let markerMatch = null;
@ -760,7 +760,7 @@ function emphasisMarkersInContent(params) {
}
});
byLine[tokenLineIndex] = inLine;
});
}
// Search code spans
filterTokens(params, "inline", (token) => {
const { children, lineNumber, map } = token;
@ -768,7 +768,7 @@ function emphasisMarkersInContent(params) {
const tokenLines = lines.slice(map[0], map[1]);
forEachInlineCodeSpan(tokenLines.join("\n"), (code, lineIndex, column, tickCount) => {
const codeLines = code.split(newLineRe);
codeLines.forEach((codeLine, codeLineIndex) => {
for (const [codeLineIndex, codeLine] of codeLines.entries()) {
const byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex;
const inLine = byLine[byLineIndex];
const codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount;
@ -777,7 +777,7 @@ function emphasisMarkersInContent(params) {
inLine.push(codeLineOffset + match.index);
}
byLine[byLineIndex] = inLine;
});
}
});
}
});
@ -924,7 +924,7 @@ function getPreferredLineEnding(input, os) {
let lf = 0;
let crlf = 0;
const endings = input.match(newLineRe) || [];
endings.forEach((ending) => {
for (const ending of endings) {
// eslint-disable-next-line default-case
switch (ending) {
case "\r":
@ -937,7 +937,7 @@ function getPreferredLineEnding(input, os) {
crlf++;
break;
}
});
}
let preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding = (os && os.EOL) || "\n";
@ -1021,8 +1021,10 @@ function applyFixes(input, errors) {
return unique;
});
// Collapse insert/no-delete and no-insert/delete for same line/column
lastFixInfo = {};
fixInfos.forEach((fixInfo) => {
lastFixInfo = {
"lineNumber": -1
};
for (const fixInfo of fixInfos) {
if ((fixInfo.lineNumber === lastFixInfo.lineNumber) &&
(fixInfo.editColumn === lastFixInfo.editColumn) &&
!fixInfo.insertText &&
@ -1033,12 +1035,12 @@ function applyFixes(input, errors) {
lastFixInfo.lineNumber = 0;
}
lastFixInfo = fixInfo;
});
}
fixInfos = fixInfos.filter((fixInfo) => fixInfo.lineNumber);
// Apply all (remaining/updated) fixes
let lastLineIndex = -1;
let lastEditIndex = -1;
fixInfos.forEach((fixInfo) => {
for (const fixInfo of fixInfos) {
const { lineNumber, editColumn, deleteCount } = fixInfo;
const lineIndex = lineNumber - 1;
const editIndex = editColumn - 1;
@ -1050,7 +1052,7 @@ function applyFixes(input, errors) {
}
lastLineIndex = lineIndex;
lastEditIndex = editIndex;
});
}
// Return corrected input
return lines.filter((line) => line !== null).join(lineEnding);
}
@ -1289,31 +1291,31 @@ function validateRuleList(ruleList, synchronous) {
return result;
}
const allIds = {};
ruleList.forEach(function forRule(rule, index) {
for (const [index, rule] of ruleList.entries()) {
const customIndex = index - rules.length;
// eslint-disable-next-line jsdoc/require-jsdoc
// eslint-disable-next-line no-inner-declarations, jsdoc/require-jsdoc
function newError(property) {
return new Error("Property '" + property + "' of custom rule at index " +
customIndex + " is incorrect.");
}
["names", "tags"].forEach(function forProperty(property) {
for (const property of ["names", "tags"]) {
const value = rule[property];
if (!result &&
(!value || !Array.isArray(value) || (value.length === 0) ||
!value.every(helpers.isString) || value.some(helpers.isEmptyString))) {
result = newError(property);
}
});
[
}
for (const propertyInfo of [
["description", "string"],
["function", "function"]
].forEach(function forProperty(propertyInfo) {
]) {
const property = propertyInfo[0];
const value = rule[property];
if (!result && (!value || (typeof value !== propertyInfo[1]))) {
result = newError(property);
}
});
}
if (!result &&
rule.information &&
(Object.getPrototypeOf(rule.information) !== URL.prototype)) {
@ -1329,24 +1331,25 @@ function validateRuleList(ruleList, synchronous) {
" is asynchronous and can not be used in a synchronous context.");
}
if (!result) {
rule.names.forEach(function forName(name) {
for (const name of rule.names) {
const nameUpper = name.toUpperCase();
if (!result && (allIds[nameUpper] !== undefined)) {
result = new Error("Name '" + name + "' of custom rule at index " +
customIndex + " is already used as a name or tag.");
}
allIds[nameUpper] = true;
});
rule.tags.forEach(function forTag(tag) {
}
for (const tag of rule.tags) {
const tagUpper = tag.toUpperCase();
if (!result && allIds[tagUpper]) {
result = new Error("Tag '" + tag + "' of custom rule at index " +
customIndex + " is already used as a name.");
}
allIds[tagUpper] = false;
});
}
});
}
}
// @ts-ignore
return result;
}
/**
@ -1363,10 +1366,10 @@ function newResults(ruleList) {
const results = [];
const keys = Object.keys(lintResults);
keys.sort();
keys.forEach(function forFile(file) {
for (const file of keys) {
const fileResults = lintResults[file];
if (Array.isArray(fileResults)) {
fileResults.forEach(function forResult(result) {
for (const result of fileResults) {
const ruleMoniker = result.ruleNames ?
result.ruleNames.join("/") :
(result.ruleName + "/" + result.ruleAlias);
@ -1380,30 +1383,32 @@ function newResults(ruleList) {
(result.errorContext ?
" [Context: \"" + result.errorContext + "\"]" :
""));
});
}
}
else {
if (!ruleNameToRule) {
ruleNameToRule = {};
ruleList.forEach(function forRule(rule) {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
ruleNameToRule[ruleName] = rule;
});
}
Object.keys(fileResults).forEach(function forRule(ruleName) {
}
for (const [ruleName, ruleResults] of Object.entries(fileResults)) {
const rule = ruleNameToRule[ruleName.toUpperCase()];
const ruleResults = fileResults[ruleName];
ruleResults.forEach(function forLine(lineNumber) {
for (const lineNumber of ruleResults) {
// @ts-ignore
const nameIndex = Math.min(useAlias ? 1 : 0, rule.names.length - 1);
const result = file + ": " +
lineNumber + ": " +
// @ts-ignore
rule.names[nameIndex] + " " +
// @ts-ignore
rule.description;
results.push(result);
});
});
}
});
}
}
}
return results.join("\n");
}
Object.defineProperty(lintResults, "toString", { "value": toString });
@ -1445,7 +1450,7 @@ function removeFrontMatter(content, frontMatter) {
*/
function annotateTokens(tokens, lines) {
let trMap = null;
tokens.forEach(function forToken(token) {
for (const token of tokens) {
// Provide missing maps for table content
if (token.type === "tr_open") {
trMap = token.map;
@ -1475,7 +1480,7 @@ function annotateTokens(tokens, lines) {
helpers.forEachInlineCodeSpan(token.content, function handleInlineCodeSpan(code) {
codeSpanExtraLines.push(code.split(helpers.newLineRe).length - 1);
});
(token.children || []).forEach(function forChild(child) {
for (const child of (token.children || [])) {
child.lineNumber = lineNumber;
child.line = lines[lineNumber - 1];
if ((child.type === "softbreak") || (child.type === "hardbreak")) {
@ -1484,9 +1489,9 @@ function annotateTokens(tokens, lines) {
else if (child.type === "code_inline") {
lineNumber += codeSpanExtraLines.shift();
}
});
}
});
}
}
}
/**
* Map rule names/tags to canonical rule name.
@ -1497,24 +1502,24 @@ function annotateTokens(tokens, lines) {
function mapAliasToRuleNames(ruleList) {
const aliasToRuleNames = {};
// const tagToRuleNames = {};
ruleList.forEach(function forRule(rule) {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
// The following is useful for updating README.md:
// console.log(
// "* **[" + ruleName + "](doc/Rules.md#" + ruleName.toLowerCase() +
// ")** *" + rule.names.slice(1).join("/") + "* - " + rule.description);
rule.names.forEach(function forName(name) {
for (const name of rule.names) {
const nameUpper = name.toUpperCase();
aliasToRuleNames[nameUpper] = [ruleName];
});
rule.tags.forEach(function forTag(tag) {
}
for (const tag of rule.tags) {
const tagUpper = tag.toUpperCase();
const ruleNames = aliasToRuleNames[tagUpper] || [];
ruleNames.push(ruleName);
aliasToRuleNames[tagUpper] = ruleNames;
// tagToRuleNames[tag] = ruleName;
});
});
}
}
// The following is useful for updating README.md:
// Object.keys(tagToRuleNames).sort().forEach(function forTag(tag) {
// console.log("* **" + tag + "** - " +
@ -1536,14 +1541,14 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
const defaultKey = Object.keys(config).filter((key) => key.toUpperCase() === "DEFAULT");
const ruleDefault = (defaultKey.length === 0) || !!config[defaultKey[0]];
const effectiveConfig = {};
ruleList.forEach((rule) => {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
effectiveConfig[ruleName] = ruleDefault;
});
deprecatedRuleNames.forEach((ruleName) => {
}
for (const ruleName of deprecatedRuleNames) {
effectiveConfig[ruleName] = false;
});
Object.keys(config).forEach((key) => {
}
for (const key of Object.keys(config)) {
let value = config[key];
if (value) {
if (!(value instanceof Object)) {
@ -1554,10 +1559,10 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
value = false;
}
const keyUpper = key.toUpperCase();
(aliasToRuleNames[keyUpper] || []).forEach((ruleName) => {
for (const ruleName of (aliasToRuleNames[keyUpper] || [])) {
effectiveConfig[ruleName] = value;
});
});
}
}
return effectiveConfig;
}
/**
@ -1615,7 +1620,7 @@ function getEnabledRulesPerLineNumber(ruleList, lines, frontMatterLines, noInlin
// Helper functions
// eslint-disable-next-line jsdoc/require-jsdoc
function handleInlineConfig(input, forEachMatch, forEachLine) {
input.forEach((line, lineIndex) => {
for (const [lineIndex, line] of input.entries()) {
if (!noInlineConfig) {
let match = null;
while ((match = helpers.inlineCommentStartRe.exec(line))) {
@ -1632,7 +1637,7 @@ function getEnabledRulesPerLineNumber(ruleList, lines, frontMatterLines, noInlin
if (forEachLine) {
forEachLine();
}
});
}
}
// eslint-disable-next-line jsdoc/require-jsdoc
function configureFile(action, parameter) {
@ -1649,11 +1654,11 @@ function getEnabledRulesPerLineNumber(ruleList, lines, frontMatterLines, noInlin
const enabled = (action.startsWith("ENABLE"));
const trimmed = parameter && parameter.trim();
const items = trimmed ? trimmed.toUpperCase().split(/\s+/) : allRuleNames;
items.forEach((nameUpper) => {
(aliasToRuleNames[nameUpper] || []).forEach((ruleName) => {
for (const nameUpper of items) {
for (const ruleName of (aliasToRuleNames[nameUpper] || [])) {
state[ruleName] = enabled;
});
});
}
}
return state;
}
// eslint-disable-next-line jsdoc/require-jsdoc
@ -1691,11 +1696,11 @@ function getEnabledRulesPerLineNumber(ruleList, lines, frontMatterLines, noInlin
// Handle inline comments
handleInlineConfig([lines.join("\n")], configureFile);
const effectiveConfig = getEffectiveConfig(ruleList, config, aliasToRuleNames);
ruleList.forEach((rule) => {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
allRuleNames.push(ruleName);
enabledRules[ruleName] = !!effectiveConfig[ruleName];
});
}
capturedRules = enabledRules;
handleInlineConfig(lines, enableDisableFile);
handleInlineConfig(lines, captureRestoreEnableDisable, updateLineState);
@ -2019,10 +2024,10 @@ function lintInput(options, synchronous, callback) {
3 : options.resultVersion;
const md = markdownIt({ "html": true });
const markdownItPlugins = options.markdownItPlugins || [];
markdownItPlugins.forEach(function forPlugin(plugin) {
for (const plugin of markdownItPlugins) {
// @ts-ignore
md.use(...plugin);
});
}
const fs = options.fs || __webpack_require__(/*! fs */ "?ec0a");
const results = newResults(ruleList);
let done = false;
@ -2429,12 +2434,12 @@ module.exports = {
const style = String(params.config.style || "consistent");
let expectedStyle = style;
const nestingStyles = [];
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
if (list.unordered) {
if (expectedStyle === "consistent") {
expectedStyle = unorderedListStyleFor(list.items[0]);
}
list.items.forEach((item) => {
for (const item of list.items) {
const itemStyle = unorderedListStyleFor(item);
if (style === "sublist") {
const nesting = list.nesting;
@ -2463,9 +2468,9 @@ module.exports = {
};
}
addErrorDetailIf(onError, item.lineNumber, expectedStyle, itemStyle, null, null, range, fixInfo);
});
}
});
}
}
}
};
@ -2488,12 +2493,12 @@ module.exports = {
"description": "Inconsistent indentation for list items at the same level",
"tags": ["bullet", "ul", "indentation"],
"function": function MD005(params, onError) {
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
const expectedIndent = list.indent;
let expectedEnd = 0;
let actualEnd = -1;
let endMatching = false;
list.items.forEach((item) => {
for (const item of list.items) {
const { line, lineNumber } = item;
const actualIndent = indentFor(item);
let match = null;
@ -2528,8 +2533,8 @@ module.exports = {
}
}
}
});
});
}
}
}
};
@ -2552,16 +2557,16 @@ module.exports = {
"description": "Consider starting bulleted lists at the beginning of the line",
"tags": ["bullet", "ul", "indentation"],
"function": function MD006(params, onError) {
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
if (list.unordered && !list.nesting && (list.indent !== 0)) {
list.items.forEach((item) => {
for (const item of list.items) {
const { lineNumber, line } = item;
addErrorDetailIf(onError, lineNumber, 0, list.indent, null, null, rangeFromRegExp(line, listItemMarkerRe), {
"deleteCount": line.length - line.trimStart().length
});
});
}
});
}
}
}
};
@ -2587,9 +2592,9 @@ module.exports = {
const indent = Number(params.config.indent || 2);
const startIndented = !!params.config.start_indented;
const startIndent = Number(params.config.start_indent || indent);
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
if (list.unordered && list.parentsUnordered) {
list.items.forEach((item) => {
for (const item of list.items) {
const { lineNumber, line } = item;
const expectedIndent = (startIndented ? startIndent : 0) +
(list.nesting * indent);
@ -2606,9 +2611,9 @@ module.exports = {
"deleteCount": actualIndent,
"insertText": "".padEnd(expectedIndent)
});
});
}
});
}
}
}
};
@ -2879,11 +2884,11 @@ module.exports = {
const linkOnlyLineNumbers = [];
filterTokens(params, "inline", (token) => {
let childTokenTypes = "";
token.children.forEach((child) => {
for (const child of token.children) {
if (child.type !== "text" || child.content !== "") {
childTokenTypes += tokenTypeMap[child.type] || "x";
}
});
}
if (linkOrImageOnlyLineRe.test(childTokenTypes)) {
linkOnlyLineNumbers.push(token.lineNumber);
}
@ -2930,7 +2935,7 @@ module.exports = {
"description": "Dollar signs used before commands without showing output",
"tags": ["code"],
"function": function MD014(params, onError) {
["code_block", "fence"].forEach((type) => {
for (const type of ["code_block", "fence"]) {
filterTokens(params, type, (token) => {
const margin = (token.type === "fence") ? 1 : 0;
const dollarInstances = [];
@ -2951,16 +2956,16 @@ module.exports = {
}
}
if (allDollars) {
dollarInstances.forEach((instance) => {
for (const instance of dollarInstances) {
const [i, lineTrim, column, length] = instance;
addErrorContext(onError, i + 1, lineTrim, null, null, [column, length], {
"editColumn": column,
"deleteCount": length
});
});
}
}
});
});
}
}
};
@ -3362,7 +3367,7 @@ module.exports = {
"function": function MD027(params, onError) {
let blockquoteNesting = 0;
let listItemNesting = 0;
params.tokens.forEach((token) => {
for (const token of params.tokens) {
const { content, lineNumber, type } = token;
if (type === "blockquote_open") {
blockquoteNesting++;
@ -3392,7 +3397,7 @@ module.exports = {
}
}
}
});
}
}
};
@ -3416,7 +3421,7 @@ module.exports = {
"function": function MD028(params, onError) {
let prevToken = {};
let prevLineNumber = null;
params.tokens.forEach(function forToken(token) {
for (const token of params.tokens) {
if ((token.type === "blockquote_open") &&
(prevToken.type === "blockquote_close")) {
for (let lineNumber = prevLineNumber; lineNumber < token.lineNumber; lineNumber++) {
@ -3427,7 +3432,7 @@ module.exports = {
if (token.type === "blockquote_open") {
prevLineNumber = token.map[1] + 1;
}
});
}
}
};
@ -3456,7 +3461,8 @@ module.exports = {
"tags": ["ol"],
"function": function MD029(params, onError) {
const style = String(params.config.style || "one_or_ordered");
flattenedLists().filter((list) => !list.unordered).forEach((list) => {
const filteredLists = flattenedLists().filter((list) => !list.unordered);
for (const list of filteredLists) {
const { items } = list;
let current = 1;
let incrementing = false;
@ -3488,7 +3494,7 @@ module.exports = {
current = 1;
}
// Validate each list item marker
items.forEach((item) => {
for (const item of items) {
const match = orderedListItemMarkerRe.exec(item.line);
if (match) {
addErrorDetailIf(onError, item.lineNumber, String(current), match[1], "Style: " + listStyleExamples[listStyle], null, rangeFromRegExp(item.line, listItemMarkerRe));
@ -3496,8 +3502,8 @@ module.exports = {
current++;
}
}
});
});
}
}
}
};
@ -3524,13 +3530,13 @@ module.exports = {
const olSingle = Number(params.config.ol_single || 1);
const ulMulti = Number(params.config.ul_multi || 1);
const olMulti = Number(params.config.ol_multi || 1);
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
const lineCount = list.lastLineIndex - list.open.map[0];
const allSingle = lineCount === list.items.length;
const expectedSpaces = list.unordered ?
(allSingle ? ulSingle : ulMulti) :
(allSingle ? olSingle : olMulti);
list.items.forEach((item) => {
for (const item of list.items) {
const { line, lineNumber } = item;
const match = /^[\s>]*\S+(\s*)/.exec(line);
const [{ "length": matchLength }, { "length": actualSpaces }] = match;
@ -3545,8 +3551,8 @@ module.exports = {
}
addErrorDetailIf(onError, lineNumber, expectedSpaces, actualSpaces, null, null, [1, matchLength], fixInfo);
}
});
});
}
}
}
};
@ -3611,7 +3617,8 @@ module.exports = {
"tags": ["bullet", "ul", "ol", "blank_lines"],
"function": function MD032(params, onError) {
const { lines } = params;
flattenedLists().filter((list) => !list.nesting).forEach((list) => {
const filteredLists = flattenedLists().filter((list) => !list.nesting);
for (const list of filteredLists) {
const firstIndex = list.open.map[0];
if (!isBlankLine(lines[firstIndex - 1])) {
const line = lines[firstIndex];
@ -3629,7 +3636,7 @@ module.exports = {
"insertText": `${quotePrefix}\n`
});
}
});
}
}
};
@ -3703,7 +3710,7 @@ module.exports = {
"function": function MD034(params, onError) {
filterTokens(params, "inline", (token) => {
let inLink = false;
token.children.forEach((child) => {
for (const child of token.children) {
const { content, line, lineNumber, type } = child;
let match = null;
if (type === "link_open") {
@ -3739,7 +3746,7 @@ module.exports = {
}
}
}
});
}
});
}
};
@ -3837,9 +3844,9 @@ module.exports = {
return base;
}
let state = base;
params.tokens.forEach(function forToken(token) {
for (const token of params.tokens) {
state = state(token);
});
}
}
};
@ -4101,7 +4108,7 @@ module.exports = {
let inLink = false;
let linkText = "";
let lineIndex = 0;
children.forEach((child) => {
for (const child of children) {
const { content, markup, type } = child;
if (type === "link_open") {
inLink = true;
@ -4139,7 +4146,7 @@ module.exports = {
`${markup}${content}${markup}` :
(content || markup);
}
});
}
});
}
};
@ -4240,15 +4247,15 @@ module.exports = {
let inLink = false;
let linkText = "";
let emptyLink = false;
token.children.forEach(function forChild(child) {
for (const child of token.children) {
if (child.type === "link_open") {
inLink = true;
linkText = "";
child.attrs.forEach(function forAttr(attr) {
for (const attr of child.attrs) {
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
emptyLink = true;
}
});
}
}
else if (child.type === "link_close") {
inLink = false;
@ -4267,7 +4274,7 @@ module.exports = {
else if (inLink) {
linkText += child.content;
}
});
}
});
}
};
@ -4293,9 +4300,9 @@ module.exports = {
const requiredHeadings = params.config.headings || params.config.headers;
if (Array.isArray(requiredHeadings)) {
const levels = {};
[1, 2, 3, 4, 5, 6].forEach((level) => {
for (const level of [1, 2, 3, 4, 5, 6]) {
levels["h" + level] = "######".substr(-level);
});
}
let i = 0;
let matchAny = false;
let hasError = false;
@ -4466,15 +4473,14 @@ module.exports = {
"tags": ["code"],
"function": function MD046(params, onError) {
let expectedStyle = String(params.config.style || "consistent");
params.tokens
.filter((token) => token.type === "code_block" || token.type === "fence")
.forEach((token) => {
const codeBlocksAndFences = params.tokens.filter((token) => (token.type === "code_block") || (token.type === "fence"));
for (const token of codeBlocksAndFences) {
const { lineNumber, type } = token;
if (expectedStyle === "consistent") {
expectedStyle = tokenTypeToStyle[type];
}
addErrorDetailIf(onError, lineNumber, expectedStyle, tokenTypeToStyle[type]);
});
}
}
};
@ -4527,15 +4533,14 @@ module.exports = {
"function": function MD048(params, onError) {
const style = String(params.config.style || "consistent");
let expectedStyle = style;
params.tokens
.filter((token) => token.type === "fence")
.forEach((fenceToken) => {
const fenceTokens = params.tokens.filter((token) => token.type === "fence");
for (const fenceToken of fenceTokens) {
const { lineNumber, markup } = fenceToken;
if (expectedStyle === "consistent") {
expectedStyle = fencedCodeBlockStyleFor(markup);
}
addErrorDetailIf(onError, lineNumber, expectedStyle, fencedCodeBlockStyleFor(markup));
});
}
}
};
@ -4825,12 +4830,12 @@ const rules = [
__webpack_require__(/*! ./md052 */ "../lib/md052.js"),
__webpack_require__(/*! ./md053 */ "../lib/md053.js")
];
rules.forEach((rule) => {
for (const rule of rules) {
const name = rule.names[0].toLowerCase();
// eslint-disable-next-line dot-notation
rule["information"] =
new URL(`${homepage}/blob/v${version}/doc/Rules.md#${name}`);
});
}
module.exports = rules;

View file

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"strict": true, /* Enable all strict type-checking options. */
"noUnusedLocals": true, /* Report errors on unused locals. */

View file

@ -328,11 +328,11 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
filterTokens(params, "hr", (token) => {
lineMetadata[token.map[0]][6] = true;
});
params.tokens.filter(isMathBlock).forEach((token) => {
for (const token of params.tokens.filter(isMathBlock)) {
for (let i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][7] = true;
}
});
}
return lineMetadata;
};
@ -345,9 +345,9 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
* @returns {void}
*/
function forEachLine(lineMetadata, handler) {
lineMetadata.forEach(function forMetadata(metadata) {
for (const metadata of lineMetadata) {
handler(...metadata);
});
}
}
module.exports.forEachLine = forEachLine;
@ -359,7 +359,7 @@ module.exports.flattenLists = function flattenLists(tokens) {
let nesting = 0;
const nestingStack = [];
let lastWithMap = { "map": [ 0, 1 ] };
tokens.forEach((token) => {
for (const token of tokens) {
if ((token.type === "bullet_list_open") ||
(token.type === "ordered_list_open")) {
// Save current context and start a new one
@ -398,7 +398,7 @@ module.exports.flattenLists = function flattenLists(tokens) {
// Track last token with map
lastWithMap = token;
}
});
}
return flattenedLists;
};
@ -406,18 +406,18 @@ module.exports.flattenLists = function flattenLists(tokens) {
module.exports.forEachInlineChild =
function forEachInlineChild(params, type, handler) {
filterTokens(params, "inline", function forToken(token) {
token.children.forEach(function forChild(child) {
for (const child of token.children) {
if (child.type === type) {
handler(child, token);
}
});
}
});
};
// Calls the provided function for each heading's content
module.exports.forEachHeading = function forEachHeading(params, handler) {
let heading = null;
params.tokens.forEach(function forToken(token) {
for (const token of params.tokens) {
if (token.type === "heading_open") {
heading = token;
} else if (token.type === "heading_close") {
@ -425,7 +425,7 @@ module.exports.forEachHeading = function forEachHeading(params, handler) {
} else if ((token.type === "inline") && heading) {
handler(heading, token.content, token);
}
});
}
};
/**
@ -767,7 +767,7 @@ function emphasisMarkersInContent(params) {
const { lines } = params;
const byLine = new Array(lines.length);
// Search links
lines.forEach((tokenLine, tokenLineIndex) => {
for (const [ tokenLineIndex, tokenLine ] of lines.entries()) {
const inLine = [];
forEachLink(tokenLine, (index, match) => {
let markerMatch = null;
@ -776,7 +776,7 @@ function emphasisMarkersInContent(params) {
}
});
byLine[tokenLineIndex] = inLine;
});
}
// Search code spans
filterTokens(params, "inline", (token) => {
const { children, lineNumber, map } = token;
@ -786,7 +786,7 @@ function emphasisMarkersInContent(params) {
tokenLines.join("\n"),
(code, lineIndex, column, tickCount) => {
const codeLines = code.split(newLineRe);
codeLines.forEach((codeLine, codeLineIndex) => {
for (const [ codeLineIndex, codeLine ] of codeLines.entries()) {
const byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex;
const inLine = byLine[byLineIndex];
const codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount;
@ -795,7 +795,7 @@ function emphasisMarkersInContent(params) {
inLine.push(codeLineOffset + match.index);
}
byLine[byLineIndex] = inLine;
});
}
}
);
}
@ -952,7 +952,7 @@ function getPreferredLineEnding(input, os) {
let lf = 0;
let crlf = 0;
const endings = input.match(newLineRe) || [];
endings.forEach((ending) => {
for (const ending of endings) {
// eslint-disable-next-line default-case
switch (ending) {
case "\r":
@ -965,7 +965,7 @@ function getPreferredLineEnding(input, os) {
crlf++;
break;
}
});
}
let preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding = (os && os.EOL) || "\n";
@ -1053,8 +1053,10 @@ function applyFixes(input, errors) {
return unique;
});
// Collapse insert/no-delete and no-insert/delete for same line/column
lastFixInfo = {};
fixInfos.forEach((fixInfo) => {
lastFixInfo = {
"lineNumber": -1
};
for (const fixInfo of fixInfos) {
if (
(fixInfo.lineNumber === lastFixInfo.lineNumber) &&
(fixInfo.editColumn === lastFixInfo.editColumn) &&
@ -1066,12 +1068,12 @@ function applyFixes(input, errors) {
lastFixInfo.lineNumber = 0;
}
lastFixInfo = fixInfo;
});
}
fixInfos = fixInfos.filter((fixInfo) => fixInfo.lineNumber);
// Apply all (remaining/updated) fixes
let lastLineIndex = -1;
let lastEditIndex = -1;
fixInfos.forEach((fixInfo) => {
for (const fixInfo of fixInfos) {
const { lineNumber, editColumn, deleteCount } = fixInfo;
const lineIndex = lineNumber - 1;
const editIndex = editColumn - 1;
@ -1085,7 +1087,7 @@ function applyFixes(input, errors) {
}
lastLineIndex = lineIndex;
lastEditIndex = editIndex;
});
}
// Return corrected input
return lines.filter((line) => line !== null).join(lineEnding);
}

View file

@ -29,32 +29,32 @@ function validateRuleList(ruleList, synchronous) {
return result;
}
const allIds = {};
ruleList.forEach(function forRule(rule, index) {
for (const [ index, rule ] of ruleList.entries()) {
const customIndex = index - rules.length;
// eslint-disable-next-line jsdoc/require-jsdoc
// eslint-disable-next-line no-inner-declarations, jsdoc/require-jsdoc
function newError(property) {
return new Error(
"Property '" + property + "' of custom rule at index " +
customIndex + " is incorrect.");
}
[ "names", "tags" ].forEach(function forProperty(property) {
for (const property of [ "names", "tags" ]) {
const value = rule[property];
if (!result &&
(!value || !Array.isArray(value) || (value.length === 0) ||
!value.every(helpers.isString) || value.some(helpers.isEmptyString))) {
result = newError(property);
}
});
[
}
for (const propertyInfo of [
[ "description", "string" ],
[ "function", "function" ]
].forEach(function forProperty(propertyInfo) {
]) {
const property = propertyInfo[0];
const value = rule[property];
if (!result && (!value || (typeof value !== propertyInfo[1]))) {
result = newError(property);
}
});
}
if (
!result &&
rule.information &&
@ -76,24 +76,25 @@ function validateRuleList(ruleList, synchronous) {
);
}
if (!result) {
rule.names.forEach(function forName(name) {
for (const name of rule.names) {
const nameUpper = name.toUpperCase();
if (!result && (allIds[nameUpper] !== undefined)) {
result = new Error("Name '" + name + "' of custom rule at index " +
customIndex + " is already used as a name or tag.");
}
allIds[nameUpper] = true;
});
rule.tags.forEach(function forTag(tag) {
}
for (const tag of rule.tags) {
const tagUpper = tag.toUpperCase();
if (!result && allIds[tagUpper]) {
result = new Error("Tag '" + tag + "' of custom rule at index " +
customIndex + " is already used as a name.");
}
allIds[tagUpper] = false;
});
}
});
}
}
// @ts-ignore
return result;
}
@ -111,10 +112,10 @@ function newResults(ruleList) {
const results = [];
const keys = Object.keys(lintResults);
keys.sort();
keys.forEach(function forFile(file) {
for (const file of keys) {
const fileResults = lintResults[file];
if (Array.isArray(fileResults)) {
fileResults.forEach(function forResult(result) {
for (const result of fileResults) {
const ruleMoniker = result.ruleNames ?
result.ruleNames.join("/") :
(result.ruleName + "/" + result.ruleAlias);
@ -129,30 +130,32 @@ function newResults(ruleList) {
(result.errorContext ?
" [Context: \"" + result.errorContext + "\"]" :
""));
});
}
} else {
if (!ruleNameToRule) {
ruleNameToRule = {};
ruleList.forEach(function forRule(rule) {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
ruleNameToRule[ruleName] = rule;
});
}
Object.keys(fileResults).forEach(function forRule(ruleName) {
}
for (const [ ruleName, ruleResults ] of Object.entries(fileResults)) {
const rule = ruleNameToRule[ruleName.toUpperCase()];
const ruleResults = fileResults[ruleName];
ruleResults.forEach(function forLine(lineNumber) {
for (const lineNumber of ruleResults) {
// @ts-ignore
const nameIndex = Math.min(useAlias ? 1 : 0, rule.names.length - 1);
const result =
file + ": " +
lineNumber + ": " +
// @ts-ignore
rule.names[nameIndex] + " " +
// @ts-ignore
rule.description;
results.push(result);
});
});
}
});
}
}
}
return results.join("\n");
}
Object.defineProperty(lintResults, "toString", { "value": toString });
@ -196,7 +199,7 @@ function removeFrontMatter(content, frontMatter) {
*/
function annotateTokens(tokens, lines) {
let trMap = null;
tokens.forEach(function forToken(token) {
for (const token of tokens) {
// Provide missing maps for table content
if (token.type === "tr_open") {
trMap = token.map;
@ -228,7 +231,7 @@ function annotateTokens(tokens, lines) {
codeSpanExtraLines.push(code.split(helpers.newLineRe).length - 1);
}
);
(token.children || []).forEach(function forChild(child) {
for (const child of (token.children || [])) {
child.lineNumber = lineNumber;
child.line = lines[lineNumber - 1];
if ((child.type === "softbreak") || (child.type === "hardbreak")) {
@ -236,9 +239,9 @@ function annotateTokens(tokens, lines) {
} else if (child.type === "code_inline") {
lineNumber += codeSpanExtraLines.shift();
}
});
}
});
}
}
}
/**
@ -250,24 +253,24 @@ function annotateTokens(tokens, lines) {
function mapAliasToRuleNames(ruleList) {
const aliasToRuleNames = {};
// const tagToRuleNames = {};
ruleList.forEach(function forRule(rule) {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
// The following is useful for updating README.md:
// console.log(
// "* **[" + ruleName + "](doc/Rules.md#" + ruleName.toLowerCase() +
// ")** *" + rule.names.slice(1).join("/") + "* - " + rule.description);
rule.names.forEach(function forName(name) {
for (const name of rule.names) {
const nameUpper = name.toUpperCase();
aliasToRuleNames[nameUpper] = [ ruleName ];
});
rule.tags.forEach(function forTag(tag) {
}
for (const tag of rule.tags) {
const tagUpper = tag.toUpperCase();
const ruleNames = aliasToRuleNames[tagUpper] || [];
ruleNames.push(ruleName);
aliasToRuleNames[tagUpper] = ruleNames;
// tagToRuleNames[tag] = ruleName;
});
});
}
}
// The following is useful for updating README.md:
// Object.keys(tagToRuleNames).sort().forEach(function forTag(tag) {
// console.log("* **" + tag + "** - " +
@ -292,14 +295,14 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
);
const ruleDefault = (defaultKey.length === 0) || !!config[defaultKey[0]];
const effectiveConfig = {};
ruleList.forEach((rule) => {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
effectiveConfig[ruleName] = ruleDefault;
});
deprecatedRuleNames.forEach((ruleName) => {
}
for (const ruleName of deprecatedRuleNames) {
effectiveConfig[ruleName] = false;
});
Object.keys(config).forEach((key) => {
}
for (const key of Object.keys(config)) {
let value = config[key];
if (value) {
if (!(value instanceof Object)) {
@ -309,10 +312,10 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
value = false;
}
const keyUpper = key.toUpperCase();
(aliasToRuleNames[keyUpper] || []).forEach((ruleName) => {
for (const ruleName of (aliasToRuleNames[keyUpper] || [])) {
effectiveConfig[ruleName] = value;
});
});
}
}
return effectiveConfig;
}
@ -378,7 +381,7 @@ function getEnabledRulesPerLineNumber(
// Helper functions
// eslint-disable-next-line jsdoc/require-jsdoc
function handleInlineConfig(input, forEachMatch, forEachLine) {
input.forEach((line, lineIndex) => {
for (const [ lineIndex, line ] of input.entries()) {
if (!noInlineConfig) {
let match = null;
while ((match = helpers.inlineCommentStartRe.exec(line))) {
@ -395,7 +398,7 @@ function getEnabledRulesPerLineNumber(
if (forEachLine) {
forEachLine();
}
});
}
}
// eslint-disable-next-line jsdoc/require-jsdoc
function configureFile(action, parameter) {
@ -417,11 +420,11 @@ function getEnabledRulesPerLineNumber(
const enabled = (action.startsWith("ENABLE"));
const trimmed = parameter && parameter.trim();
const items = trimmed ? trimmed.toUpperCase().split(/\s+/) : allRuleNames;
items.forEach((nameUpper) => {
(aliasToRuleNames[nameUpper] || []).forEach((ruleName) => {
for (const nameUpper of items) {
for (const ruleName of (aliasToRuleNames[nameUpper] || [])) {
state[ruleName] = enabled;
});
});
}
}
return state;
}
// eslint-disable-next-line jsdoc/require-jsdoc
@ -463,11 +466,11 @@ function getEnabledRulesPerLineNumber(
handleInlineConfig([ lines.join("\n") ], configureFile);
const effectiveConfig = getEffectiveConfig(
ruleList, config, aliasToRuleNames);
ruleList.forEach((rule) => {
for (const rule of ruleList) {
const ruleName = rule.names[0].toUpperCase();
allRuleNames.push(ruleName);
enabledRules[ruleName] = !!effectiveConfig[ruleName];
});
}
capturedRules = enabledRules;
handleInlineConfig(lines, enableDisableFile);
handleInlineConfig(lines, captureRestoreEnableDisable, updateLineState);
@ -844,10 +847,10 @@ function lintInput(options, synchronous, callback) {
3 : options.resultVersion;
const md = markdownIt({ "html": true });
const markdownItPlugins = options.markdownItPlugins || [];
markdownItPlugins.forEach(function forPlugin(plugin) {
for (const plugin of markdownItPlugins) {
// @ts-ignore
md.use(...plugin);
});
}
const fs = options.fs || require("fs");
const results = newResults(ruleList);
let done = false;

View file

@ -26,12 +26,12 @@ module.exports = {
const style = String(params.config.style || "consistent");
let expectedStyle = style;
const nestingStyles = [];
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
if (list.unordered) {
if (expectedStyle === "consistent") {
expectedStyle = unorderedListStyleFor(list.items[0]);
}
list.items.forEach((item) => {
for (const item of list.items) {
const itemStyle = unorderedListStyleFor(item);
if (style === "sublist") {
const nesting = list.nesting;
@ -69,8 +69,8 @@ module.exports = {
range,
fixInfo
);
});
}
});
}
}
}
};

View file

@ -11,12 +11,12 @@ module.exports = {
"description": "Inconsistent indentation for list items at the same level",
"tags": [ "bullet", "ul", "indentation" ],
"function": function MD005(params, onError) {
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
const expectedIndent = list.indent;
let expectedEnd = 0;
let actualEnd = -1;
let endMatching = false;
list.items.forEach((item) => {
for (const item of list.items) {
const { line, lineNumber } = item;
const actualIndent = indentFor(item);
let match = null;
@ -63,7 +63,7 @@ module.exports = {
}
}
}
});
});
}
}
}
};

View file

@ -12,9 +12,9 @@ module.exports = {
"Consider starting bulleted lists at the beginning of the line",
"tags": [ "bullet", "ul", "indentation" ],
"function": function MD006(params, onError) {
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
if (list.unordered && !list.nesting && (list.indent !== 0)) {
list.items.forEach((item) => {
for (const item of list.items) {
const { lineNumber, line } = item;
addErrorDetailIf(
onError,
@ -27,8 +27,8 @@ module.exports = {
{
"deleteCount": line.length - line.trimStart().length
});
});
}
});
}
}
}
};

View file

@ -14,9 +14,9 @@ module.exports = {
const indent = Number(params.config.indent || 2);
const startIndented = !!params.config.start_indented;
const startIndent = Number(params.config.start_indent || indent);
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
if (list.unordered && list.parentsUnordered) {
list.items.forEach((item) => {
for (const item of list.items) {
const { lineNumber, line } = item;
const expectedIndent =
(startIndented ? startIndent : 0) +
@ -42,8 +42,8 @@ module.exports = {
"deleteCount": actualIndent,
"insertText": "".padEnd(expectedIndent)
});
});
}
});
}
}
}
};

View file

@ -58,11 +58,11 @@ module.exports = {
const linkOnlyLineNumbers = [];
filterTokens(params, "inline", (token) => {
let childTokenTypes = "";
token.children.forEach((child) => {
for (const child of token.children) {
if (child.type !== "text" || child.content !== "") {
childTokenTypes += tokenTypeMap[child.type] || "x";
}
});
}
if (linkOrImageOnlyLineRe.test(childTokenTypes)) {
linkOnlyLineNumbers.push(token.lineNumber);
}

View file

@ -11,7 +11,7 @@ module.exports = {
"description": "Dollar signs used before commands without showing output",
"tags": [ "code" ],
"function": function MD014(params, onError) {
[ "code_block", "fence" ].forEach((type) => {
for (const type of [ "code_block", "fence" ]) {
filterTokens(params, type, (token) => {
const margin = (token.type === "fence") ? 1 : 0;
const dollarInstances = [];
@ -31,7 +31,7 @@ module.exports = {
}
}
if (allDollars) {
dollarInstances.forEach((instance) => {
for (const instance of dollarInstances) {
const [ i, lineTrim, column, length ] = instance;
addErrorContext(
onError,
@ -45,9 +45,9 @@ module.exports = {
"deleteCount": length
}
);
});
}
}
});
});
}
}
};

View file

@ -13,7 +13,7 @@ module.exports = {
"function": function MD027(params, onError) {
let blockquoteNesting = 0;
let listItemNesting = 0;
params.tokens.forEach((token) => {
for (const token of params.tokens) {
const { content, lineNumber, type } = token;
if (type === "blockquote_open") {
blockquoteNesting++;
@ -51,6 +51,6 @@ module.exports = {
}
}
}
});
}
}
};

View file

@ -11,7 +11,7 @@ module.exports = {
"function": function MD028(params, onError) {
let prevToken = {};
let prevLineNumber = null;
params.tokens.forEach(function forToken(token) {
for (const token of params.tokens) {
if ((token.type === "blockquote_open") &&
(prevToken.type === "blockquote_close")) {
for (
@ -25,6 +25,6 @@ module.exports = {
if (token.type === "blockquote_open") {
prevLineNumber = token.map[1] + 1;
}
});
}
}
};

View file

@ -18,7 +18,8 @@ module.exports = {
"tags": [ "ol" ],
"function": function MD029(params, onError) {
const style = String(params.config.style || "one_or_ordered");
flattenedLists().filter((list) => !list.unordered).forEach((list) => {
const filteredLists = flattenedLists().filter((list) => !list.unordered);
for (const list of filteredLists) {
const { items } = list;
let current = 1;
let incrementing = false;
@ -49,7 +50,7 @@ module.exports = {
current = 1;
}
// Validate each list item marker
items.forEach((item) => {
for (const item of items) {
const match = orderedListItemMarkerRe.exec(item.line);
if (match) {
addErrorDetailIf(onError, item.lineNumber,
@ -60,7 +61,7 @@ module.exports = {
current++;
}
}
});
});
}
}
}
};

View file

@ -14,13 +14,13 @@ module.exports = {
const olSingle = Number(params.config.ol_single || 1);
const ulMulti = Number(params.config.ul_multi || 1);
const olMulti = Number(params.config.ol_multi || 1);
flattenedLists().forEach((list) => {
for (const list of flattenedLists()) {
const lineCount = list.lastLineIndex - list.open.map[0];
const allSingle = lineCount === list.items.length;
const expectedSpaces = list.unordered ?
(allSingle ? ulSingle : ulMulti) :
(allSingle ? olSingle : olMulti);
list.items.forEach((item) => {
for (const item of list.items) {
const { line, lineNumber } = item;
const match = /^[\s>]*\S+(\s*)/.exec(line);
const [ { "length": matchLength }, { "length": actualSpaces } ] = match;
@ -44,7 +44,7 @@ module.exports = {
fixInfo
);
}
});
});
}
}
}
};

View file

@ -13,7 +13,8 @@ module.exports = {
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
"function": function MD032(params, onError) {
const { lines } = params;
flattenedLists().filter((list) => !list.nesting).forEach((list) => {
const filteredLists = flattenedLists().filter((list) => !list.nesting);
for (const list of filteredLists) {
const firstIndex = list.open.map[0];
if (!isBlankLine(lines[firstIndex - 1])) {
const line = lines[firstIndex];
@ -45,6 +46,6 @@ module.exports = {
"insertText": `${quotePrefix}\n`
});
}
});
}
}
};

View file

@ -11,7 +11,7 @@ module.exports = {
"function": function MD034(params, onError) {
filterTokens(params, "inline", (token) => {
let inLink = false;
token.children.forEach((child) => {
for (const child of token.children) {
const { content, line, lineNumber, type } = child;
let match = null;
if (type === "link_open") {
@ -55,7 +55,7 @@ module.exports = {
}
}
}
});
}
});
}
};

View file

@ -49,8 +49,8 @@ module.exports = {
return base;
}
let state = base;
params.tokens.forEach(function forToken(token) {
for (const token of params.tokens) {
state = state(token);
});
}
}
};

View file

@ -18,7 +18,7 @@ module.exports = {
let inLink = false;
let linkText = "";
let lineIndex = 0;
children.forEach((child) => {
for (const child of children) {
const { content, markup, type } = child;
if (type === "link_open") {
inLink = true;
@ -61,7 +61,7 @@ module.exports = {
`${markup}${content}${markup}` :
(content || markup);
}
});
}
});
}
};

View file

@ -14,15 +14,15 @@ module.exports = {
let inLink = false;
let linkText = "";
let emptyLink = false;
token.children.forEach(function forChild(child) {
for (const child of token.children) {
if (child.type === "link_open") {
inLink = true;
linkText = "";
child.attrs.forEach(function forAttr(attr) {
for (const attr of child.attrs) {
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
emptyLink = true;
}
});
}
} else if (child.type === "link_close") {
inLink = false;
if (emptyLink) {
@ -43,7 +43,7 @@ module.exports = {
} else if (inLink) {
linkText += child.content;
}
});
}
});
}
};

View file

@ -13,9 +13,9 @@ module.exports = {
const requiredHeadings = params.config.headings || params.config.headers;
if (Array.isArray(requiredHeadings)) {
const levels = {};
[ 1, 2, 3, 4, 5, 6 ].forEach((level) => {
for (const level of [ 1, 2, 3, 4, 5, 6 ]) {
levels["h" + level] = "######".substr(-level);
});
}
let i = 0;
let matchAny = false;
let hasError = false;

View file

@ -15,9 +15,10 @@ module.exports = {
"tags": [ "code" ],
"function": function MD046(params, onError) {
let expectedStyle = String(params.config.style || "consistent");
params.tokens
.filter((token) => token.type === "code_block" || token.type === "fence")
.forEach((token) => {
const codeBlocksAndFences = params.tokens.filter(
(token) => (token.type === "code_block") || (token.type === "fence")
);
for (const token of codeBlocksAndFences) {
const { lineNumber, type } = token;
if (expectedStyle === "consistent") {
expectedStyle = tokenTypeToStyle[type];
@ -27,6 +28,6 @@ module.exports = {
lineNumber,
expectedStyle,
tokenTypeToStyle[type]);
});
}
}
};

View file

@ -11,9 +11,8 @@ module.exports = {
"function": function MD048(params, onError) {
const style = String(params.config.style || "consistent");
let expectedStyle = style;
params.tokens
.filter((token) => token.type === "fence")
.forEach((fenceToken) => {
const fenceTokens = params.tokens.filter((token) => token.type === "fence");
for (const fenceToken of fenceTokens) {
const { lineNumber, markup } = fenceToken;
if (expectedStyle === "consistent") {
expectedStyle = fencedCodeBlockStyleFor(markup);
@ -24,6 +23,6 @@ module.exports = {
expectedStyle,
fencedCodeBlockStyleFor(markup)
);
});
}
}
};

View file

@ -54,10 +54,10 @@ const rules = [
require("./md052"),
require("./md053")
];
rules.forEach((rule) => {
for (const rule of rules) {
const name = rule.names[0].toLowerCase();
// eslint-disable-next-line dot-notation
rule["information"] =
new URL(`${homepage}/blob/v${version}/doc/Rules.md#${name}`);
});
}
module.exports = rules;

View file

@ -17,7 +17,7 @@
"build-config": "npm run build-config-schema && npm run build-config-example",
"build-config-example": "node schema/build-config-example.js",
"build-config-schema": "node schema/build-config-schema.js",
"build-declaration": "tsc --allowJs --declaration --emitDeclarationOnly --resolveJsonModule lib/markdownlint.js && node scripts delete 'lib/{c,md,r}*.d.ts' 'helpers/*.d.ts'",
"build-declaration": "tsc --allowJs --declaration --emitDeclarationOnly --module commonjs --resolveJsonModule --target es2015 lib/markdownlint.js && node scripts delete 'lib/{c,md,r}*.d.ts' 'helpers/*.d.ts'",
"build-demo": "node scripts copy node_modules/markdown-it/dist/markdown-it.min.js demo/markdown-it.min.js && cd demo && webpack --no-stats",
"build-example": "npm install --no-save --ignore-scripts grunt grunt-cli gulp through2",
"ci": "npm-run-all --continue-on-error --parallel build-config lint serial-declaration-demo test-cover && git diff --exit-code",

View file

@ -41,12 +41,12 @@ const schema = {
const tags = {};
// Add rules
rules.forEach(function forRule(rule) {
rule.tags.forEach(function forTag(tag) {
for (const rule of rules) {
for (const tag of rule.tags) {
const tagRules = tags[tag] || [];
tagRules.push(rule.names[0]);
tags[tag] = tagRules;
});
}
const scheme = {
"description": rule.names.join("/") + " - " + rule.description,
"type": "boolean",
@ -472,22 +472,22 @@ rules.forEach(function forRule(rule) {
scheme.type = [ "boolean", "object" ];
scheme.additionalProperties = false;
}
rule.names.forEach(function forName(name, index) {
for (const [ index, name ] of rule.names.entries()) {
schema.properties[name] = (index === 0) ? scheme : {
"$ref": `#/properties/${rule.names[0]}`
};
});
});
}
}
// Add tags
Object.keys(tags).forEach(function forTag(tag) {
for (const [ tag, tagTags ] of Object.entries(tags)) {
const scheme = {
"description": tag + " - " + tags[tag].join(", "),
"description": tag + " - " + tagTags.join(", "),
"type": "boolean",
"default": true
};
schema.properties[tag] = scheme;
});
}
// Write schema
const schemaFile = path.join(__dirname, "markdownlint-config-schema.json");

View file

@ -350,7 +350,7 @@ test.cb("customRulesNpmPackage", (t) => {
test("customRulesBadProperty", (t) => {
t.plan(27);
[
for (const testCase of [
{
"propertyName": "names",
"propertyValues":
@ -377,9 +377,9 @@ test("customRulesBadProperty", (t) => {
"propertyName": "function",
"propertyValues": [ null, "string", [] ]
}
].forEach(function forTestCase(testCase) {
]) {
const { propertyName, propertyValues } = testCase;
propertyValues.forEach(function forPropertyValue(propertyValue) {
for (const propertyValue of propertyValues) {
const badRule = { ...customRules.anyBlockquote };
badRule[propertyName] = propertyValue;
const options = {
@ -395,8 +395,8 @@ test("customRulesBadProperty", (t) => {
},
"Did not get correct exception for missing property."
);
});
});
}
}
});
test.cb("customRulesUsedNameName", (t) => {
@ -639,7 +639,7 @@ test("customRulesOnErrorNullSync", (t) => {
test("customRulesOnErrorBad", (t) => {
t.plan(21);
[
for (const testCase of [
{
"propertyName": "lineNumber",
"subPropertyName": null,
@ -685,9 +685,9 @@ test("customRulesOnErrorBad", (t) => {
"subPropertyName": "insertText",
"propertyValues": [ 10, [] ]
}
].forEach(function forTestCase(testCase) {
]) {
const { propertyName, subPropertyName, propertyValues } = testCase;
propertyValues.forEach(function forPropertyValue(propertyValue) {
for (const propertyValue of propertyValues) {
const badObject = {
"lineNumber": 1
};
@ -725,13 +725,13 @@ test("customRulesOnErrorBad", (t) => {
},
"Did not get correct exception for bad object."
);
});
});
}
}
});
test("customRulesOnErrorInvalid", (t) => {
t.plan(17);
[
for (const testCase of [
{
"propertyName": "lineNumber",
"subPropertyName": null,
@ -757,9 +757,9 @@ test("customRulesOnErrorInvalid", (t) => {
"subPropertyName": "deleteCount",
"propertyValues": [ -2, 5 ]
}
].forEach(function forTestCase(testCase) {
]) {
const { propertyName, subPropertyName, propertyValues } = testCase;
propertyValues.forEach(function forPropertyValue(propertyValue) {
for (const propertyValue of propertyValues) {
const badObject = {
"lineNumber": 1
};
@ -797,13 +797,13 @@ test("customRulesOnErrorInvalid", (t) => {
},
"Did not get correct exception for invalid object."
);
});
});
}
}
});
test("customRulesOnErrorValid", (t) => {
t.plan(24);
[
for (const testCase of [
{
"propertyName": "lineNumber",
"subPropertyName": null,
@ -835,9 +835,9 @@ test("customRulesOnErrorValid", (t) => {
"propertyValues":
[ "", "1", "123456", "\n", "\nText", "Text\n", "\nText\n" ]
}
].forEach(function forTestCase(testCase) {
]) {
const { propertyName, subPropertyName, propertyValues } = testCase;
propertyValues.forEach(function forPropertyValue(propertyValue) {
for (const propertyValue of propertyValues) {
const goodObject = {
"lineNumber": 1
};
@ -864,8 +864,8 @@ test("customRulesOnErrorValid", (t) => {
};
markdownlint.sync(options);
t.truthy(true);
});
});
}
}
});
test.cb("customRulesOnErrorLazy", (t) => {
@ -1424,7 +1424,7 @@ const stringScenarios = [
]
];
[
for (const flavor of [
[
"customRulesThrowString",
() => {
@ -1437,7 +1437,7 @@ const stringScenarios = [
throw new Error(errorMessage);
}
]
].forEach((flavor) => {
]) {
const [ name, func ] = flavor;
const customRule = [
{
@ -1461,7 +1461,7 @@ const stringScenarios = [
}
]
};
stringScenarios.forEach((inputs) => {
for (const inputs of stringScenarios) {
const [ subname, files, strings ] = inputs;
test.cb(`${name}${subname}UnhandledAsync`, (t) => {
@ -1530,10 +1530,10 @@ const stringScenarios = [
});
t.deepEqual(actualResult, expectedResult, "Undetected issues.");
});
});
});
}
}
[
for (const flavor of [
[
"customRulesAsyncExceptionString",
() => {
@ -1570,7 +1570,7 @@ const stringScenarios = [
"customRulesAsyncRejectError",
() => Promise.reject(new Error(errorMessage))
]
].forEach((flavor) => {
]) {
const [ name, func ] = flavor;
const customRule = {
"names": [ "name" ],
@ -1579,7 +1579,7 @@ const stringScenarios = [
"asynchronous": true,
"function": func
};
stringScenarios.forEach((inputs) => {
for (const inputs of stringScenarios) {
const [ subname, files, strings ] = inputs;
test.cb(`${name}${subname}Unhandled`, (t) => {
@ -1630,5 +1630,5 @@ const stringScenarios = [
t.end();
});
});
});
});
}
}

View file

@ -8,8 +8,10 @@ const test = require("ava").default;
const markdownlint = require("../lib/markdownlint");
// Simulates typing each test file to validate handling of partial input
const files = fs.readdirSync("./test");
files.filter((file) => /\.md$/.test(file)).forEach((file) => {
const files = fs
.readdirSync("./test")
.filter((file) => /\.md$/.test(file));
for (const file of files) {
const strings = {};
let content = fs.readFileSync(path.join("./test", file), "utf8");
while (content) {
@ -25,4 +27,4 @@ files.filter((file) => /\.md$/.test(file)).forEach((file) => {
});
t.pass();
});
});
}

View file

@ -219,11 +219,11 @@ bar`
"_"
]
];
testCases.forEach(function forTestCase(testCase) {
for (const testCase of testCases) {
const [ markdown, expected, replacement ] = testCase;
const actual = helpers.unescapeMarkdown(markdown, replacement);
t.is(actual, expected);
});
}
});
test("isBlankLine", (t) => {
@ -253,7 +253,9 @@ test("isBlankLine", (t) => {
"text --> <!--text--> <!--text--> <!-- text",
"text --> --> <!--text--> <!--text--> <!-- <!-- text"
];
blankLines.forEach((line) => t.true(helpers.isBlankLine(line), line || ""));
for (const line of blankLines) {
t.true(helpers.isBlankLine(line), line || "");
}
const nonBlankLines = [
"text",
" text ",
@ -266,7 +268,9 @@ test("isBlankLine", (t) => {
"text --> <!--text--> text <!--text--> <!-- text",
"text --> --> <!--text--> text <!--text--> <!-- <!-- text"
];
nonBlankLines.forEach((line) => t.true(!helpers.isBlankLine(line), line));
for (const line of nonBlankLines) {
t.true(!helpers.isBlankLine(line), line);
}
});
test("includesSorted", (t) => {
@ -280,11 +284,11 @@ test("includesSorted", (t) => {
[ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
];
inputs.forEach((input) => {
for (const input of inputs) {
for (let i = 0; i <= 21; i++) {
t.is(helpers.includesSorted(input, i), input.includes(i));
}
});
}
});
test("forEachInlineCodeSpan", (t) => {
@ -377,7 +381,7 @@ test("forEachInlineCodeSpan", (t) => {
"expecteds": [ [ "code", 1, 1, 1 ] ]
}
];
testCases.forEach((testCase) => {
for (const testCase of testCases) {
const { input, expecteds } = testCase;
helpers.forEachInlineCodeSpan(input, (code, line, column, ticks) => {
const [ expectedCode, expectedLine, expectedColumn, expectedTicks ] =
@ -388,7 +392,7 @@ test("forEachInlineCodeSpan", (t) => {
t.is(ticks, expectedTicks, input);
});
t.is(expecteds.length, 0, "length");
});
}
});
test("getPreferredLineEnding", (t) => {
@ -412,11 +416,11 @@ test("getPreferredLineEnding", (t) => {
[ "t\nt\r\nt\nt", "\n" ],
[ "t\rt\t\rt", "\r" ]
];
testCases.forEach((testCase) => {
for (const testCase of testCases) {
const [ input, expected ] = testCase;
const actual = helpers.getPreferredLineEnding(input);
t.is(actual, expected, "Incorrect line ending returned.");
});
}
t.is(helpers.getPreferredLineEnding("", null), "\n");
t.is(helpers.getPreferredLineEnding("", { "EOL": "\n" }), "\n");
t.is(helpers.getPreferredLineEnding("", { "EOL": "\r\n" }), "\r\n");
@ -463,12 +467,12 @@ test("applyFix", (t) => {
"Hello world.\r\n"
]
];
testCases.forEach((testCase) => {
for (const testCase of testCases) {
const [ line, fixInfo, lineEnding, expected ] = testCase;
// @ts-ignore
const actual = helpers.applyFix(line, fixInfo, lineEnding);
t.is(actual, expected, "Incorrect fix applied.");
});
}
});
test("applyFixes", (t) => {
@ -941,12 +945,12 @@ test("applyFixes", (t) => {
"Hello world"
]
];
testCases.forEach((testCase) => {
for (const testCase of testCases) {
const [ input, errors, expected ] = testCase;
// @ts-ignore
const actual = helpers.applyFixes(input, errors);
t.is(actual, expected, "Incorrect fix applied.");
});
}
});
test("deepFreeze", (t) => {
@ -962,7 +966,7 @@ test("deepFreeze", (t) => {
}
};
t.is(helpers.deepFreeze(obj), obj, "Did not return object.");
[
for (const scenario of [
() => {
obj.prop = false;
},
@ -978,9 +982,9 @@ test("deepFreeze", (t) => {
() => {
obj.sub.sub.prop = "zero";
}
].forEach((scenario) => {
]) {
t.throws(scenario, null, "Assigned to frozen object.");
});
}
});
test("forEachLink", (t) => {

View file

@ -36,18 +36,18 @@ function createTestForFile(file) {
.then((params) => {
const [ config, content, results ] = params;
// Canonicalize version number
const errors = results[file];
errors
.filter((error) => !!error.ruleInformation)
.forEach((error) => {
const errors = results[file]
.filter((error) => !!error.ruleInformation);
for (const error of errors) {
error.ruleInformation =
error.ruleInformation.replace(/v\d+\.\d+\.\d+/, "v0.0.0");
});
}
// Match identified issues by MD### markers
const marker = /\{(MD\d+)(?::(\d+))?\}/g;
const lines = content.split(helpers.newLineRe);
const expected = {};
lines.forEach((line, index) => {
// @ts-ignore
for (const [ index, line ] of lines.entries()) {
let match = null;
while ((match = marker.exec(line))) {
const rule = match[1];
@ -55,17 +55,17 @@ function createTestForFile(file) {
const indices = expected[rule] = expected[rule] || [];
indices.push(match[2] ? Number.parseInt(match[2], 10) : index + 1);
}
});
}
if (Object.keys(expected).length > 0) {
const actual = {};
errors.forEach((error) => {
for (const error of errors) {
const rule = error.ruleNames[0];
// eslint-disable-next-line no-multi-assign
const indices = actual[rule] = actual[rule] || [];
if (indices[indices.length - 1] !== error.lineNumber) {
indices.push(error.lineNumber);
}
});
}
t.deepEqual(actual, expected, "Too few or too many issues found.");
}
// Create snapshot
@ -91,10 +91,13 @@ function createTestForFile(file) {
};
}
require("fs").readdirSync("./test")
.filter((file) => /\.md$/.test(file))
const files = require("fs")
.readdirSync("./test")
.filter((file) => /\.md$/.test(file));
for (const file of files) {
// @ts-ignore
.forEach((file) => test(
test(
file,
createTestForFile(path.join("./test", file))
));
);
}

View file

@ -433,9 +433,9 @@ test.cb("styleFiles", (t) => {
t.plan(4);
fs.readdir("./style", function readdir(err, files) {
t.falsy(err);
files.forEach(function forFile(file) {
for (const file of files) {
t.truthy(require(path.join("../style", file)), "Unable to load/parse.");
});
}
t.end();
});
});
@ -847,13 +847,13 @@ test.cb("customFileSystemAsync", (t) => {
test.cb("readme", (t) => {
t.plan(125);
const tagToRules = {};
rules.forEach(function forRule(rule) {
rule.tags.forEach(function forTag(tag) {
for (const rule of rules) {
for (const tag of rule.tags) {
const tagRules = tagToRules[tag] || [];
tagRules.push(rule.names[0]);
tagToRules[tag] = tagRules;
});
});
}
}
fs.readFile("README.md", "utf8",
function readFile(err, contents) {
t.falsy(err);
@ -864,7 +864,7 @@ test.cb("readme", (t) => {
let seenTags = false;
let inTags = false;
// @ts-ignore
md.parse(contents, {}).forEach(function forToken(token) {
for (const token of md.parse(contents, {})) {
if (
(token.type === "bullet_list_open") &&
(token.level === 0)
@ -909,7 +909,7 @@ test.cb("readme", (t) => {
delete tagToRules[tag];
}
}
});
}
const ruleLeft = rulesLeft.shift();
t.true(!ruleLeft,
"Missing rule documentation for " +
@ -943,7 +943,7 @@ test.cb("rules", (t) => {
"Missing parameters for rule " + r.names + ".");
};
// @ts-ignore
md.parse(contents, {}).forEach(function forToken(token) {
for (const token of md.parse(contents, {})) {
if ((token.type === "heading_open") && (token.tag === "h2")) {
inHeading = true;
} else if (token.type === "heading_close") {
@ -995,7 +995,7 @@ test.cb("rules", (t) => {
ruleUsesParams = null;
}
}
});
}
const ruleLeft = rulesLeft.shift();
t.true(!ruleLeft,
"Missing rule documentation for " +
@ -1013,13 +1013,15 @@ test("validateJsonUsingConfigSchemaStrict", (t) => {
const jsConfigFileRe = /^jsconfig\.json$/i;
const wrongTypesFileRe = /wrong-types-in-config-file.json$/i;
const testDirectory = __dirname;
const testFiles = fs.readdirSync(testDirectory);
testFiles.filter(function filterFile(file) {
const testFiles = fs
.readdirSync(testDirectory)
.filter(function filterFile(file) {
return jsonFileRe.test(file) &&
!resultsFileRe.test(file) &&
!jsConfigFileRe.test(file) &&
!wrongTypesFileRe.test(file);
}).forEach(function forFile(file) {
});
for (const file of testFiles) {
const data = fs.readFileSync(
path.join(testDirectory, file),
"utf8"
@ -1028,7 +1030,7 @@ test("validateJsonUsingConfigSchemaStrict", (t) => {
// @ts-ignore
tv4.validate(JSON.parse(data), configSchemaStrict),
file + "\n" + JSON.stringify(tv4.error, null, 2));
});
}
});
test("validateConfigSchemaAllowsUnknownProperties", (t) => {
@ -1043,7 +1045,7 @@ test("validateConfigSchemaAllowsUnknownProperties", (t) => {
}
}
];
testCases.forEach((testCase) => {
for (const testCase of testCases) {
t.true(
// @ts-ignore
tv4.validate(testCase, configSchema),
@ -1052,7 +1054,7 @@ test("validateConfigSchemaAllowsUnknownProperties", (t) => {
// @ts-ignore
tv4.validate(testCase, configSchemaStrict),
"Unknown property allowed when strict: " + JSON.stringify(testCase));
});
}
});
test("validateConfigSchemaAppliesToUnknownProperties", (t) => {
@ -1099,20 +1101,23 @@ test("validateConfigExampleJson", async(t) => {
test("allBuiltInRulesHaveValidUrl", (t) => {
t.plan(147);
rules.forEach(function forRule(rule) {
for (const rule of rules) {
// @ts-ignore
t.truthy(rule.information);
// @ts-ignore
t.true(Object.getPrototypeOf(rule.information) === URL.prototype);
const name = rule.names[0].toLowerCase();
t.is(
// @ts-ignore
rule.information.href,
`${homepage}/blob/v${version}/doc/Rules.md#${name}`
);
});
}
});
test("someCustomRulesHaveValidUrl", (t) => {
t.plan(8);
customRules.all.forEach(function forRule(rule) {
for (const rule of customRules.all) {
t.true(!rule.information ||
(Object.getPrototypeOf(rule.information) === URL.prototype));
if (rule === customRules.anyBlockquote) {
@ -1126,7 +1131,7 @@ test("someCustomRulesHaveValidUrl", (t) => {
`${homepage}/blob/main/test/rules/letters-E-X.js`
);
}
});
}
});
test.cb("markdownItPluginsSingle", (t) => {

View file

@ -11,12 +11,12 @@ module.exports = {
),
"tags": [ "test" ],
"function": function rule(params, onError) {
params.tokens.filter(function filterToken(token) {
for (const inline of params.tokens.filter(function filterToken(token) {
return token.type === "inline";
}).forEach(function forToken(inline) {
inline.children.filter(function filterChild(child) {
})) {
for (const text of inline.children.filter(function filterChild(child) {
return child.type === "text";
}).forEach(function forChild(text) {
})) {
const index = text.content.toLowerCase().indexOf("ex");
if (index !== -1) {
onError({
@ -24,7 +24,7 @@ module.exports = {
"context": text.content.substr(index - 1, 4)
});
}
});
});
}
}
}
};

View file

@ -36,14 +36,14 @@ module.exports = {
.then((config) => {
config = cleanJsdocRulesFromEslintConfig(config);
const results = linter.verify(fence.content, config);
results.forEach((result) => {
for (const result of results) {
const lineNumber = fence.lineNumber + result.line;
onError({
"lineNumber": lineNumber,
"detail": result.message,
"context": params.lines[lineNumber - 1]
});
});
}
});
}
return Promise.resolve();

View file

@ -7,13 +7,13 @@ module.exports = {
"description": "Sample rule",
"tags": [ "sample" ],
"function": function rule(params, onError) {
params.tokens.forEach((token) => {
for (const token of params.tokens) {
if (token.type === "hr") {
onError({
"lineNumber": token.lineNumber,
"detail": "Sample error for hr"
});
}
});
}
}
};