Skip to content

Commit

Permalink
Snapshot custom rule definitions to prevent them from changing dynami…
Browse files Browse the repository at this point in the history
…cally.
  • Loading branch information
DavidAnson committed Jul 12, 2023
1 parent c699b8e commit 14a7529
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
5 changes: 4 additions & 1 deletion demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2628,8 +2628,11 @@ function lintInput(options, synchronous, callback) {
// Normalize inputs
options = options || {};
callback = callback || function noop() {};
var customRuleList = [options.customRules || []].flat().map(function (rule) {
return _objectSpread({}, rule);
});
// eslint-disable-next-line unicorn/prefer-spread
var ruleList = rules.concat(options.customRules || []);
var ruleList = rules.concat(customRuleList);
var ruleErr = validateRuleList(ruleList, synchronous);
if (ruleErr) {
callback(ruleErr);
Expand Down
6 changes: 5 additions & 1 deletion lib/markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,12 @@ function lintInput(options, synchronous, callback) {
// Normalize inputs
options = options || {};
callback = callback || function noop() {};
const customRuleList =
[ options.customRules || [] ]
.flat()
.map((rule) => ({ ...rule }));
// eslint-disable-next-line unicorn/prefer-spread
const ruleList = rules.concat(options.customRules || []);
const ruleList = rules.concat(customRuleList);
const ruleErr = validateRuleList(ruleList, synchronous);
if (ruleErr) {
callback(ruleErr);
Expand Down
45 changes: 45 additions & 0 deletions test/markdownlint-test-custom-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,51 @@ test("customRulesUsedTagName", (t) => new Promise((resolve) => {
});
}));

test("customRulesDefinitionStatic", (t) => new Promise((resolve) => {
t.plan(2);
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"information": new URL("https://example.com/information"),
"tags": [ "tag" ],
"function": (params, onError) => {
const definition = options.customRules[0];
definition.names = [ "changed" ];
definition.description = "changed";
definition.information = new URL("https://example.com/changed");
onError({
"lineNumber": 1
});
}
}
],
"strings": {
"string": "# Heading\n"
}
};
markdownlint(options, (err, actualResult) => {
t.falsy(err);
const expectedResult = {
"string": [
{
"lineNumber": 1,
"ruleNames": [ "name" ],
"ruleDescription": "description",
"ruleInformation": "https://example.com/information",
"errorDetail": null,
"errorContext": null,
"errorRange": null,
"fixInfo": null
}
]
};
t.deepEqual(actualResult, expectedResult, "Undetected issues.");
resolve();
});
}));

test("customRulesThrowForFile", (t) => new Promise((resolve) => {
t.plan(4);
const exceptionMessage = "Test exception message";
Expand Down

0 comments on commit 14a7529

Please sign in to comment.