Skip to content

Commit 337ff3c

Browse files
committed
✨ add bad linebreaks linter+fixer
1 parent a2f5b62 commit 337ff3c

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
},
1111
rules: {
1212
"arrow-spacing": "error",
13+
"brace-style": ["error", "1tbs"],
1314
"object-curly-spacing": ["error", "always"]
1415
},
1516
overrides: [

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
{
22
"private": true,
33
"scripts": {
4+
"bad-linebreaks": "node ./scripts/bad-linebreaks.mjs",
5+
"bad-linebreaks:fix": "npm run bad-linebreaks fix",
46
"check-delegates": "node -e 'import(\"./scripts/check-delegates.mjs\").then(cd => cd.checkDelegates())'",
57
"check-delegates-test": "node ./scripts/check-delegates-test.mjs",
8+
"fix:all": "npm run lint:fix && npm run mdlint:fix && npm run bad-linebreaks:fix",
69
"lint": "eslint . --ext .js,.mjs,.cjs",
710
"lint:fix": "npm run lint -- --fix",
811
"//": "markdownlint commands ignore old files",
912
"mdlint": "markdownlint-cli2 '**/*.md' '!node_modules' '!meetings/201*/*.md' '!meetings/202[0-2]*/*.md' '!meetings/2023-0[1-3]/*.md'",
1013
"mdlint:fix": "markdownlint-cli2-fix '**/*.md' '!node_modules' '!meetings/201*/*.md' '!meetings/202[0-2]*/*.md' '!meetings/2023-0[1-3]/*.md'",
1114
"//": "markdownlint most likely to fail, so run that first",
12-
"test": "npm run mdlint && npm run lint && npm run check-delegates-test && npm run check-delegates"
15+
"test": "npm run mdlint && npm run bad-linebreaks && npm run lint && npm run check-delegates-test && npm run check-delegates"
1316
},
1417
"devDependencies": {
1518
"eslint": "^8.41.0",
19+
"glob": "^10.3.3",
1620
"markdownlint-cli2": "^0.7.1"
1721
}
1822
}

scripts/bad-linebreaks.mjs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs';
4+
import { glob } from 'glob';
5+
6+
function getLine(txt, index) {
7+
8+
let line = 1;
9+
10+
for (let i = 0; i < index; i++) {
11+
if (txt[i] === '\n') {
12+
line += 1;
13+
}
14+
}
15+
16+
return line;
17+
18+
}
19+
20+
export function findBadLinebreaks(file, fix = false) {
21+
22+
let contents = fs.readFileSync(file, 'utf8').toString();
23+
24+
const re = /(?<=[\w\d ])\n(?=[\w\d])/g;
25+
const matches = Array.from(contents.matchAll(re));
26+
27+
for (const m of matches) {
28+
29+
if (fix) {
30+
contents = `${contents.slice(0, m.index).trimEnd()} ${contents.slice(m.index + 1)}`;
31+
} else {
32+
33+
const start = Math.max(0, m.index - 33);
34+
const end = Math.min(contents.length - 1, m.index + 33);
35+
36+
console.log(`found erroneous linebreak at line ${getLine(contents, m.index)}:\n${contents.slice(start, end)}\n`);
37+
38+
}
39+
40+
}
41+
42+
if (matches.length > 0) {
43+
44+
if (fix) {
45+
fs.writeFileSync(file, contents);
46+
console.log(`fixed ${matches.length} erroneous linebreaks`);
47+
} else {
48+
process.exitCode = 1;
49+
}
50+
51+
}
52+
53+
}
54+
55+
// patterns match what is in package.json mdlint scripts
56+
const files = await glob(
57+
'**/*.md',
58+
{
59+
ignore: [
60+
'node_modules/**',
61+
'meetings/201*/*.md',
62+
'meetings/202[0-2]*/*.md',
63+
'meetings/2023-0[1-3]/*.md',
64+
]
65+
}
66+
);
67+
68+
const fix = process.argv?.[2] === 'fix';
69+
70+
for (const f of files) {
71+
findBadLinebreaks(f, fix);
72+
}

0 commit comments

Comments
 (0)