Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add bad linebreaks linter+fixer #269

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
},
rules: {
"arrow-spacing": "error",
"brace-style": ["error", "1tbs"],
"object-curly-spacing": ["error", "always"]
},
overrides: [
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"private": true,
"scripts": {
"bad-linebreaks": "node ./scripts/bad-linebreaks.mjs",
"bad-linebreaks:fix": "npm run bad-linebreaks fix",
"check-delegates": "node -e 'import(\"./scripts/check-delegates.mjs\").then(cd => cd.checkDelegates())'",
"check-delegates-test": "node ./scripts/check-delegates-test.mjs",
"fix:all": "npm run lint:fix && npm run mdlint:fix && npm run bad-linebreaks:fix",
"lint": "eslint . --ext .js,.mjs,.cjs",
"lint:fix": "npm run lint -- --fix",
"//": "markdownlint commands ignore old files",
"mdlint": "markdownlint-cli2 '**/*.md' '!node_modules' '!meetings/201*/*.md' '!meetings/202[0-2]*/*.md' '!meetings/2023-0[1-3]/*.md'",
"mdlint:fix": "markdownlint-cli2-fix '**/*.md' '!node_modules' '!meetings/201*/*.md' '!meetings/202[0-2]*/*.md' '!meetings/2023-0[1-3]/*.md'",
ctcpip marked this conversation as resolved.
Show resolved Hide resolved
"//": "markdownlint most likely to fail, so run that first",
"test": "npm run mdlint && npm run lint && npm run check-delegates-test && npm run check-delegates"
"test": "npm run mdlint && npm run bad-linebreaks && npm run lint && npm run check-delegates-test && npm run check-delegates"
ctcpip marked this conversation as resolved.
Show resolved Hide resolved
ctcpip marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"eslint": "^8.41.0",
"glob": "^10.3.3",
"markdownlint-cli2": "^0.7.1"
}
}
72 changes: 72 additions & 0 deletions scripts/bad-linebreaks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env node

import fs from 'fs';
import { glob } from 'glob';

function getLine(txt, index) {

let line = 1;

for (let i = 0; i < index; i++) {
if (txt[i] === '\n') {
line += 1;
}
}

return line;

ctcpip marked this conversation as resolved.
Show resolved Hide resolved
}

export function findBadLinebreaks(file, fix = false) {

let contents = fs.readFileSync(file, 'utf8').toString();

const re = /(?<=[\w\d ])\n(?=[\w\d])/g;
const matches = Array.from(contents.matchAll(re));

for (const m of matches) {

if (fix) {
contents = `${contents.slice(0, m.index).trimEnd()} ${contents.slice(m.index + 1)}`;
} else {

const start = Math.max(0, m.index - 33);
const end = Math.min(contents.length - 1, m.index + 33);

console.log(`found erroneous linebreak at line ${getLine(contents, m.index)}:\n${contents.slice(start, end)}\n`);

}

}

if (matches.length > 0) {

if (fix) {
fs.writeFileSync(file, contents);
console.log(`fixed ${matches.length} erroneous linebreaks`);
} else {
process.exitCode = 1;
}

}

}

// patterns match what is in package.json mdlint scripts
const files = await glob(
'**/*.md',
{
ignore: [
'node_modules/**',
'meetings/201*/*.md',
'meetings/202[0-2]*/*.md',
'meetings/2023-0[1-3]/*.md',
]
}
);

const fix = process.argv?.[2] === 'fix';

for (const f of files) {
findBadLinebreaks(f, fix);
}
Loading