Skip to content

Commit

Permalink
Update every-n-lines custom rule to not use outdated forEachLine helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Jan 14, 2025
1 parent 5ba87b8 commit 5e1fbe9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
18 changes: 7 additions & 11 deletions test/customRules/rules/every-n-lines.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

"use strict";

const { forEachLine, getLineMetadata } = require("markdownlint/helpers");

/** @type {import("markdownlint").Rule} */
module.exports = {
"names": [ "every-n-lines" ],
"description": "Rule that reports an error every N lines",
"tags": [ "test" ],
"function": (params, onError) => {
const n = params.config.n || 2;
forEachLine(getLineMetadata(params), (line, lineIndex) => {
const lineNumber = lineIndex + 1;
if ((lineNumber % n) === 0) {
onError({
"lineNumber": lineNumber,
"detail": "Line number " + lineNumber
});
}
});
for (let lineNumber = n; lineNumber <= params.lines.length; lineNumber += n) {
onError({
lineNumber,
"detail": `Line number ${lineNumber}`
});
}
}
};
1 change: 1 addition & 0 deletions test/customRules/rules/first-line.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"use strict";

/** @type {import("markdownlint").Rule} */
module.exports = {
"names": [ "first-line" ],
"description": "Rule that reports an error for the first line",
Expand Down
2 changes: 2 additions & 0 deletions test/customRules/rules/second-line.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"use strict";

/** @type {import("markdownlint").Rule} */
module.exports = {
"names": [ "second-line" ],
"description": "Rule that reports an error for the second line",
Expand All @@ -15,6 +16,7 @@ module.exports = {
"lineNumber": 2
});
}
// @ts-ignore
resolve();
});
}
Expand Down
16 changes: 10 additions & 6 deletions test/snapshots/markdownlint-cli2-test-exec.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2691,12 +2691,13 @@ Generated by [AVA](https://avajs.dev).
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `dir/about.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
dir/about.md:1 first-line Rule that reports an error for the first line␊
stderr: `dir/about.md:1 first-line Rule that reports an error for the first line␊
dir/about.md:1:3 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:1:10 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊
dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊
dir/about.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
dir/subdir/hr.md:1:6 extended-ascii Only extended ASCII characters are allowed [Blocked character: '✅']␊
dir/subdir/hr.md:1 first-line Rule that reports an error for the first line␊
dir/subdir/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
Expand All @@ -2712,34 +2713,37 @@ Generated by [AVA](https://avajs.dev).
dir/subdir2/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
dir/subdir2/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
dir/subdir2/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/subdir3/info.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
dir/subdir3/info.md:1 first-line Rule that reports an error for the first line␊
dir/subdir3/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊
dir/subdir3/info.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## Information"]␊
dir/subdir3/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
dir/subdir3/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
dir/subdir3/info.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir/subdir3/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir2/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir2/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
dir3/hr.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
dir3/hr.md:1 first-line Rule that reports an error for the first line␊
dir3/hr.md:2 second-line Rule that reports an error for the second line␊
dir3/hr.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
dir4/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
viewme.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
viewme.md:1 first-line Rule that reports an error for the first line␊
viewme.md:3 any-blockquote Rule that reports an error for any blockquote [Blockquote spans 1 line(s).] [Context: "> Tagli"]␊
viewme.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
viewme.md:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
viewme.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
viewme.md:9 every-n-lines Rule that reports an error every N lines [Line number 9]␊
viewme.md:12 every-n-lines Rule that reports an error every N lines [Line number 12]␊
viewme.md:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
viewme.md:14:14 MD047/single-trailing-newline Files should end with a single newline character`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 9 file(s)␊
Summary: 45 error(s)`,
Summary: 49 error(s)`,
}

## customRules-pre-imported (exec)
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-exec.mjs.snap
Binary file not shown.
16 changes: 10 additions & 6 deletions test/snapshots/markdownlint-cli2-test-main.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2884,12 +2884,13 @@ Generated by [AVA](https://avajs.dev).
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `dir/about.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
dir/about.md:1 first-line Rule that reports an error for the first line␊
stderr: `dir/about.md:1 first-line Rule that reports an error for the first line␊
dir/about.md:1:3 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:1:10 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊
dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊
dir/about.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
dir/subdir/hr.md:1:6 extended-ascii Only extended ASCII characters are allowed [Blocked character: '✅']␊
dir/subdir/hr.md:1 first-line Rule that reports an error for the first line␊
dir/subdir/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
Expand All @@ -2905,35 +2906,38 @@ Generated by [AVA](https://avajs.dev).
dir/subdir2/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
dir/subdir2/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
dir/subdir2/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/subdir3/info.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
dir/subdir3/info.md:1 first-line Rule that reports an error for the first line␊
dir/subdir3/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊
dir/subdir3/info.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## Information"]␊
dir/subdir3/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
dir/subdir3/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
dir/subdir3/info.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir/subdir3/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir2/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir2/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
dir3/hr.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
dir3/hr.md:1 first-line Rule that reports an error for the first line␊
dir3/hr.md:2 second-line Rule that reports an error for the second line␊
dir3/hr.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
dir4/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
viewme.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
viewme.md:1 first-line Rule that reports an error for the first line␊
viewme.md:3 any-blockquote Rule that reports an error for any blockquote [Blockquote spans 1 line(s).] [Context: "> Tagli"]␊
viewme.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
viewme.md:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
viewme.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
viewme.md:9 every-n-lines Rule that reports an error every N lines [Line number 9]␊
viewme.md:12 every-n-lines Rule that reports an error every N lines [Line number 12]␊
viewme.md:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
viewme.md:14:14 MD047/single-trailing-newline Files should end with a single newline character␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 9 file(s)␊
Summary: 45 error(s)␊
Summary: 49 error(s)␊
`,
}

Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-main.mjs.snap
Binary file not shown.

0 comments on commit 5e1fbe9

Please sign in to comment.