mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Convert var to const/let (except in browser-only code).
This commit is contained in:
parent
78c1af7bfd
commit
213aef4564
56 changed files with 524 additions and 518 deletions
|
|
@ -2,29 +2,29 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var md = require("markdown-it")({ "html": true });
|
||||
var rules = require("./rules");
|
||||
var shared = require("./shared");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const md = require("markdown-it")({ "html": true });
|
||||
const rules = require("./rules");
|
||||
const shared = require("./shared");
|
||||
|
||||
// Validates the list of rules for structure and reuse
|
||||
function validateRuleList(ruleList) {
|
||||
var result = null;
|
||||
let result = null;
|
||||
if (ruleList.length === rules.length) {
|
||||
// No need to validate if only using built-in rules
|
||||
return result;
|
||||
}
|
||||
var allIds = {};
|
||||
const allIds = {};
|
||||
ruleList.forEach(function forRule(rule, index) {
|
||||
var customIndex = index - rules.length;
|
||||
const customIndex = index - rules.length;
|
||||
function newError(property) {
|
||||
return new Error(
|
||||
"Property '" + property + "' of custom rule at index " +
|
||||
customIndex + " is incorrect.");
|
||||
}
|
||||
[ "names", "tags" ].forEach(function forProperty(property) {
|
||||
var value = rule[property];
|
||||
const value = rule[property];
|
||||
if (!result &&
|
||||
(!value || !Array.isArray(value) || (value.length === 0) ||
|
||||
!value.every(shared.isString) || value.some(shared.isEmptyString))) {
|
||||
|
|
@ -35,15 +35,15 @@ function validateRuleList(ruleList) {
|
|||
[ "description", "string" ],
|
||||
[ "function", "function" ]
|
||||
].forEach(function forProperty(propertyInfo) {
|
||||
var property = propertyInfo[0];
|
||||
var value = rule[property];
|
||||
const property = propertyInfo[0];
|
||||
const value = rule[property];
|
||||
if (!result && (!value || (typeof value !== propertyInfo[1]))) {
|
||||
result = newError(property);
|
||||
}
|
||||
});
|
||||
if (!result) {
|
||||
rule.names.forEach(function forName(name) {
|
||||
var nameUpper = name.toUpperCase();
|
||||
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.");
|
||||
|
|
@ -51,7 +51,7 @@ function validateRuleList(ruleList) {
|
|||
allIds[nameUpper] = true;
|
||||
});
|
||||
rule.tags.forEach(function forTag(tag) {
|
||||
var tagUpper = tag.toUpperCase();
|
||||
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.");
|
||||
|
|
@ -67,14 +67,14 @@ function validateRuleList(ruleList) {
|
|||
function newResults(ruleList) {
|
||||
function Results() {}
|
||||
Results.prototype.toString = function resultsToString(useAlias) {
|
||||
var that = this;
|
||||
var ruleNameToRule = null;
|
||||
var results = [];
|
||||
const that = this;
|
||||
let ruleNameToRule = null;
|
||||
const results = [];
|
||||
Object.keys(that).forEach(function forFile(file) {
|
||||
var fileResults = that[file];
|
||||
const fileResults = that[file];
|
||||
if (Array.isArray(fileResults)) {
|
||||
fileResults.forEach(function forResult(result) {
|
||||
var ruleMoniker = result.ruleNames ?
|
||||
const ruleMoniker = result.ruleNames ?
|
||||
result.ruleNames.join("/") :
|
||||
(result.ruleName + "/" + result.ruleAlias);
|
||||
results.push(
|
||||
|
|
@ -93,16 +93,16 @@ function newResults(ruleList) {
|
|||
if (!ruleNameToRule) {
|
||||
ruleNameToRule = {};
|
||||
ruleList.forEach(function forRule(rule) {
|
||||
var ruleName = rule.names[0].toUpperCase();
|
||||
const ruleName = rule.names[0].toUpperCase();
|
||||
ruleNameToRule[ruleName] = rule;
|
||||
});
|
||||
}
|
||||
Object.keys(fileResults).forEach(function forRule(ruleName) {
|
||||
var rule = ruleNameToRule[ruleName.toUpperCase()];
|
||||
var ruleResults = fileResults[ruleName];
|
||||
const rule = ruleNameToRule[ruleName.toUpperCase()];
|
||||
const ruleResults = fileResults[ruleName];
|
||||
ruleResults.forEach(function forLine(lineNumber) {
|
||||
var nameIndex = Math.min(useAlias ? 1 : 0, rule.names.length - 1);
|
||||
var result =
|
||||
const nameIndex = Math.min(useAlias ? 1 : 0, rule.names.length - 1);
|
||||
const result =
|
||||
file + ": " +
|
||||
lineNumber + ": " +
|
||||
rule.names[nameIndex] + " " +
|
||||
|
|
@ -119,11 +119,11 @@ function newResults(ruleList) {
|
|||
|
||||
// Remove front matter (if present at beginning of content)
|
||||
function removeFrontMatter(content, frontMatter) {
|
||||
var frontMatterLines = [];
|
||||
let frontMatterLines = [];
|
||||
if (frontMatter) {
|
||||
var frontMatterMatch = content.match(frontMatter);
|
||||
const frontMatterMatch = content.match(frontMatter);
|
||||
if (frontMatterMatch && !frontMatterMatch.index) {
|
||||
var contentMatched = frontMatterMatch[0];
|
||||
const contentMatched = frontMatterMatch[0];
|
||||
content = content.slice(contentMatched.length);
|
||||
frontMatterLines = contentMatched.split(shared.newLineRe);
|
||||
if (frontMatterLines.length &&
|
||||
|
|
@ -140,7 +140,7 @@ function removeFrontMatter(content, frontMatter) {
|
|||
|
||||
// Annotate tokens with line/lineNumber
|
||||
function annotateTokens(tokens, lines) {
|
||||
var tbodyMap = null;
|
||||
let tbodyMap = null;
|
||||
tokens.forEach(function forToken(token) {
|
||||
// Handle missing maps for table body
|
||||
if (token.type === "tbody_open") {
|
||||
|
|
@ -162,7 +162,7 @@ function annotateTokens(tokens, lines) {
|
|||
token.map[1]--;
|
||||
}
|
||||
// Annotate children with lineNumber
|
||||
var lineNumber = token.lineNumber;
|
||||
let lineNumber = token.lineNumber;
|
||||
(token.children || []).forEach(function forChild(child) {
|
||||
child.lineNumber = lineNumber;
|
||||
child.line = lines[lineNumber - 1];
|
||||
|
|
@ -176,21 +176,21 @@ function annotateTokens(tokens, lines) {
|
|||
|
||||
// Map rule names/tags to canonical rule name
|
||||
function mapAliasToRuleNames(ruleList) {
|
||||
var aliasToRuleNames = {};
|
||||
// var tagToRuleNames = {};
|
||||
const aliasToRuleNames = {};
|
||||
// const tagToRuleNames = {};
|
||||
ruleList.forEach(function forRule(rule) {
|
||||
var ruleName = rule.names[0].toUpperCase();
|
||||
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) {
|
||||
var nameUpper = name.toUpperCase();
|
||||
const nameUpper = name.toUpperCase();
|
||||
aliasToRuleNames[nameUpper] = [ ruleName ];
|
||||
});
|
||||
rule.tags.forEach(function forTag(tag) {
|
||||
var tagUpper = tag.toUpperCase();
|
||||
var ruleNames = aliasToRuleNames[tagUpper] || [];
|
||||
const tagUpper = tag.toUpperCase();
|
||||
const ruleNames = aliasToRuleNames[tagUpper] || [];
|
||||
ruleNames.push(ruleName);
|
||||
aliasToRuleNames[tagUpper] = ruleNames;
|
||||
// tagToRuleNames[tag] = ruleName;
|
||||
|
|
@ -206,17 +206,17 @@ function mapAliasToRuleNames(ruleList) {
|
|||
|
||||
// Apply (and normalize) config
|
||||
function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
|
||||
var defaultKey = Object.keys(config).filter(function forKey(key) {
|
||||
const defaultKey = Object.keys(config).filter(function forKey(key) {
|
||||
return key.toUpperCase() === "DEFAULT";
|
||||
});
|
||||
var ruleDefault = (defaultKey.length === 0) || !!config[defaultKey[0]];
|
||||
var effectiveConfig = {};
|
||||
const ruleDefault = (defaultKey.length === 0) || !!config[defaultKey[0]];
|
||||
const effectiveConfig = {};
|
||||
ruleList.forEach(function forRule(rule) {
|
||||
var ruleName = rule.names[0].toUpperCase();
|
||||
const ruleName = rule.names[0].toUpperCase();
|
||||
effectiveConfig[ruleName] = ruleDefault;
|
||||
});
|
||||
Object.keys(config).forEach(function forKey(key) {
|
||||
var value = config[key];
|
||||
let value = config[key];
|
||||
if (value) {
|
||||
if (!(value instanceof Object)) {
|
||||
value = {};
|
||||
|
|
@ -224,7 +224,7 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
|
|||
} else {
|
||||
value = false;
|
||||
}
|
||||
var keyUpper = key.toUpperCase();
|
||||
const keyUpper = key.toUpperCase();
|
||||
(aliasToRuleNames[keyUpper] || []).forEach(function forRule(ruleName) {
|
||||
effectiveConfig[ruleName] = value;
|
||||
});
|
||||
|
|
@ -236,16 +236,16 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
|
|||
function getEnabledRulesPerLineNumber(
|
||||
ruleList, lines, frontMatterLines, noInlineConfig,
|
||||
effectiveConfig, aliasToRuleNames) {
|
||||
var enabledRules = {};
|
||||
var allRuleNames = [];
|
||||
let enabledRules = {};
|
||||
const allRuleNames = [];
|
||||
ruleList.forEach(function forRule(rule) {
|
||||
var ruleName = rule.names[0].toUpperCase();
|
||||
const ruleName = rule.names[0].toUpperCase();
|
||||
allRuleNames.push(ruleName);
|
||||
enabledRules[ruleName] = !!effectiveConfig[ruleName];
|
||||
});
|
||||
function forMatch(match) {
|
||||
var enabled = match[1].toUpperCase() === "EN";
|
||||
var items = match[2] ?
|
||||
const enabled = match[1].toUpperCase() === "EN";
|
||||
const items = match[2] ?
|
||||
match[2].trim().toUpperCase().split(/\s+/) :
|
||||
allRuleNames;
|
||||
items.forEach(function forItem(nameUpper) {
|
||||
|
|
@ -254,10 +254,10 @@ function getEnabledRulesPerLineNumber(
|
|||
});
|
||||
});
|
||||
}
|
||||
var enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
|
||||
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
|
||||
lines.forEach(function forLine(line) {
|
||||
if (!noInlineConfig) {
|
||||
var match = shared.inlineCommentRe.exec(line);
|
||||
let match = shared.inlineCommentRe.exec(line);
|
||||
if (match) {
|
||||
enabledRules = shared.clone(enabledRules);
|
||||
while (match) {
|
||||
|
|
@ -288,38 +288,39 @@ function lintContent(
|
|||
// Remove UTF-8 byte order marker (if present)
|
||||
content = content.replace(/^\ufeff/, "");
|
||||
// Remove front matter
|
||||
var removeFrontMatterResult = removeFrontMatter(content, frontMatter);
|
||||
var frontMatterLines = removeFrontMatterResult.frontMatterLines;
|
||||
const removeFrontMatterResult = removeFrontMatter(content, frontMatter);
|
||||
const frontMatterLines = removeFrontMatterResult.frontMatterLines;
|
||||
// Ignore the content of HTML comments
|
||||
content = shared.clearHtmlCommentText(removeFrontMatterResult.content);
|
||||
// Parse content into tokens and lines
|
||||
var tokens = md.parse(content, {});
|
||||
var lines = content.split(shared.newLineRe);
|
||||
const tokens = md.parse(content, {});
|
||||
const lines = content.split(shared.newLineRe);
|
||||
annotateTokens(tokens, lines);
|
||||
var aliasToRuleNames = mapAliasToRuleNames(ruleList);
|
||||
var effectiveConfig = getEffectiveConfig(ruleList, config, aliasToRuleNames);
|
||||
var enabledRulesPerLineNumber = getEnabledRulesPerLineNumber(
|
||||
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
|
||||
const effectiveConfig =
|
||||
getEffectiveConfig(ruleList, config, aliasToRuleNames);
|
||||
const enabledRulesPerLineNumber = getEnabledRulesPerLineNumber(
|
||||
ruleList, lines, frontMatterLines, noInlineConfig,
|
||||
effectiveConfig, aliasToRuleNames);
|
||||
// Create parameters for rules
|
||||
var params = {
|
||||
const params = {
|
||||
"tokens": tokens,
|
||||
"lines": lines,
|
||||
"frontMatterLines": frontMatterLines
|
||||
};
|
||||
shared.makeTokenCache(params);
|
||||
// Function to run for each rule
|
||||
var result = (resultVersion === 0) ? {} : [];
|
||||
const result = (resultVersion === 0) ? {} : [];
|
||||
function forRule(rule) {
|
||||
// Configure rule
|
||||
var ruleNameFriendly = rule.names[0];
|
||||
var ruleName = ruleNameFriendly.toUpperCase();
|
||||
const ruleNameFriendly = rule.names[0];
|
||||
const ruleName = ruleNameFriendly.toUpperCase();
|
||||
params.config = effectiveConfig[ruleName];
|
||||
function throwError(property) {
|
||||
throw new Error(
|
||||
"Property '" + property + "' of onError parameter is incorrect.");
|
||||
}
|
||||
var errors = [];
|
||||
const errors = [];
|
||||
function onError(errorInfo) {
|
||||
if (!errorInfo ||
|
||||
!errorInfo.lineNumber ||
|
||||
|
|
@ -353,7 +354,7 @@ function lintContent(
|
|||
// Record any errors (significant performance benefit from length check)
|
||||
if (errors.length) {
|
||||
errors.sort(lineNumberComparison);
|
||||
var filteredErrors = errors
|
||||
const filteredErrors = errors
|
||||
.filter(uniqueFilterForSortedErrors)
|
||||
.filter(function removeDisabledRules(error) {
|
||||
return enabledRulesPerLineNumber[error.lineNumber][ruleName];
|
||||
|
|
@ -362,7 +363,7 @@ function lintContent(
|
|||
if (resultVersion === 0) {
|
||||
return error.lineNumber;
|
||||
}
|
||||
var errorObject = {};
|
||||
const errorObject = {};
|
||||
errorObject.lineNumber = error.lineNumber;
|
||||
if (resultVersion === 1) {
|
||||
errorObject.ruleName = ruleNameFriendly;
|
||||
|
|
@ -425,30 +426,30 @@ function lintInput(options, synchronous, callback) {
|
|||
// Normalize inputs
|
||||
options = options || {};
|
||||
callback = callback || function noop() {};
|
||||
var ruleList = rules.concat(options.customRules || []);
|
||||
var ruleErr = validateRuleList(ruleList);
|
||||
const ruleList = rules.concat(options.customRules || []);
|
||||
const ruleErr = validateRuleList(ruleList);
|
||||
if (ruleErr) {
|
||||
return callback(ruleErr);
|
||||
}
|
||||
var files = [];
|
||||
let files = [];
|
||||
if (Array.isArray(options.files)) {
|
||||
files = options.files.slice();
|
||||
} else if (options.files) {
|
||||
files = [ String(options.files) ];
|
||||
}
|
||||
var strings = options.strings || {};
|
||||
var stringsKeys = Object.keys(strings);
|
||||
var config = options.config || { "default": true };
|
||||
var frontMatter = (options.frontMatter === undefined) ?
|
||||
const strings = options.strings || {};
|
||||
const stringsKeys = Object.keys(strings);
|
||||
const config = options.config || { "default": true };
|
||||
const frontMatter = (options.frontMatter === undefined) ?
|
||||
shared.frontMatterRe : options.frontMatter;
|
||||
var noInlineConfig = !!options.noInlineConfig;
|
||||
var resultVersion = (options.resultVersion === undefined) ?
|
||||
const noInlineConfig = !!options.noInlineConfig;
|
||||
const resultVersion = (options.resultVersion === undefined) ?
|
||||
2 : options.resultVersion;
|
||||
var results = newResults(ruleList);
|
||||
const results = newResults(ruleList);
|
||||
// Helper to lint the next string or file
|
||||
function lintNextItem() {
|
||||
var iterating = true;
|
||||
var item = null;
|
||||
let iterating = true;
|
||||
let item = null;
|
||||
function lintNextItemCallback(err, result) {
|
||||
if (err) {
|
||||
iterating = false;
|
||||
|
|
@ -506,7 +507,7 @@ function markdownlint(options, callback) {
|
|||
* @returns {Object} Result object.
|
||||
*/
|
||||
function markdownlintSync(options) {
|
||||
var results = null;
|
||||
let results = null;
|
||||
lintInput(options, true, function callback(error, res) {
|
||||
if (error) {
|
||||
throw error;
|
||||
|
|
@ -530,7 +531,7 @@ function readConfig(file, callback) {
|
|||
return callback(err);
|
||||
}
|
||||
// Parse file
|
||||
var config = null;
|
||||
let config = null;
|
||||
try {
|
||||
config = JSON.parse(content);
|
||||
} catch (ex) {
|
||||
|
|
@ -538,7 +539,7 @@ function readConfig(file, callback) {
|
|||
}
|
||||
if (config.extends) {
|
||||
// Extend configuration
|
||||
var extendsFile = path.resolve(path.dirname(file), config.extends);
|
||||
const extendsFile = path.resolve(path.dirname(file), config.extends);
|
||||
readConfig(extendsFile, function handleConfig(errr, extendsConfig) {
|
||||
if (errr) {
|
||||
return callback(errr);
|
||||
|
|
@ -560,7 +561,7 @@ function readConfig(file, callback) {
|
|||
*/
|
||||
function readConfigSync(file) {
|
||||
// Parse file
|
||||
var config = JSON.parse(fs.readFileSync(file, shared.utf8Encoding));
|
||||
let config = JSON.parse(fs.readFileSync(file, shared.utf8Encoding));
|
||||
if (config.extends) {
|
||||
// Extend configuration
|
||||
config = shared.assign(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue