Skip to content

Instantly share code, notes, and snippets.

@craigbeck
Created March 15, 2016 22:45
Show Gist options
  • Save craigbeck/a029da1d8f8cd34dbefc to your computer and use it in GitHub Desktop.
Save craigbeck/a029da1d8f8cd34dbefc to your computer and use it in GitHub Desktop.
automatic fix semicolons from eslint output
#!/usr/bin/env node
var fs = require("fs");
function fixFile(filename, lineNumbers) {
fs.readFile(filename, { encoding: "utf-8", flag: "r" }, function (err, buffer) {
if (err) {
console.error("Could not read", filename, err);
return;
}
if (!buffer) {
console.warn("Empty file", filname);
return;
}
var lines = buffer.toString().split("\n");
lineNumbers.forEach(function (i) {
lines[i-1] = lines[i-1] + ";";
});
var fixed = lines.join("\n");
fs.writeFile(filename, fixed, { encoding: "utf-8", flag: "w" }, function (err) {
if (err) {
console.error("Error writing", filename, error);
return;
}
console.log("FIXED", lineNumbers.length, filename);
});
});
}
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on("data", function (data) {
console.log(data.length, "::", data.substring(0, Math.min(64, data.length)));
var lines = data.split("\n");
console.log("lines", lines.length);
var file = null;
var linesToFix = [];
var COUNT = lines.length;
for (var i = 0; i < COUNT; i++) {
var line = lines[i];
if (line === "") {
if (file) {
fixFile(file, linesToFix);
}
file = null;
linesToFix = [];
continue;
}
if (/^\/.+\.jsx?$/.test(line)) {
file = line;
continue;
}
if (!file) {
continue;
}
// check rule
var rule = /^\s{2}(\d{1,}):\d{1,}.+\s{2}semi$/;
var matches = rule.exec(line);
if (matches) {
linesToFix.push(matches[1]);
}
}
});

usage

> eslint . | fixsemi.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment