Skip to content

Commit

Permalink
Allow a custom rule's onError implementation to override that rule's …
Browse files Browse the repository at this point in the history
…information URL for each error.
  • Loading branch information
DavidAnson committed Jul 12, 2023
1 parent 14974e5 commit c699b8e
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 18 deletions.
16 changes: 13 additions & 3 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ module.exports.isEmptyString = function isEmptyString(str) {

// Returns true iff the input is an object
module.exports.isObject = function isObject(obj) {
return obj !== null && _typeof(obj) === "object" && !Array.isArray(obj);
return !!obj && _typeof(obj) === "object" && !Array.isArray(obj);
};

// Returns true iff the input is a URL
module.exports.isUrl = function isUrl(obj) {
return !!obj && Object.getPrototypeOf(obj) === URL.prototype;
};

/**
Expand Down Expand Up @@ -1712,7 +1717,7 @@ function validateRuleList(ruleList, synchronous) {
result = newError(_property);
}
}
if (!result && rule.information && Object.getPrototypeOf(rule.information) !== URL.prototype) {
if (!result && rule.information && !helpers.isUrl(rule.information)) {
result = newError("information");
}
if (!result && rule.asynchronous !== undefined && typeof rule.asynchronous !== "boolean") {
Expand Down Expand Up @@ -2401,6 +2406,9 @@ function lintContent(ruleList, aliasToRuleNames, name, content, md, config, conf
if (errorInfo.context && !helpers.isString(errorInfo.context)) {
throwError("context");
}
if (errorInfo.information && !helpers.isUrl(errorInfo.information)) {
throwError("information");
}
if (errorInfo.range && (!Array.isArray(errorInfo.range) || errorInfo.range.length !== 2 || !helpers.isNumber(errorInfo.range[0]) || errorInfo.range[0] < 1 || !helpers.isNumber(errorInfo.range[1]) || errorInfo.range[1] < 1 || errorInfo.range[0] + errorInfo.range[1] - 1 > lines[errorInfo.lineNumber - 1].length)) {
throwError("range");

This comment has been minimized.

Copy link
@sufferlx

sufferlx Oct 16, 2023

This comment has been minimized.

Copy link
@Azizjon8822

Azizjon8822 Mar 14, 2024

все нормально

}
Expand Down Expand Up @@ -2436,12 +2444,13 @@ function lintContent(ruleList, aliasToRuleNames, name, content, md, config, conf
cleanFixInfo.insertText = fixInfo.insertText;
}
}
var information = errorInfo.information || rule.information;
results.push({
lineNumber: lineNumber,
"ruleName": rule.names[0],
"ruleNames": rule.names,
"ruleDescription": rule.description,
"ruleInformation": rule.information ? rule.information.href : null,
"ruleInformation": information ? information.href : null,
"errorDetail": errorInfo.detail || null,
"errorContext": errorInfo.context || null,
"errorRange": errorInfo.range ? _toConsumableArray(errorInfo.range) : null,
Expand Down Expand Up @@ -3036,6 +3045,7 @@ module.exports = markdownlint;
* @property {number} lineNumber Line number (1-based).
* @property {string} [detail] Detail about the error.
* @property {string} [context] Context for the error.
* @property {URL} [information] Link to more information.
* @property {number[]} [range] Column number (1-based) and length.
* @property {RuleOnErrorFixInfo} [fixInfo] Fix information.
*/
Expand Down
2 changes: 2 additions & 0 deletions doc/CustomRules.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ properties:
error.
- `context` is an optional `String` with relevant text surrounding the error
location.
- `information` is an optional (absolute) `URL` of a link to override the
same-named value provided by the rule definition. (Uncommon)
- `range` is an optional `Array` with two `Number` values identifying the
1-based column and length of the error.
- `fixInfo` is an optional `Object` with information about how to fix the
Expand Down
3 changes: 2 additions & 1 deletion example/typescript/type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ markdownlint(options, assertLintResultsCallback);
const testRule = {
"names": [ "test-rule" ],
"description": "Test rule",
"information": new URL("https://example.com/test-rule"),
"information": new URL("https://example.com/rule-information"),
"tags": [ "test-tag" ],
"function": function rule(params: markdownlint.RuleParams, onError: markdownlint.RuleOnError) {
assert(!!params);
Expand All @@ -136,6 +136,7 @@ const testRule = {
"lineNumber": 1,
"detail": "detail",
"context": "context",
"information": new URL("https://example.com/error-information"),
"range": [ 1, 2 ],
"fixInfo": {
"lineNumber": 1,
Expand Down
7 changes: 6 additions & 1 deletion helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ module.exports.isEmptyString = function isEmptyString(str) {

// Returns true iff the input is an object
module.exports.isObject = function isObject(obj) {
return (obj !== null) && (typeof obj === "object") && !Array.isArray(obj);
return !!obj && (typeof obj === "object") && !Array.isArray(obj);
};

// Returns true iff the input is a URL
module.exports.isUrl = function isUrl(obj) {
return !!obj && (Object.getPrototypeOf(obj) === URL.prototype);
};

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/markdownlint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ type RuleOnErrorInfo = {
* Context for the error.
*/
context?: string;
/**
* Link to more information.
*/
information?: URL;
/**
* Column number (1-based) and length.
*/
Expand Down
10 changes: 8 additions & 2 deletions lib/markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function validateRuleList(ruleList, synchronous) {
if (
!result &&
rule.information &&
(Object.getPrototypeOf(rule.information) !== URL.prototype)
!helpers.isUrl(rule.information)
) {
result = newError("information");
}
Expand Down Expand Up @@ -629,6 +629,10 @@ function lintContent(
!helpers.isString(errorInfo.context)) {
throwError("context");
}
if (errorInfo.information &&
!helpers.isUrl(errorInfo.information)) {
throwError("information");
}
if (errorInfo.range &&
(!Array.isArray(errorInfo.range) ||
(errorInfo.range.length !== 2) ||
Expand Down Expand Up @@ -681,12 +685,13 @@ function lintContent(
cleanFixInfo.insertText = fixInfo.insertText;
}
}
const information = errorInfo.information || rule.information;
results.push({
lineNumber,
"ruleName": rule.names[0],
"ruleNames": rule.names,
"ruleDescription": rule.description,
"ruleInformation": rule.information ? rule.information.href : null,
"ruleInformation": information ? information.href : null,
"errorDetail": errorInfo.detail || null,
"errorContext": errorInfo.context || null,
"errorRange": errorInfo.range ? [ ...errorInfo.range ] : null,
Expand Down Expand Up @@ -1314,6 +1319,7 @@ module.exports = markdownlint;
* @property {number} lineNumber Line number (1-based).
* @property {string} [detail] Detail about the error.
* @property {string} [context] Context for the error.
* @property {URL} [information] Link to more information.
* @property {number[]} [range] Column number (1-based) and length.
* @property {RuleOnErrorFixInfo} [fixInfo] Fix information.
*/
Expand Down

0 comments on commit c699b8e

Please sign in to comment.