Skip to content

Commit 5e1fbe9

Browse files
committed
Update every-n-lines custom rule to not use outdated forEachLine helper.
1 parent 5ba87b8 commit 5e1fbe9

7 files changed

+30
-23
lines changed

test/customRules/rules/every-n-lines.cjs

+7-11
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22

33
"use strict";
44

5-
const { forEachLine, getLineMetadata } = require("markdownlint/helpers");
6-
5+
/** @type {import("markdownlint").Rule} */
76
module.exports = {
87
"names": [ "every-n-lines" ],
98
"description": "Rule that reports an error every N lines",
109
"tags": [ "test" ],
1110
"function": (params, onError) => {
1211
const n = params.config.n || 2;
13-
forEachLine(getLineMetadata(params), (line, lineIndex) => {
14-
const lineNumber = lineIndex + 1;
15-
if ((lineNumber % n) === 0) {
16-
onError({
17-
"lineNumber": lineNumber,
18-
"detail": "Line number " + lineNumber
19-
});
20-
}
21-
});
12+
for (let lineNumber = n; lineNumber <= params.lines.length; lineNumber += n) {
13+
onError({
14+
lineNumber,
15+
"detail": `Line number ${lineNumber}`
16+
});
17+
}
2218
}
2319
};

test/customRules/rules/first-line.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
"use strict";
44

5+
/** @type {import("markdownlint").Rule} */
56
module.exports = {
67
"names": [ "first-line" ],
78
"description": "Rule that reports an error for the first line",

test/customRules/rules/second-line.cjs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
"use strict";
44

5+
/** @type {import("markdownlint").Rule} */
56
module.exports = {
67
"names": [ "second-line" ],
78
"description": "Rule that reports an error for the second line",
@@ -15,6 +16,7 @@ module.exports = {
1516
"lineNumber": 2
1617
});
1718
}
19+
// @ts-ignore
1820
resolve();
1921
});
2022
}

test/snapshots/markdownlint-cli2-test-exec.mjs.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -2691,12 +2691,13 @@ Generated by [AVA](https://avajs.dev).
26912691
formatterJson: '',
26922692
formatterJunit: '',
26932693
formatterSarif: '',
2694-
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]␊
2695-
dir/about.md:1 first-line Rule that reports an error for the first line␊
2694+
stderr: `dir/about.md:1 first-line Rule that reports an error for the first line␊
26962695
dir/about.md:1:3 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
26972696
dir/about.md:1:10 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
2697+
dir/about.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
26982698
dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊
26992699
dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊
2700+
dir/about.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
27002701
dir/subdir/hr.md:1:6 extended-ascii Only extended ASCII characters are allowed [Blocked character: '✅']␊
27012702
dir/subdir/hr.md:1 first-line Rule that reports an error for the first line␊
27022703
dir/subdir/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
@@ -2712,34 +2713,37 @@ Generated by [AVA](https://avajs.dev).
27122713
dir/subdir2/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
27132714
dir/subdir2/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
27142715
dir/subdir2/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
2715-
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]␊
27162716
dir/subdir3/info.md:1 first-line Rule that reports an error for the first line␊
27172717
dir/subdir3/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊
27182718
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"]␊
27192719
dir/subdir3/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
27202720
dir/subdir3/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
2721+
dir/subdir3/info.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
27212722
dir/subdir3/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
27222723
dir2/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
27232724
dir2/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
2724-
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]␊
27252725
dir3/hr.md:1 first-line Rule that reports an error for the first line␊
27262726
dir3/hr.md:2 second-line Rule that reports an error for the second line␊
2727+
dir3/hr.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
27272728
dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
27282729
dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
27292730
dir4/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
27302731
dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
2731-
viewme.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
27322732
viewme.md:1 first-line Rule that reports an error for the first line␊
27332733
viewme.md:3 any-blockquote Rule that reports an error for any blockquote [Blockquote spans 1 line(s).] [Context: "> Tagli"]␊
2734+
viewme.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
27342735
viewme.md:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
27352736
viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
2737+
viewme.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
27362738
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
2739+
viewme.md:9 every-n-lines Rule that reports an error every N lines [Line number 9]␊
2740+
viewme.md:12 every-n-lines Rule that reports an error every N lines [Line number 12]␊
27372741
viewme.md:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
27382742
viewme.md:14:14 MD047/single-trailing-newline Files should end with a single newline character`,
27392743
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
27402744
Finding: **/*.md␊
27412745
Linting: 9 file(s)␊
2742-
Summary: 45 error(s)`,
2746+
Summary: 49 error(s)`,
27432747
}
27442748

27452749
## customRules-pre-imported (exec)
Binary file not shown.

test/snapshots/markdownlint-cli2-test-main.mjs.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -2884,12 +2884,13 @@ Generated by [AVA](https://avajs.dev).
28842884
formatterJson: '',
28852885
formatterJunit: '',
28862886
formatterSarif: '',
2887-
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]␊
2888-
dir/about.md:1 first-line Rule that reports an error for the first line␊
2887+
stderr: `dir/about.md:1 first-line Rule that reports an error for the first line␊
28892888
dir/about.md:1:3 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
28902889
dir/about.md:1:10 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
2890+
dir/about.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
28912891
dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊
28922892
dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊
2893+
dir/about.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
28932894
dir/subdir/hr.md:1:6 extended-ascii Only extended ASCII characters are allowed [Blocked character: '✅']␊
28942895
dir/subdir/hr.md:1 first-line Rule that reports an error for the first line␊
28952896
dir/subdir/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
@@ -2905,35 +2906,38 @@ Generated by [AVA](https://avajs.dev).
29052906
dir/subdir2/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
29062907
dir/subdir2/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
29072908
dir/subdir2/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
2908-
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]␊
29092909
dir/subdir3/info.md:1 first-line Rule that reports an error for the first line␊
29102910
dir/subdir3/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊
29112911
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"]␊
29122912
dir/subdir3/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
29132913
dir/subdir3/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
2914+
dir/subdir3/info.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
29142915
dir/subdir3/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
29152916
dir2/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
29162917
dir2/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
2917-
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]␊
29182918
dir3/hr.md:1 first-line Rule that reports an error for the first line␊
29192919
dir3/hr.md:2 second-line Rule that reports an error for the second line␊
2920+
dir3/hr.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
29202921
dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
29212922
dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
29222923
dir4/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
29232924
dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
2924-
viewme.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊
29252925
viewme.md:1 first-line Rule that reports an error for the first line␊
29262926
viewme.md:3 any-blockquote Rule that reports an error for any blockquote [Blockquote spans 1 line(s).] [Context: "> Tagli"]␊
2927+
viewme.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
29272928
viewme.md:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
29282929
viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
2930+
viewme.md:6 every-n-lines Rule that reports an error every N lines [Line number 6]␊
29292931
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
2932+
viewme.md:9 every-n-lines Rule that reports an error every N lines [Line number 9]␊
2933+
viewme.md:12 every-n-lines Rule that reports an error every N lines [Line number 12]␊
29302934
viewme.md:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
29312935
viewme.md:14:14 MD047/single-trailing-newline Files should end with a single newline character␊
29322936
`,
29332937
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
29342938
Finding: **/*.md␊
29352939
Linting: 9 file(s)␊
2936-
Summary: 45 error(s)␊
2940+
Summary: 49 error(s)␊
29372941
`,
29382942
}
29392943

Binary file not shown.

0 commit comments

Comments
 (0)