Add markdownlint "version" property to custom rule "params" object in case it is ever necessary for a rule to specialize its behavior.

This commit is contained in:
David Anson 2024-09-29 18:11:41 -07:00
parent ee50519e00
commit d3819c4e75
6 changed files with 39 additions and 5 deletions

View file

@ -1657,7 +1657,7 @@ module.exports = {
const path = __webpack_require__(/*! node:path */ "?9a52"); const path = __webpack_require__(/*! node:path */ "?9a52");
const { promisify } = __webpack_require__(/*! node:util */ "?39e5"); const { promisify } = __webpack_require__(/*! node:util */ "?39e5");
const micromark = __webpack_require__(/*! ../helpers/micromark-parse.cjs */ "../helpers/micromark-parse.cjs"); const micromark = __webpack_require__(/*! ../helpers/micromark-parse.cjs */ "../helpers/micromark-parse.cjs");
// const { deprecatedRuleNames } = require("./constants"); const { version } = __webpack_require__(/*! ./constants */ "../lib/constants.js");
const rules = __webpack_require__(/*! ./rules */ "../lib/rules.js"); const rules = __webpack_require__(/*! ./rules */ "../lib/rules.js");
const helpers = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const helpers = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const cache = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const cache = __webpack_require__(/*! ./cache */ "../lib/cache.js");
@ -2177,6 +2177,7 @@ function lintContent(
const parsersNone = Object.freeze({}); const parsersNone = Object.freeze({});
const paramsBase = { const paramsBase = {
name, name,
version,
"lines": Object.freeze(lines), "lines": Object.freeze(lines),
"frontMatterLines": Object.freeze(frontMatterLines) "frontMatterLines": Object.freeze(frontMatterLines)
}; };
@ -2861,7 +2862,7 @@ function readConfigSync(file, parsers, fs) {
* @returns {string} SemVer string. * @returns {string} SemVer string.
*/ */
function getVersion() { function getVersion() {
return (__webpack_require__(/*! ./constants */ "../lib/constants.js").version); return version;
} }
// Export a/synchronous/Promise APIs // Export a/synchronous/Promise APIs
@ -2898,6 +2899,7 @@ module.exports = markdownlint;
* @property {readonly string[]} lines File/string lines. * @property {readonly string[]} lines File/string lines.
* @property {readonly string[]} frontMatterLines Front matter lines. * @property {readonly string[]} frontMatterLines Front matter lines.
* @property {RuleConfiguration} config Rule configuration. * @property {RuleConfiguration} config Rule configuration.
* @property {string} version Version of the markdownlint library.
*/ */
/* eslint-enable jsdoc/valid-types */ /* eslint-enable jsdoc/valid-types */

View file

@ -85,6 +85,7 @@ A rule is implemented as an `Object`:
front matter (not present in `lines`). front matter (not present in `lines`).
- `config` is an `Object` corresponding to the rule's entry in - `config` is an `Object` corresponding to the rule's entry in
`options.config` (if present). `options.config` (if present).
- `version` is a `String` that corresponds to the version of `markdownlint`
- `onError` is a function that takes a single `Object` parameter with one - `onError` is a function that takes a single `Object` parameter with one
required and four optional properties: required and four optional properties:
- `lineNumber` is a required `Number` specifying the 1-based line number of - `lineNumber` is a required `Number` specifying the 1-based line number of

View file

@ -136,7 +136,8 @@ const testRule: markdownlint.Rule = {
"frontMatterLines": [ "frontMatterLines": [
"three" "three"
], ],
"config": options.config "config": options.config,
"version": "1.2.3"
}; };
assert(ruleParams); assert(ruleParams);
let ruleOnErrorInfo: markdownlint.RuleOnErrorInfo; let ruleOnErrorInfo: markdownlint.RuleOnErrorInfo;

View file

@ -77,6 +77,10 @@ type RuleParams = {
* Rule configuration. * Rule configuration.
*/ */
config: RuleConfiguration; config: RuleConfiguration;
/**
* Version of the markdownlint library.
*/
version: string;
}; };
/** /**
* Markdown parser data. * Markdown parser data.

View file

@ -5,7 +5,7 @@
const path = require("node:path"); const path = require("node:path");
const { promisify } = require("node:util"); const { promisify } = require("node:util");
const micromark = require("../helpers/micromark-parse.cjs"); const micromark = require("../helpers/micromark-parse.cjs");
// const { deprecatedRuleNames } = require("./constants"); const { version } = require("./constants");
const rules = require("./rules"); const rules = require("./rules");
const helpers = require("../helpers"); const helpers = require("../helpers");
const cache = require("./cache"); const cache = require("./cache");
@ -525,6 +525,7 @@ function lintContent(
const parsersNone = Object.freeze({}); const parsersNone = Object.freeze({});
const paramsBase = { const paramsBase = {
name, name,
version,
"lines": Object.freeze(lines), "lines": Object.freeze(lines),
"frontMatterLines": Object.freeze(frontMatterLines) "frontMatterLines": Object.freeze(frontMatterLines)
}; };
@ -1209,7 +1210,7 @@ function readConfigSync(file, parsers, fs) {
* @returns {string} SemVer string. * @returns {string} SemVer string.
*/ */
function getVersion() { function getVersion() {
return require("./constants").version; return version;
} }
// Export a/synchronous/Promise APIs // Export a/synchronous/Promise APIs
@ -1246,6 +1247,7 @@ module.exports = markdownlint;
* @property {readonly string[]} lines File/string lines. * @property {readonly string[]} lines File/string lines.
* @property {readonly string[]} frontMatterLines Front matter lines. * @property {readonly string[]} frontMatterLines Front matter lines.
* @property {RuleConfiguration} config Rule configuration. * @property {RuleConfiguration} config Rule configuration.
* @property {string} version Version of the markdownlint library.
*/ */
/* eslint-enable jsdoc/valid-types */ /* eslint-enable jsdoc/valid-types */

View file

@ -1338,6 +1338,30 @@ test("customRulesOnErrorInvalidHandledSync", (t) => {
t.deepEqual(actualResult, expectedResult, "Undetected issues."); t.deepEqual(actualResult, expectedResult, "Undetected issues.");
}); });
test("customRulesVersion", (t) => new Promise((resolve) => {
t.plan(2);
// eslint-disable-next-line jsdoc/valid-types
/** @type import("../lib/markdownlint").Options */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "none",
"function": function stringName(params) {
t.is(params.version, version, "Incorrect version");
}
}
],
"files": "doc/CustomRules.md"
};
markdownlint(options, function callback(err) {
t.falsy(err);
resolve();
});
}));
test("customRulesFileName", (t) => new Promise((resolve) => { test("customRulesFileName", (t) => new Promise((resolve) => {
t.plan(2); t.plan(2);
// eslint-disable-next-line jsdoc/valid-types // eslint-disable-next-line jsdoc/valid-types